欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

word更新目录域宏命令 博客分类: word vbscriptwordjava 

程序员文章站 2024-03-24 13:33:52
...

===============文档打开时执行=========================

Sub AutoOpen()
  Dim aStory As Range
  Dim aField As Field
     For Each aStory In ActiveDocument.StoryRanges
        For Each aField In aStory.Fields
           aField.Update
        Next aField
     Next aStory


End Sub

 

==============文档另存============================
Sub saveAsFile()
   Dim filePath As String

   Dim fileName As String

   Dim transFileName As String

   filePath = Application.ActiveDocument.Path

   transFileName = filePath+"\"+Left(ThisDocument.Name,Len(ThisDocument.Name)-4)+"docx"

   Application.DisplayAlerts=False

   ChDir filePath

           ActiveDocument.SaveAs2 fileName:=transFileName,FileFormat:= _

           wdFormatXMLDocument,LockCommnets:=False,Password:="",AddToRecentFiles _

           :=True, WritePassword:="",ReadOnlyRecommended:=False,EmbedTrueTypeFonts  _

          :=False,SaveNativePictureFormat:=False,SaveFormsData:=False, _ SaveAsAOCELetter _

          :=False,CompatibilityMode:=14
End Sub

===================java 调用vbscript==========================

public synchronized void handleVBS(String vbsPath,String wordPath){
          File vbsFile = new File(vbsPath);
          if(vbsFile.exists()){
                  vbsFile.delete();
          }
          try{
                vbsFile.createNewFile();
                Map<Integer,String> map = new HashMap<Integer,String>();
                map.put(0,"Set objWord = CreateObject(\"Word.Application\")");
                map.put(1," objWord.Visible = false");
                map.put(2,"Set objDocument = objWord.Documents.Open(\""
                                    +wordPath+"\")");
               map.put(3," objWord.ActiveDocument.Save");
               map.put(4," objWord.Application.Quit");
               map.put(5,"Set objWord = Nothing");
               String[] vbs = new String[6];
               for(int i =0;i<vbs.length;i++){
                    vbs[i]=map.get(i);
               }

               writeLine(vbsPath,vbs);
               String commond = null;
               String osName = System.getProperties.getProperty("os.name");
               if(os.startsWith("win") || os.startsWith("Win")){
                       commond = new String[]{"wscript",vbsPath};
               }else{
                        //linux WORD Document handle Application
               }
               Process process = Runtime.getRuntime().exec(commond);
               int val = process.waitFor();
               process.destroy();
               
          }catch(Exception exception){
                //
          }
}
private void writLine(String vbsPath,String[] vbs){
     OutPutStream fileWriter =null;
      try{
          fileWriter = new BufferedOutputStream(new FileOutputSream(vbsPath,true));
          for(int i =0;i <vbs.length;i++){
              fileWriter.write(vbs[i].getBytes("GBK"));
              fileWriter.write("\r\n".getBytes("GBK"));
          }
     }catch(Exception e){
           
     }finally{
          fileWriter.close();
     }
}