VBA腳本實現痕跡保留、手寫簽名、電子印章的插件

<script language=javascript>
//系統變量
var vFileName=""; //打開的文件名
var vObject=null; //打開的對象
var gFileName=""; //文件名
var gObject=null; //對象
var gOpened=false; //是否被打開
var gUserName=""; //系統用戶
</script>


<script language="JScript" for=WebOffice event="OnDocumentOpened(vFileName,vObject)">
 OnDocumentOpened(vFileName,vObject); //打開文件事件
</script>

<script language="JScript" for=WebOffice event="OnDocumentClosed()">
 OnDocumentClosed();   //關閉文件事件
</script>

<script language=javascript>
//打開文檔,根據用戶權限(EditType),進行初始化設置
function OnDocumentOpened(vFileName,vObject) 
{
  var mProtect,mTools,mTrack,mPrint,mShow;
  gFileName=vFileName;    //取得全局文件名
  gObject=vObject;    //取得全局對象
  //設置痕跡和保護狀態
  if (webform.WebOffice.EditType=="0"){
 mProtect=true;
 mTools=false;
 mTrack=false;
 mPrint=false;
 mShow=true;
  }else if(webform.WebOffice.EditType=="1"){
 mProtect=false;
 mTools=false;
 mTrack=false;
 mPrint=false;
 mShow=false;  
  }else if(webform.WebOffice.EditType=="2"){
 mProtect=false;
 mTools=false;
 mTrack=true;
 mPrint=true;
 mShow=true;
  }else if(webform.WebOffice.EditType=="3"){
 mProtect=false;
 mTools=true;
 mTrack=true;
 mPrint=true;
 mShow=true;
  }else{
 var mType="";
 var mEdit=webform.WebOffice.EditType;
 var mIdx=mEdit.indexOf(",");
 if (mIdx>0){
     mType=mEdit.substring(0,mIdx);
 }
 mEdit=mEdit.substring(mIdx+1,mEdit.length);

 mIdx=mEdit.indexOf(",");
 if (mIdx>0){
     mType=mEdit.substring(0,mIdx);
     if (mType=="1") mProtect=true;
     if (mType=="0") mProtect=false;
 }
 mEdit=mEdit.substring(mIdx+1,mEdit.length);

 var mIdx=mEdit.indexOf(",");
 if (mIdx>0){
     mType=mEdit.substring(0,mIdx);
     if (mType=="1") mShow=true;
     if (mType=="0") mShow=false;
 }
 mEdit=mEdit.substring(mIdx+1,mEdit.length);

 var mIdx=mEdit.indexOf(",");
 if (mIdx>0){
     mType=mEdit.substring(0,mIdx);
     if (mType=="1") mTrack=true;
     if (mType=="0") mTrack=false;
 }
 mEdit=mEdit.substring(mIdx+1,mEdit.length);  

    var mIdx=mEdit.indexOf(",");
 if (mIdx>0){
     mType=mEdit.substring(0,mIdx);
     if (mType=="1") mPrint=true;
     if (mType=="0") mPrint=false;
 }
 mEdit=mEdit.substring(mIdx+1,mEdit.length);  
 
    var mIdx=mEdit.indexOf(",");
 if (mIdx>0){
     mType=mEdit.substring(0,mIdx);
     if (mType=="1") mTools=true;
     if (mType=="0") mTools=false;
 }
 
  }

  if (webform.WebOffice.FileType==".doc"){
   gObject.TrackRevisions=mTrack;
  gObject.PrintRevisions=mPrint;
   gObject.ShowRevisions=mShow;

   gObject.CommandBars('Reviewing').Enabled =mTools;
   gObject.CommandBars('Reviewing').Visible =mTools;

   var mCount=gObject.CommandBars('Track Changes').Controls.Count;
         for (var mIndex = 1; mIndex<= mCount; mIndex++){
           var mCurID=gObject.CommandBars('Track Changes').Controls(mIndex).ID;
           if ((mCurID==1715) || (mCurID==1716) || (mCurID==2041) || (mCurID==305)) {
              gObject.CommandBars('Track Changes').Controls(mIndex).Enabled = mTools;
           }
         }
 if (mProtect){
    gObject.Protect(2);
 }
 if (gUserName == "") {
          //保存原來的用戶
          gUserName=gObject.Application.UserName;
          //設置系統當前用戶
          gObject.Application.UserName=webform.WebOffice.UserName;
 }
  }


  if (webform.WebOffice.FileType==".xls"){
 if (mShow){
          if (mTrack){
              gObject.Application.DisplayCommentIndicator = 1;
   }
 }else{
          gObject.Application.DisplayCommentIndicator = 0;
 }

   gObject.CommandBars('Reviewing').Enabled =mTools;
   gObject.CommandBars('Reviewing').Visible =mTools;

  if (mProtect){
            for (var mIndex=1;mIndex<=gObject.Application.ActiveWorkbook.Sheets.Count;mIndex++){
                var mSheet=gObject.Application.ActiveWorkbook.Sheets(mIndex);
                mSheet.Protect("", true, true, true);
            }
 }

 if (gUserName == "") {
          //保存原來的用戶
          gUserName=gObject.Application.UserName;
          //設置系統當前用戶
          gObject.Application.UserName=webform.WebOffice.UserName;
 }
  }
  //打開成功
  gOpened = true;  
}

function OnDocumentClosed()
{
  //關閉成功
  gOpened = false;
}

//顯示系統狀態信息
function StatusMsg(mString){
  StatusBar.innerText=mString;
}

//頁面裝入事件
function Load(){
  //給控件屬性付值
  try{
  webform.WebOffice.WebUrl="<%=mServerUrl%>";
  webform.WebOffice.RecordID="<%=mRecordID%>";
  webform.WebOffice.Template="<%=mTemplate%>";
  webform.WebOffice.FileName="<%=mFileName%>";
  webform.WebOffice.FileType="<%=mFileType%>";
  webform.WebOffice.EditType="<%=mEditType%>";
  webform.WebOffice.UserName="<%=mUserName%>";
  //打開文檔,執行發送LOADFILE操作
  webform.WebOffice.WebOpen();
  if (webform.WebOffice.Error!=""){
 //如果不成功,執行發送LOADTEMPLATE操作
 webform.WebOffice.WebLoadTemplate();
 if (webform.WebOffice.Error!=""){
  //如果還沒有成功,則根據擴展名創建新文檔
  if (webform.WebOffice.FileType==".doc"){
   webform.WebOffice.CreateNew("Word.Document");
  }else if(webform.WebOffice.FileType==".xls"){
   webform.WebOffice.CreateNew("Excel.Sheet");
  }else if(webform.WebOffice.FileType==".ppt"){
   webform.WebOffice.CreateNew("PowerPoint.Show");
  }else{
   webform.WebOffice.CreateNew("");
  }
 }
  }
  }catch(e){
  }
  //顯示狀態信息
  StatusMsg(webform.WebOffice.Status);
}

//頁面退出事件
function UnLoad(){
  try{
     //關閉OCX控件
     webform.WebOffice.WebClose();
  }catch(e){}
}

//設置是否顯示痕跡
function ShowRevision(vValue){
  var object=webform.WebOffice.ActiveDocument;
   object.ShowRevisions=vValue;
  if (vValue){
     StatusMsg("顯示痕跡...");
  }else{
     StatusMsg("隱藏痕跡...");
  }
}

//調入文檔
function LoadDocument(){
  StatusMsg("正在打開文檔...");
  webform.WebOffice.WebOpen();
  StatusMsg(webform.WebOffice.Status);
}

//保存文檔
function SaveDocument(){
  //判斷狀態
  if (webform.EditType.value=="0"){
     alert("你在查看狀態,不能保存!");
     return false;
  }
  //保存文檔
  webform.WebOffice.WebSave();
  //如果沒有錯誤
  if (webform.WebOffice.Error!=""){
     StatusMsg(webform.WebOffice.Status);
     return false;
  }else{
     StatusMsg(webform.WebOffice.Status);
     return true;
  }
}

//打開版本信息
function WebOpenVersion(){
  var mDialogUrl = "Version/VersionList.asp?RecordID="+webform.WebOffice.RecordID;
  var mObject = new Object();
      mObject.FileID = "";
      mObject.Result = false;
      window.showModalDialog(mDialogUrl, mObject, "dialogHeight:280px; dialogWidth:420px;center:yes;scroll:no;status:no;");
  //如果用戶確認選擇
  if (mObject.Result &&(mObject.FileID.length>0)){
     //清除所有設置文本信息
     webform.WebOffice.WebMsgTextClear();
     //設置COMMAND爲LOADVERSION
     webform.WebOffice.WebSetMsgByName("COMMAND","LOADVERSION");
     //設置用戶選擇的版本編號
     webform.WebOffice.WebSetMsgByName("FILEID",mObject.FileID);
     //發送命令給服務器
     webform.WebOffice.WebSendMessage();
     //如果沒有錯誤
     if (webform.WebOffice.Error==""){
       //關閉當前文檔
       webform.WebOffice.Close();
       //保存新版本爲系統文件名
       webform.WebOffice.WebMsgFileSave(gFileName);
       //如果保存成功
       if (webform.WebOffice.Error==""){
   //打開該版本文檔,
          webform.WebOffice.Open(gFileName,true);
       } 
    }else{
       alert(webform.WebOffice.Error);
    }
  }else{
    StatusMsg("取消打開版本");
  }
  StatusMsg(webform.WebOffice.Status);
}

//保存版本信息
function WebSaveVersion(){
  var mDialogUrl = "Version/VersionForm.asp";
  var mObject = new Object();
      mObject.Descript = "";
      mObject.Result=false;
      window.showModalDialog(mDialogUrl, mObject, "dialogHeight:200px; dialogWidth:360px;center:yes;scroll:no;status:no;");
      //如果用戶確認選擇
      if (mObject.Result){
        //清除所有設置文本信息
        webform.WebOffice.WebMsgTextClear();
        //設置COMMAND爲SAVEVERSION 
        webform.WebOffice.WebSetMsgByName("COMMAND","SAVEVERSION");
        //設置描述信息內容
        webform.WebOffice.WebSetMsgByName("DESCRIPT",mObject.Descript);
        //保存當前版本
        webform.WebOffice.Save(gFileName,true);
        //調入該文檔
        webform.WebOffice.WebMsgFileLoad(gFileName);
        //發送到服務器上
        webform.WebOffice.WebSendMessage();
        if (webform.WebOffice.Error!=""){
           alert(webform.WebOffice.Error);
       }
     }
     StatusMsg(webform.WebOffice.Status);
}

//用數據庫中的內容填充標籤裏的內容
function LoadBookmarks(){
 var mCount,mIndex;
 var mBookObject,mBookName,mBookIdx;
    //清除所有設置變量信息
    webform.WebOffice.WebMsgTextClear();
    //設置COMMAND爲LOADBOOKMARKS
    webform.WebOffice.WebSetMsgByName("COMMAND","LOADBOOKMARKS");
    //發送信息到服務器上
    webform.WebOffice.WebSendMessage();
    //如果沒有錯誤信息
    if (webform.WebOffice.Error==""){
      //如果是Word文件  
      if (webform.WebOffice.FileType==".doc"){
         //替換所有的標籤內容
         for (mBookIdx = 1; mBookIdx<=webform.WebOffice.ActiveDocument.Bookmarks.Count;mBookIdx++){
      //取出標籤名
             mBookName = webform.WebOffice.ActiveDocument.Bookmarks.Item(mBookIdx).Name;
             //取出該標籤對應的服務器上的數據
             mBookValue = webform.WebOffice.WebGetMsgByName(mBookName);
             //如果該數據有值 
             if ((mBookValue)&&(mBookValue.length>0)) {
   //取得標籤對應的對象
                 mBookObject = webform.WebOffice.ActiveDocument.Bookmarks.Item(mBookIdx).Range;
   //設置爲服務器數據
   mBookObject.Text=mBookValue;
   //重新設置爲標籤
                 webform.WebOffice.ActiveDocument.Bookmarks.Add(mBookName, mBookObject);
             }
         }
      }
    }
    StatusMsg(webform.WebOffice.Status);
}

//設置標籤的內容
function SetBookmarks(vbmName,vbmValue){
  //取得標籤對象
  var mBookmarks=WebFindBookmarks(vbmName);
  if (mBookmarks){
     //設置該標籤的內容爲新值
     mBookmarks.Text=vbmValue;
     //重新設置爲標籤
     webform.WebOffice.ActiveDocument.Bookmarks.Add(vbmName, mBookmarks);
  }
}

//取得標籤內容
function GetBookmarks(vbmName){
  var vbmValue="";
  //取得標籤對象
  var mBookmarks=WebFindBookmarks(vbmName);
  if (mBookmarks){
     //取得內容
     vbmValue=mBookmarks.Text;
  }
  return vbmValue;
}

//調用打印
function WebOpenPrint(){
  try{
    //調用打印功能,true表示顯示打印設置,false表示直接打印
    webform.WebOffice.PrintOut(true);
    StatusMsg(webform.WebOffice.Status);
  }catch(e){}
}

//調用打印頁面設置對話匡
function WebOpenPageSetup(){
   try{
 if (webform.WebOffice.FileType==".doc"){
            //如果是word
     webform.WebOffice.ActiveDocument.Application.Dialogs(178).Show();
 }else{
            //如果是Excel
     webform.WebOffice.ActiveDocument.Application.Dialogs(7).Show();
 }
   }catch(e){

   }

}
//調用打開圖片對話框
function WebOpenPicture(){
   try{
 if (webform.WebOffice.FileType==".doc"){
   //如果是Word
   webform.WebOffice.ActiveDocument.Application.Dialogs(163).Show();
 }else{
          //如果是Excel
   webform.WebOffice.ActiveDocument.Application.Dialogs(342).Show();
 }
   }catch(e){

   }
}

//電子印章功能
function WebOpenSignature(){
  var mDialogUrl = "Signature/SignatureForm.asp";
  var mMarkName,mPassWord,mMarkTime;
  var mObject = new Object();
      mObject.MarkName = "";
      mObject.PassWord = "";
      window.showModalDialog(mDialogUrl, mObject, "dialogHeight:200px; dialogWidth:360px;center:yes;scroll:no;status:no;");
  //判斷用戶是否選擇印章名稱和密碼
  if (mObject.MarkName==""||mObject.PassWord==null){
    StatusMsg("取消印章");
    return false;
  }else{
     //取得系統文件目錄
     var mFilePath=webform.WebOffice.WebMsgFilepath;
     var mImgShape=null;
     var mLabName=null;
     var mZOrder=null;
     mMarkName=mObject.MarkName;
     mPassWord=mObject.PassWord;
     //清除所有設置的變量
     webform.WebOffice.WebMsgTextClear();
     //設置COMMAND 爲 LOADMARKIMAGE
     webform.WebOffice.WebSetMsgByName("COMMAND","LOADMARKIMAGE");
     //設置印章名稱
     webform.WebOffice.WebSetMsgByName("IMAGENAME",mMarkName);
     //設置印章密碼
     webform.WebOffice.WebSetMsgByName("PASSWORD",mPassWord);
     //發送到服務器上
     webform.WebOffice.WebSendMessage();
     //如果沒有錯誤
     if (webform.WebOffice.Error==""){
       //取得服務器返回時間
       mMarkTime=webform.WebOffice.WebGetMsgByName("DATETIME");
       //取得印章所蓋的位置,POSITION所指的是標籤的對象名
       mLabName=webform.WebOffice.WebGetMsgByName("POSITION");
       //設置印章的顯示形式,4是在文字上方,5是在文字下方
       mZOrder=webform.WebOffice.WebGetMsgByName("ZORDER");
       //取得完整的印章圖片名稱
       var mFileName=webform.WebOffice.WebGetMsgByName("IMAGENAME");
       //保存該印章
       webform.WebOffice.WebMsgFileSave(mFilePath+mFileName);
       //如果一切正常
       if (webform.WebOffice.Error==""){
         //如果是word文件
         if (webform.WebOffice.FileType==".doc"){
           var mInlineShapes=null; //=webform.WebOffice.ActiveDocument.InlineShapes.Count;
           //var mShapes=webform.WebOffice.ActiveDocument.Shapes.Count;
           var mBookMark=WebFindBookmarks(mLabName);
           if (mBookMark){
              mInlineShapes=mBookMark.InlineShapes.AddPicture(mFilePath+mFileName);
           }else{
              mInlineShapes=webform.WebOffice.ActiveDocument.Application.Selection.InlineShapes.AddPicture(mFilePath+mFileName);
           } 

           //if (mInlineShapes!=webform.WebOffice.ActiveDocument.InlineShapes.Count){
           //   mImgShape = webform.WebOffice.ActiveDocument.InlineShapes.Item(webform.WebOffice.ActiveDocument.InlineShapes.Count).ConvertToShape();
           //}
           //if (mShapes!=webform.WebOffice.ActiveDocument.Shapes.Count){
           //   mImgShape = webform.WebOffice.ActiveDocument.Shapes.Item(webform.WebOffice.ActiveDocument.Shapes.Count);
           //}
          
          mImgShape = mInlineShapes.ConvertToShape();
          mImgShape.Select();
          mImgShape.AlternativeText="DBSTEP_2004=印章名稱:["+mMarkName+"] 用戶名稱:["+webform.WebOffice.UserName+"] 蓋章時間[:"+mMarkTime+"] 印章編號:["+webform.WebOffice.RecordID+"]";
          mImgShape.PictureFormat.TransparentBackground = true;
          mImgShape.PictureFormat.TransparencyColor = 16777215;
          mImgShape.Fill.Visible = false;
          mImgShape.WrapFormat.Type = 3;
          mImgShape.ZOrder(mZOrder);
        }
        //如果是Excel文件
        if (webform.WebOffice.FileType==".xls"){
     webform.WebOffice.ActiveDocument.ActiveSheet.Pictures.Insert(mFilePath+mFileName).Select();
   mImgShape =webform.WebOffice.ActiveDocument.Application.Selection.ShapeRange;
          mImgShape.AlternativeText="DBSTEP_2004=印章名稱:["+mMarkName+"] 用戶名稱:["+webform.WebOffice.UserName+"] 蓋章時間[:"+mMarkTime+"] 印章編號:["+webform.WebOffice.RecordID+"]";
          mImgShape.PictureFormat.TransparentBackground = true;
          mImgShape.PictureFormat.TransparencyColor = 16777215;
          mImgShape.Fill.Visible = false;
          mImgShape.ZOrder(mZOrder);
        }
       } 
       //清除印章文件
       webform.WebOffice.WebMsgFileClear(mFilePath+mFileName);
    }
    StatusMsg(webform.WebOffice.Error);
  }
}

//顯示印章信息
function WebShowSignature(){
  var SignatureString="";
  try{
    //如果是word文件
    if (webform.WebOffice.FileType==".doc"){
      for (var mIndex=1;mIndex<=webform.WebOffice.ActiveDocument.Shapes.Count;mIndex++){
        var mValidString=webform.WebOffice.ActiveDocument.Shapes.Item(mIndex).AlternativeText;
        if ((mValidString!=null)&&(mValidString.length!=0)){
            if ((mValidString.indexOf("DBSTEP_2004")!=-1) && (mValidString.indexOf(webform.WebOffice.RecordID)!=-1)){
  SignatureString=SignatureString+"/r/n"+mValidString.substring(12,mValidString.length);
     }
        }
      }
    }
    //如果是Excel文件
    if (webform.WebOffice.FileType==".xls"){
      for (var mSheet=1;mSheet<=webform.WebOffice.ActiveDocument.Application.Sheets.Count;mSheet++){
        webform.WebOffice.ActiveDocument.Application.Sheets(mSheet).Select();
        for (var mIndex=1;mIndex<=webform.WebOffice.ActiveDocument.ActiveSheet.Shapes.Count;mIndex++){
   var mValidString=webform.WebOffice.ActiveDocument.ActiveSheet.Shapes.Item(mIndex).AlternativeText;
          if ((mValidString!=null)&&(mValidString.length!=0)){
              if ((mValidString.indexOf("DBSTEP_2004")!=-1) && (mValidString.indexOf(webform.WebOffice.RecordID)!=-1)){
    SignatureString=SignatureString+"/r/n"+mValidString.substring(12,mValidString.length);
       }
          }
        }
      }
    }

    if (SignatureString){
       alert("印章列表如下:"+SignatureString);
    }else{
       alert("沒有找到任何有效印章");
    }
  }catch(e){}
}


//是否顯示工具欄
function WebToolBars(Visible){
  try{
     webform.WebOffice.ToolBars=Visible;
  }catch(e){}
}
//是否顯示菜單欄
function WebMenuBar(Visible){
  try{
     webform.WebOffice.MenuBar=Visible;
  }catch(e){}
}

//是否顯示某種工具欄
function WebToolsVisible(ToolName,Visible){
  try{
    webform.WebOffice.ActiveDocument.CommandBars(ToolName).Visible = Visible;
  }catch(e){}
}

//是否允許某工具欄上的某工具有效
function WebToolsEnable(ToolName,ToolIndex,Enable){
 var i;
 try{
    for (i=1;i<=webform.WebOffice.ActiveDocument.CommandBars(ToolName).Controls.Count;i++){
 if  (webform.WebOffice.ActiveDocument.CommandBars(ToolName).Controls(i).id==ToolIndex){
      webform.WebOffice.ActiveDocument.CommandBars(ToolName).Controls(i).Enabled = Enable;
  break;
 }
    }
 }catch(e){}
}

//設置是否保護
function WebProtect(value){
  try{
    if (value){
 if (webform.WebOffice.FileType==".doc"){ 
       webform.WebOffice.ActiveDocument.Protect(2);
 }else if(webform.WebOffice.FileType=='.xls'){
            for (var mIndex=1;mIndex<=webform.WebOffice.ActiveDocument.Application.ActiveWorkbook.Sheets.Count;mIndex++){
                var mSheet=webform.WebOffice.ActiveDocument.Application.ActiveWorkbook.Sheets(mIndex);
                mSheet.Protect("", true, true, true);
            }
 }
    }else{
 if (webform.WebOffice.FileType==".doc"){ 
  webform.WebOffice.ActiveDocument.UnProtect();
 }else if(webform.WebOffice.FileType=='.xls'){
            for (var mIndex=1;mIndex<=webform.WebOffice.ActiveDocument.Application.ActiveWorkbook.Sheets.Count;mIndex++){
                var mSheet=webform.WebOffice.ActiveDocument.Application.ActiveWorkbook.Sheets(mIndex);
                mSheet.UnProtect("");
            }
 }

    }
  }catch(e){}
}

//取得Word內容
function WebGetWordContent(){
  try{
    alert(webform.WebOffice.ActiveDocument.Content.Text);
  }catch(e){}
}

//插入Word內容
function WebSetWordContent(){
  var mText=window.prompt("請輸入內容:","測試內容");
  if (mText==null){
     return (false);
  }
  else
  {
     //下面爲顯示選中的文本
     //alert(webform.WebOffice.ActiveDocument.Application.Selection.Range.Text);
     //下面爲在當前光標出插入文本
     webform.WebOffice.ActiveDocument.Application.Selection.Range.InsertAfter (mText+"/n");
     //下面爲在第一段後插入文本
     //webform.WebOffice.ActiveDocument.Application.ActiveDocument.Range(1).InsertAfter(mText);
  }
}

//設置並取得Excel的內容
function WebGetExcelContent(){
    webform.WebOffice.ActiveDocument.Application.Sheets(1).Select;
    webform.WebOffice.ActiveDocument.Application.Range("C5").Select;
    webform.WebOffice.ActiveDocument.Application.ActiveCell.FormulaR1C1 = "126";
    webform.WebOffice.ActiveDocument.Application.Range("C6").Select;
    webform.WebOffice.ActiveDocument.Application.ActiveCell.FormulaR1C1 = "446";
    webform.WebOffice.ActiveDocument.Application.Range("C7").Select;
    webform.WebOffice.ActiveDocument.Application.ActiveCell.FormulaR1C1 = "556";
    webform.WebOffice.ActiveDocument.Application.Range("C5:C8").Select;
    webform.WebOffice.ActiveDocument.Application.Range("C8").Activate;
    webform.WebOffice.ActiveDocument.Application.ActiveCell.FormulaR1C1 = "=SUM(R[-3]C:R[-1]C)";
    webform.WebOffice.ActiveDocument.Application.Range("D8").Select;
    alert(webform.WebOffice.ActiveDocument.Application.Range("C8").Text);
}

//設置Excel頁,並演示鎖定相應欄目
function WebSheetsLock(){
    webform.WebOffice.ActiveDocument.Application.Sheets(1).Select;

    webform.WebOffice.ActiveDocument.Application.Range("A1").Select;
    webform.WebOffice.ActiveDocument.Application.ActiveCell.FormulaR1C1 = "產品";
    webform.WebOffice.ActiveDocument.Application.Range("B1").Select;
    webform.WebOffice.ActiveDocument.Application.ActiveCell.FormulaR1C1 = "價格";
    webform.WebOffice.ActiveDocument.Application.Range("C1").Select;
    webform.WebOffice.ActiveDocument.Application.ActiveCell.FormulaR1C1 = "詳細說明";
    webform.WebOffice.ActiveDocument.Application.Range("D1").Select;
    webform.WebOffice.ActiveDocument.Application.ActiveCell.FormulaR1C1 = "庫存";
    webform.WebOffice.ActiveDocument.Application.Range("A2").Select;
    webform.WebOffice.ActiveDocument.Application.ActiveCell.FormulaR1C1 = "書籤";
    webform.WebOffice.ActiveDocument.Application.Range("A3").Select;
    webform.WebOffice.ActiveDocument.Application.ActiveCell.FormulaR1C1 = "毛筆";
    webform.WebOffice.ActiveDocument.Application.Range("A4").Select;
    webform.WebOffice.ActiveDocument.Application.ActiveCell.FormulaR1C1 = "鋼筆";
    webform.WebOffice.ActiveDocument.Application.Range("A5").Select;
    webform.WebOffice.ActiveDocument.Application.ActiveCell.FormulaR1C1 = "尺子";

    webform.WebOffice.ActiveDocument.Application.Range("B2").Select;
    webform.WebOffice.ActiveDocument.Application.ActiveCell.FormulaR1C1 = "0.5";
    webform.WebOffice.ActiveDocument.Application.Range("C2").Select;
    webform.WebOffice.ActiveDocument.Application.ActiveCell.FormulaR1C1 = "櫻花";
    webform.WebOffice.ActiveDocument.Application.Range("D2").Select;
    webform.WebOffice.ActiveDocument.Application.ActiveCell.FormulaR1C1 = "300";

    webform.WebOffice.ActiveDocument.Application.Range("B3").Select;
    webform.WebOffice.ActiveDocument.Application.ActiveCell.FormulaR1C1 = "2";
    webform.WebOffice.ActiveDocument.Application.Range("C3").Select;
    webform.WebOffice.ActiveDocument.Application.ActiveCell.FormulaR1C1 = "狼毫";
    webform.WebOffice.ActiveDocument.Application.Range("D3").Select;
    webform.WebOffice.ActiveDocument.Application.ActiveCell.FormulaR1C1 = "50";

    webform.WebOffice.ActiveDocument.Application.Range("B4").Select;
    webform.WebOffice.ActiveDocument.Application.ActiveCell.FormulaR1C1 = "3";
    webform.WebOffice.ActiveDocument.Application.Range("C4").Select;
    webform.WebOffice.ActiveDocument.Application.ActiveCell.FormulaR1C1 = "藍色";
    webform.WebOffice.ActiveDocument.Application.Range("D4").Select;
    webform.WebOffice.ActiveDocument.Application.ActiveCell.FormulaR1C1 = "90";

    webform.WebOffice.ActiveDocument.Application.Range("B5").Select;
    webform.WebOffice.ActiveDocument.Application.ActiveCell.FormulaR1C1 = "1";
    webform.WebOffice.ActiveDocument.Application.Range("C5").Select;
    webform.WebOffice.ActiveDocument.Application.ActiveCell.FormulaR1C1 = "20cm";
    webform.WebOffice.ActiveDocument.Application.Range("D5").Select;
    webform.WebOffice.ActiveDocument.Application.ActiveCell.FormulaR1C1 = "40";

    //保護工作表
    webform.WebOffice.ActiveDocument.Application.Range("B2:D5").Select;
    webform.WebOffice.ActiveDocument.Application.Selection.Locked = false;
    webform.WebOffice.ActiveDocument.Application.Selection.FormulaHidden = false;
    webform.WebOffice.ActiveDocument.Application.ActiveSheet.Protect(true,true,true);  

    alert("已經保護工作表,只有B2-D5單元格可以修改。");
}

//VBA套紅操作
function WebInsertVBA(){
 
 //畫線
 var object=webform.WebOffice.ActiveDocument;
 var myl=object.Shapes.AddLine(100,60,305,60)
 myl.Line.ForeColor=255;
 myl.Line.Weight=2;
 var myl1=object.Shapes.AddLine(326,60,520,60)
 myl1.Line.ForeColor=255;
 myl1.Line.Weight=2;

 //object.Shapes.AddLine(200,200,450,200).Line.ForeColor=6;
    var myRange=webform.WebOffice.ActiveDocument.Range(0,0);
 myRange.Select();

 var mtext="★";
 webform.WebOffice.ActiveDocument.Application.Selection.Range.InsertAfter (mtext+"/n");
    var myRange=webform.WebOffice.ActiveDocument.Paragraphs(1).Range;
    myRange.ParagraphFormat.LineSpacingRule =1.5;
    myRange.font.ColorIndex=6;
    myRange.ParagraphFormat.Alignment=1;
    myRange=webform.WebOffice.ActiveDocument.Range(0,0);
 myRange.Select();
 mtext="金格發[2003]154號";
 webform.WebOffice.ActiveDocument.Application.Selection.Range.InsertAfter (mtext+"/n");
 myRange=webform.WebOffice.ActiveDocument.Paragraphs(1).Range;
 myRange.ParagraphFormat.LineSpacingRule =1.5;
 myRange.ParagraphFormat.Alignment=1;
 myRange.font.ColorIndex=1;
 
 mtext="金格電子政務文件";
 webform.WebOffice.ActiveDocument.Application.Selection.Range.InsertAfter (mtext+"/n");
 myRange=webform.WebOffice.ActiveDocument.Paragraphs(1).Range;
 myRange.ParagraphFormat.LineSpacingRule =1.5;
 
 //myRange.Select();
 myRange.Font.ColorIndex=6;
 myRange.Font.Name="仿宋_GB2312";
 myRange.font.Bold=true;
 myRange.Font.Size=50;
 myRange.ParagraphFormat.Alignment=1;
 
 //myRange=myRange=webform.WebOffice.ActiveDocument.Paragraphs(1).Range;
 webform.WebOffice.ActiveDocument.PageSetup.LeftMargin=70;
 webform.WebOffice.ActiveDocument.PageSetup.RightMargin=70;
 webform.WebOffice.ActiveDocument.PageSetup.TopMargin=70;
 webform.WebOffice.ActiveDocument.PageSetup.BottomMargin=70;
}

//保存定稿文件
function WebUpdateFile(){
  try{
    webform.WebOffice.WebUpdateFile();
    if (webform.WebOffice.Error!=""){
 alert(webform.WebOffice.Status+webform.WebOffice.Error);
    }
  }catch(e){}
  StatusMsg(webform.WebOffice.Status);
}

//從服務器上導入數據到文檔中
function WebInportText(){
    var mText;
    webform.WebOffice.WebSetMsgByName("COMMAND","INPORTTEXT");
    webform.WebOffice.WebSendMessage();
    if (webform.WebOffice.Error==""){
      mText=webform.WebOffice.WebGetMsgByName("CONTENT");
      webform.WebOffice.ActiveDocument.Application.Selection.Range.InsertAfter(mText);
      alert("導入文本成功");
    }
    StatusMsg(webform.WebOffice.Status);
}

//導出所有文檔文本給服務器
function WebExportText(){
    var mText=webform.WebOffice.ActiveDocument.Content.Text;
    webform.WebOffice.WebSetMsgByName("COMMAND","EXPORTTEXT");
    webform.WebOffice.WebSetMsgByName("CONTENT",mText);
    webform.WebOffice.WebSendMessage();
    if (webform.WebOffice.Error==""){
      alert("導出文本成功");
    }
    StatusMsg(webform.WebOffice.Status);
}

//根據標籤名稱查找標籤
function WebFindBookmarks(mPosition)
{
  var mBookObject,mBookName,mBookIdx;
  if (webform.WebOffice.FileType==".doc"){
         for (mBookIdx = 1; mBookIdx<=webform.WebOffice.ActiveDocument.Bookmarks.Count;mBookIdx++){
             mBookName = webform.WebOffice.ActiveDocument.Bookmarks.Item(mBookIdx).Name;
             if (mBookName==mPosition) {
                 mBookObject = webform.WebOffice.ActiveDocument.Bookmarks.Item(mBookIdx).Range;
                 break;
             }
         }
  }
  return (mBookObject);
}

//插入服務器圖片
function WebInsertImage(vLabName,vImgName,vTrans,vZOrder)
{
    //取得文件目錄
    var mFilePath=webform.WebOffice.WebMsgFilepath;
    var mImgShape=null;
    var mPassword=null;
    //清除所有設置變量
    webform.WebOffice.WebMsgTextClear();
    //設置COMMAND 爲 INSERTIMAGE 命令
    webform.WebOffice.WebSetMsgByName("COMMAND","INSERTIMAGE");
    //設置要插入的圖片名稱
    webform.WebOffice.WebSetMsgByName("IMAGENAME",vImgName);
    //發送到服務器上
    webform.WebOffice.WebSendMessage();
    //如果沒有錯誤
    if (webform.WebOffice.Error==""){
       //將服務器返回的圖片保存
       webform.WebOffice.WebMsgFileSave(mFilePath+vImgName);
       //如果沒有錯誤
       if (webform.WebOffice.Error==""){
         //如果是word文檔
         if (webform.WebOffice.FileType==".doc"){
           var mInlineShapes=null;  //=webform.WebOffice.ActiveDocument.InlineShapes.Count;
           //var mShapes=webform.WebOffice.ActiveDocument.Shapes.Count;
           var mBookMark=WebFindBookmarks(vLabName);
           if (mBookMark){
              mInlineShares=mBookMark.InlineShapes.AddPicture(mFilePath+vImgName);
           }else{
              mInlineShares=webform.WebOffice.ActiveDocument.Application.Selection.InlineShapes.AddPicture(mFilePath+vImgName);
           } 
           //if (mInlineShapes!=webform.WebOffice.ActiveDocument.InlineShapes.Count){
           //   mImgShape = webform.WebOffice.ActiveDocument.InlineShapes.Item(webform.WebOffice.ActiveDocument.InlineShapes.Count).ConvertToShape();
           //}
           //if (mShapes!=webform.WebOffice.ActiveDocument.Shapes.Count){
           //   mImgShape = webform.WebOffice.ActiveDocument.Shapes.Item(webform.WebOffice.ActiveDocument.Shapes.Count);
           //}
          mImgShape=mInlineShares.ConvertToShape();
          mImgShape.Select();
          mImgShape.PictureFormat.TransparentBackground = vTrans;
          mImgShape.PictureFormat.TransparencyColor = 16777215;
          mImgShape.Fill.Visible = false;
          mImgShape.WrapFormat.Type = 3;
          mImgShape.ZOrder(vZOrder);
        }
        //如果是Excel文檔
        if (webform.WebOffice.FileType==".xls"){
     webform.WebOffice.ActiveDocument.ActiveSheet.Pictures.Insert(mFilePath+vImgName).Select();
   mImgShape =webform.WebOffice.ActiveDocument.Application.Selection.ShapeRange;
          mImgShape.PictureFormat.TransparentBackground = vTrans;
          mImgShape.PictureFormat.TransparencyColor = 16777215;
          mImgShape.Fill.Visible = false;
          mImgShape.ZOrder(vZOrder);
        }
      } 
      //清除文件
      webform.WebOffice.WebMsgFileClear(mFilePath+vImgName);
    }
    StatusMsg(webform.WebOffice.Status+webform.WebOffice.Error);
}

//套用模版定稿
function WebInsertFile()
{
  //var mDialogUrl = "Version/VersionList.asp?RecordID="+webform.WebOffice.RecordID;
  //var mObject = new Object();
  //    mObject.FileID = "";
  //    mObject.Result = false;
  //    window.showModalDialog(mDialogUrl, mObject, "dialogHeight:280px; dialogWidth:420px;center:yes;scroll:no;status:no;");
  //if (mObject.Result){
     //取得目錄名稱
     var mFilePath=webform.WebOffice.WebMsgFilepath;
     //取得文件類型
     var mFileName=webform.WebOffice.RecordID+webform.WebOffice.FileType;
     var mBookRange;
     //保存當前文件
     webform.WebOffice.Save(mFilePath+mFileName,true);
     //如果沒有問題
     if (webform.WebOffice.Error==""){
       //清除所有設置變量
       webform.WebOffice.WebMsgTextClear();
       //設置COMMAND爲INSERTFILE
       webform.WebOffice.WebSetMsgByName("COMMAND","INSERTFILE");
       //設置需要調用的模版名稱
       webform.WebOffice.WebSetMsgByName("TEMPNAME","模板一.doc");
       //發送該信息到服務器上
       webform.WebOffice.WebSendMessage();
       //如果沒有錯誤
       if (webform.WebOffice.Error==""){
         //關閉當前文件
         webform.WebOffice.Close();
         //保存模版文件
         webform.WebOffice.WebMsgFileSave(gFileName);
         //如果沒有錯誤
         if (webform.WebOffice.Error==""){
            //打開模版文件
            webform.WebOffice.Open(gFileName,true);
            //查找需要插入的位置(標籤)
            var mPosition=webform.WebOffice.WebGetMsgByName('POSITION');
            if (mPosition.length>0){
               mBookRange = WebFindBookmarks(mPosition);
            }else{
               mBookRange = webform.WebOffice.ActiveDocument.Range.Application.Selection;
            }
            //將原工作文檔插入到該位置
            mBookRange.InsertFile(mFilePath+mFileName);
         } 
       }else{
         alert(webform.WebOffice.Error);
       }
     }
     //清楚文件
     webform.WebOffice.WebMsgFileClear(mFilePath+mFileName);
     StatusMsg(webform.WebOffice.Status)
}

//作用:表格生成及填充
function WebSetWordTable(){
  var mText="",mName="",iColumns,iCells,iTable;
  //清除所有設置變量
  webform.WebOffice.WebMsgTextClear();
  //設置COMMAND爲WORDTABLE
  webform.WebOffice.WebSetMsgByName("COMMAND","WORDTABLE");   //OfficeServer.asp  COMMAND=WORDTABLE
  //發送到服務器上
  webform.WebOffice.WebSendMessage();         //OfficeServer.asp  OPTION=SENDMESSAGE
  //如果沒有錯誤
  if (webform.WebOffice.Error==""){                      
 //取得列
 iColumns = webform.WebOffice.WebGetMsgByName("COLUMNS");
 //取得行
 iCells = webform.WebOffice.WebGetMsgByName("CELLS");     
 //生成表格
 iTable=webform.WebOffice.ActiveDocument.Tables.Add(webform.WebOffice.ActiveDocument.Application.Selection.Range,iCells,iColumns);  
 for (var i=1; i<=iColumns; i++)
 {
      for (var j=1; j<=iCells; j++)
   {
  mName=i.toString()+j.toString();  
  //取得表格內容 
  mText=webform.WebOffice.WebGetMsgByName(mName); 
  //填充單元值
  iTable.Columns(i).Cells(j).Range.Text=mText;    
    }
 }
   }
   StatusMsg(webform.WebOffice.Status);
}

</script>
</head>
<body bgcolor="#ffffff" οnlοad="Load()" οnunlοad="UnLoad()">

<form name="webform" method="post" action="DocumentSave.asp" οnsubmit="return SaveDocument();">
<input type=hidden name=RecordID value="<%=mRecordID%>">
<input type=hidden name=Template value="<%=mTemplate%>">
<input type=hidden name=FileType value="<%=mFileType%>">
<input type=hidden name=EditType value="<%=mEditType%>">
<input type=hidden name=HTMLPath value="<%=mHTMLPath%>">

<table border=0  cellspacing='0' cellpadding='0' width=100% height=100% align=center class=TBStyle>
<tr>
  <td align=right class="TDTitleStyle" width=64>主題</td>
  <td class="TDStyle"><input type=text name=Subject value="<%=mSubject%>" class="IptStyle" οnblur="SetBookmarks('Caption',this.value);" ></td>
</tr>

<tr>
  <td align=right class="TDTitleStyle" width=64>作者</td>
  <td class="TDStyle"><input type=text name=Author value="<%=mAuthor%>" class="IptStyle" οnblur="SetBookmarks('Author',this.value);" ></td>
</tr>

<tr>
  <td align=right class="TDTitleStyle" width=64>時間</td>
  <td class="TDStyle"><input type=text name=FileDate value="<%=mFileDate%>" readonly class="IptStyle" ></td>
</tr>
<tr>
  <!--td align=right valign=top  class="TDTitleStyle" width=64>內容</td-->
  <td align=right valign=top  class="TDTitleStyle" width=64 hegith=90% >
  <br>功能列表<br>
                 <input type=button class=button value="隱藏痕跡" <%=mDisabled%> <%=mWord%> οnclick="ShowRevision(false)">
                 <input type=button class=button value="顯示痕跡" <%=mDisabled%> <%=mWord%> οnclick="ShowRevision(true)">
                 <input type=button class=button value="清除痕跡" <%=mDisabled%> <%=mWord%> οnclick="webform.WebOffice.ActiveDocument.AcceptAllRevisions();">
                 <input type=button class=button value="保護文檔" <%=mDisabled%> οnclick="WebProtect(true)">
                 <input type=button class=button value="解除保護" <%=mDisabled%> οnclick="WebProtect(false)">
                 <input type=button class=button value="頁面設置" <%=mDisabled%> οnclick="WebOpenPageSetup()">
                 <input type=button class=button value="打印文檔" <%=mDisabled%> οnclick="WebOpenPrint()">
                 <input type=button class=button value="插入圖片" <%=mDisabled%> οnclick="WebOpenPicture()">
    版本控制
                 <input type=button class=button value="保存版本" <%=mDisabled%> οnclick="WebSaveVersion()">
                 <input type=button class=button value="打開版本" <%=mDisabled%> οnclick="WebOpenVersion()">
    服務器交互
                 <input type=button class=button value="簽名印章" <%=mDisabled%> οnclick="WebOpenSignature()">
                 <input type=button class=button value="驗證簽章" <%=mDisabled%> οnclick="WebShowSignature()">
                 <input type=button class=button value="遠程圖片" <%=mDisabled%> οnclick="WebInsertImage('Image','樣品.jpg',true,4);">
                 <input type=button class=button value="填充模版" <%=mDisabled%> <%=mWord%> οnclick="LoadBookmarks()">
                 <input type=button class=button value="表格填充" <%=mDisabled%> <%=mWord%> οnclick="WebSetWordTable()">

    VBA調用
                 <input type=button class=button value="取Word內容" <%=mDisabled%> <%=mWord%>  οnclick="WebGetWordContent()">
                 <input type=button class=button value="寫Word內容" <%=mDisabled%> <%=mWord%> οnclick="WebSetWordContent()">
                 <input type=button class=button value="用Excel求和" <%=mDisabled%> <%=mExcel%> οnclick="WebGetExcelContent()">
                 <input type=button class=button value="Excel保護工作表" <%=mDisabled%> <%=mExcel%> οnclick="WebSheetsLock()">
      工具欄
                 <input type=button class=button value="關閉菜單" <%=mDisabled%>  οnclick="WebMenuBar(false)">
                 <input type=button class=button value="顯示菜單" <%=mDisabled%>  οnclick="WebMenuBar(true)">
                 <input type=button class=button value="關閉工具欄" <%=mDisabled%>  οnclick="WebToolBars(false)">
                 <input type=button class=button value="顯示工具欄" <%=mDisabled%>  οnclick="WebToolBars(true)">
                 <input type=button class=button value="關閉常用工具" <%=mDisabled%>  οnclick="WebToolsVisible('Standard',false)">
                 <input type=button class=button value="打開常用工具" <%=mDisabled%>  οnclick="WebToolsVisible('Standard',true)">
                 <input type=button class=button value="關閉格式工具" <%=mDisabled%>  οnclick="WebToolsVisible('Formatting',false)">
                 <input type=button class=button value="打開格式工具" <%=mDisabled%>  οnclick="WebToolsVisible('Formatting',true)">
                 <input type=button class=button value="關閉審閱按鈕" <%=mDisabled%>  οnclick="WebToolsVisible('Reviewing',false)">
                 <input type=button class=button value="打開審閱按鈕" <%=mDisabled%>  οnclick="WebToolsVisible('Reviewing',true)">
                 <input type=button class=button value="關閉打印按鈕" <%=mDisabled%>  οnclick="WebToolsEnable('Standard',2521,false);">
                 <input type=button class=button value="打開打印按鈕" <%=mDisabled%>  οnclick="WebToolsEnable('Standard',2521,true);">
    其他調用
                 <input type=button class=button value="VBA套紅定稿" <%=mDisabled%> <%=mWord%> οnclick="WebInsertVBA();">
                 <input type=button class=button value="套用模版定稿" <%=mDisabled%> οnclick="WebInsertFile()">
                 <input type=button class=button value="保存定稿文件" <%=mDisabled%> οnclick="WebUpdateFile()">
                 <input type=button class=button value="導入Text" <%=mDisabled%> <%=mWord%> οnclick="WebInportText();">
                 <input type=button class=button value="導出Text" <%=mDisabled%> <%=mWord%> οnclick="WebExportText();">

  </td>

  <td class="TDStyle" height=90% >
        <table border=0 cellspacing='0' cellpadding='0' width='100%' height='100%' >
        <tr>
          <td bgcolor=menu >
<OBJECT id="WebOffice" width="100%" height="100%" classid="clsid:23739A7E-2004-4D1C-88D5-D50B18F7C347" codebase="<%=mClientUrl%>">
         <param name="BorderStyle" value="1">
         <param name="Menubar" value="1">
         <param name="Toolbars" value="1">
</OBJECT>
          </td>
        </tr>
        <tr>
          <td bgcolor=menu height='20'>
  <div id=StatusBar>狀態欄</div>
          </td>
        </tr>
        </table>
  </td>
</tr>
</table>

<input type=submit value="  保存  " <%=mDisabled%> >
<input type=button value="  返回  " οnclick="history.back()"> 注意:只有選擇《保存》後,所做的操作纔有效!
</form>

</body>
</html>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章