2011-12-16 63 views
1

目前我正在尝试构建一个工具,该工具将打开Microsoft Word文档文件并更新文档中的所有字段。这里是主代码:无法使用.NET更新Microsoft Word文档字段

using Microsoft.Office.Interop.Word; 

public class clsDocumentFieldUpdateTool 
{ 
    private static Microsoft.Office.Interop.Word.Application wordApp = null; 
    private static Microsoft.Office.Interop.Word.Document oDoc = null; 
    private static object missing = null; 
    private static object readOnly = false; 
    private static object visible = true; 

    public static void OpenDocument(string docFileNameWithPath) 
    { 
     wordApp = new Microsoft.Office.Interop.Word.Application(); 
     missing = System.Reflection.Missing.Value; 
     object fileToOpen = docFileNameWithPath; 
     try 
     { 
      oDoc = wordApp.Documents.Open(ref fileToOpen, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref      missing, ref visible, ref visible, ref missing, ref missing, ref missing); 
     } 
     catch (Exception excOpenFile) 
     { 
      MessageBox.Show(excOpenFile.Message + System.Reflection.MethodInfo.GetCurrentMethod().DeclaringType.FullName + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + " - " + excOpenFile.StackTrace); 
     } 
    } 

    private static void Update(string file) 
    { 
     object nothing = System.Reflection.Missing.Value; // our 'void' value 
     object filename = file; // our word template 
     object notTrue = false; // our boolean false 

     try 
     { 
      // 
      // now we want to load the template and check how many fields there are to replace 
      // 
      wordApp.Visible = true; 
      oDoc = wordApp.Documents.Add(// load the template into a document workspace 
               ref filename, // and reference it through our myWordDoc 
               ref missing, 
               ref missing, 
               ref missing); 
      dynamic properties = oDoc.BuiltInDocumentProperties; 
      // count how many fields we have to update 
      int fields = oDoc.Fields.Count; 

      foreach (Field myField in oDoc.Fields) 
      { 
       myField.Select(); 
       myField.Update(); 
      } 
      oDoc.Save(); 
      oDoc.Close(ref notTrue, ref missing, ref missing); 
      wordApp.Application.Quit( ref notTrue, 
             ref missing, 
             ref missing); 
     } 
     catch (Exception excException) 
     { 
      MessageBox.Show(excOpenFile.Message + System.Reflection.MethodInfo.GetCurrentMethod().DeclaringType.FullName + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + " - " + excException.StackTrace); 
     } 
    } 

    public static void UpdateDocumentFieldsInFile() 
    { 
     string strFile = @"L:\admin\11ZG-0401\11-SWDev\Testing Field Updates (from Document Properties).docx"; 
     OpenDocument(strFile); 
     Update(strFile); 
    } 
} 

其中主函数调用UpdateDocumentFieldsInFile()。当我单步执行代码时,它会打开文件并更新它,但在程序退出并手动重新打开文件后,这些字段尚未更新。有没有人有任何建议如何解决这个问题? TIA。

+0

不显示`excException.ToString()`给你足够的信息吗? – 2011-12-16 22:38:52

回答

0

再次感谢Dennis的反馈。在确定我没有再次打开文档后,由于某些原因,我仍然无法确定,所以最终我最终创建了一个使用Java Robot来执行操作的程序我需要:

import java.awt.*; 
import java.awt.event.*; 
import java.io.IOException; 

public class Robot06{ 
    static int keyInput[] = 
    { 
    KeyEvent.VK_F11, 
    KeyEvent.VK_F9 
    }; 

    public static void main(String[] args) throws AWTException,IOException 
    { 
    Runtime.getRuntime().exec("winword L:\\admin\\11ZG-0401\\11-SWDev\\\"Testing Field Updates (from Document Properties).docx\""); 
    Robot robot = new Robot(); 

    for (int cnt2 = 0; cnt2 < 10; cnt2++) 
    { 
     robot.keyPress(keyInput[0]); 
     robot.delay(500); 
     robot.keyPress(keyInput[1]); 
     robot.delay(500); 
    }  
    } 
} 
1

看起来好像您并未使用通过OpenDocument打开的文档。您的Update方法正在创建一个新文档(通过Documents.Add),将您的文件视为模板。这将创建一个新文档并对其进行编辑。所以你实际上不是在你的Update方法中操纵位于strFile的文件。

当您单步执行代码时,正在更新“Document1”的文档的名称是什么?这将确认您没有编辑文件“测试字段更新(来自文档属性).docx”,而是使用该文件作为模板添加的新文档。

编辑:如果是我,我会把OpenDocument变成一个函数并返回打开的文档。然后将该文档传递给Update方法,并且由于它已经打开,您不需要再打开或添加它。

+0

是的,你是对的。那么,如何修改代码以使其正常工作?我是否需要删除Documents.Add行? – Roger 2011-12-20 20:37:02

相关问题