2011-12-16 3 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); 
    } 
} 

여기서 main 함수는 UpdateDocumentFieldsInFile()을 호출합니다. 코드를 단계별로 실행하면 파일이 열리고 업데이트되지만 프로그램이 종료되고 수동으로 파일을 다시 열면 필드가 업데이트되지 않습니다. 누구든지이 문제를 해결하는 방법에 대한 제안이 있습니까? TIA.

+0

'excException.ToString()'이 충분한 정보를 제공하지 않습니까? –

답변

0

의견을 보내 주셔서 다시 한번 감사드립니다. Dennis. 내가 문서를 다시 열지 않았다는 것을 확인한 후에도 나는 결정할 수 없었던 어떤 이유로 나를 위해 일하지 않을 것이다. 그래서 결국 자바 로봇을 사용하여 무엇을 할 수있는 프로그램을 만들게되었다. 내가 필요 :

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

관련 문제