当前位置: 代码迷 >> Web前端 >> 操作 WebOffice.ocx 代码聚合
  详细解决方案

操作 WebOffice.ocx 代码聚合

热度:869   发布时间:2012-11-06 14:07:00.0
操作 WebOffice.ocx 代码集合

将其调用部分Office VBA代码整理如下:
来自 http://www.goldgrid.com
<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" onload="Load()" onunload="UnLoad()">
<form name="webform" method="post" action="DocumentSave.asp" onsubmit="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" onblur="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" onblur="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%> onclick="ShowRevision(false)">
                 <input type=button class=button value="显示痕迹" <%=mDisabled%> <%=mWord%> onclick="ShowRevision(true)">
                 <input type=button class=button value="清除痕迹" <%=mDisabled%> <%=mWord%> onclick="webform.WebOffice.ActiveDocument.AcceptAllRevisions();">
                 <input type=button class=button value="保护文档" <%=mDisabled%> onclick="WebProtect(true)">
                 <input type=button class=button value="解除保护" <%=mDisabled%> onclick="WebProtect(false)">
                 <input type=button class=button value="页面设置" <%=mDisabled%> onclick="WebOpenPageSetup()">
                 <input type=button class=button value="打印文档" <%=mDisabled%> onclick="WebOpenPrint()">
                 <input type=button class=button value="插入图片" <%=mDisabled%> onclick="WebOpenPicture()">
    版本控制
                 <input type=button class=button value="保存版本" <%=mDisabled%> onclick="WebSaveVersion()">
                 <input type=button class=button value="打开版本" <%=mDisabled%> onclick="WebOpenVersion()">
    服务器交互
                 <input type=button class=button value="签名印章" <%=mDisabled%> onclick="WebOpenSignature()">
                 <input type=button class=button value="验证签章" <%=mDisabled%> onclick="WebShowSignature()">
                 <input type=button class=button value="远程图片" <%=mDisabled%> onclick="WebInsertImage('Image','样品.jpg',true,4);">
                 <input type=button class=button value="填充模版" <%=mDisabled%> <%=mWord%> onclick="LoadBookmarks()">
                 <input type=button class=button value="表格填充" <%=mDisabled%> <%=mWord%> onclick="WebSetWordTable()">
    VBA调用
                 <input type=button class=button value="取Word内容" <%=mDisabled%> <%=mWord%>  onclick="WebGetWordContent()">
                 <input type=button class=button value="写Word内容" <%=mDisabled%> <%=mWord%> onclick="WebSetWordContent()">
                 <input type=button class=button value="用Excel求和" <%=mDisabled%> <%=mExcel%> onclick="WebGetExcelContent()">
                 <input type=button class=button value="Excel保护工作表" <%=mDisabled%> <%=mExcel%> onclick="WebSheetsLock()">
      工具栏
                 <input type=button class=button value="关闭菜单" <%=mDisabled%>  onclick="WebMenuBar(false)">
                 <input type=button class=button value="显示菜单" <%=mDisabled%>  onclick="WebMenuBar(true)">
                 <input type=button class=button value="关闭工具栏" <%=mDisabled%>  onclick="WebToolBars(false)">
                 <input type=button class=button value="显示工具栏" <%=mDisabled%>  onclick="WebToolBars(true)">
                 <input type=button class=button value="关闭常用工具" <%=mDisabled%>  onclick="WebToolsVisible('Standard',false)">
                 <input type=button class=button value="打开常用工具" <%=mDisabled%>  onclick="WebToolsVisible('Standard',true)">
                 <input type=button class=button value="关闭格式工具" <%=mDisabled%>  onclick="WebToolsVisible('Formatting',false)">
                 <input type=button class=button value="打开格式工具" <%=mDisabled%>  onclick="WebToolsVisible('Formatting',true)">
                 <input type=button class=button value="关闭审阅按钮" <%=mDisabled%>  onclick="WebToolsVisible('Reviewing',false)">
                 <input type=button class=button value="打开审阅按钮" <%=mDisabled%>  onclick="WebToolsVisible('Reviewing',true)">
                 <input type=button class=button value="关闭打印按钮" <%=mDisabled%>  onclick="WebToolsEnable('Standard',2521,false);">
                 <input type=button class=button value="打开打印按钮" <%=mDisabled%>  onclick="WebToolsEnable('Standard',2521,true);">
    其他调用
                 <input type=button class=button value="VBA套红定稿" <%=mDisabled%> <%=mWord%> onclick="WebInsertVBA();">
                 <input type=button class=button value="套用模版定稿" <%=mDisabled%> onclick="WebInsertFile()">
                 <input type=button class=button value="保存定稿文件" <%=mDisabled%> onclick="WebUpdateFile()">
                 <input type=button class=button value="导入Text" <%=mDisabled%> <%=mWord%> onclick="WebInportText();">
                 <input type=button class=button value="导出Text" <%=mDisabled%> <%=mWord%> onclick="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="  返回  " onclick="history.back()"> 注意:只有选择《保存》后,所做的操作才有效!
</form>
</body>
</html>
?
1 楼 01jiangwei01 2011-02-10  
你好,能不能给我发一份html前端,jsp,java服务器段保存代码。谢谢了。我的邮箱是01jiangwei01@163.com