현재 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.
'excException.ToString()'이 충분한 정보를 제공하지 않습니까? –