2012-06-11 32 views
0

連接到數據源Word文檔我有一個在指定的位置在Word中打開一個文檔的功能:打開包含合併域和C#

static void OpenWordDocument(string fileName) 
{ 
Type applicationType = Type.GetTypeFromProgID("Word.Application"); 
object applicationObject = Activator.CreateInstance(applicationType); 

object documentsObject = applicationType.InvokeMember("Documents", System.Reflection.BindingFlags.GetProperty, null, applicationObject, null); 
applicationType.InvokeMember("Visible", System.Reflection.BindingFlags.SetProperty, null, applicationObject, new object[] { true }); 

Type documentsType = documentsObject.GetType(); 
object documentObject = documentsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, documentsObject, new Object[] { fileName }); 
} 

它正常工作與Office 2003,但在打開時該文檔在使用Office 2010的系統上,文檔似乎無法連接到數據源。 這可能是什麼原因造成的?當我打開可能阻塞連接的文檔時,是否缺少任何屬性?

回答

0

我們有一個有點變通辦法的〜

而不是去通過Interop組件和創建Word實例,我們剛剛創建跑由此拉開了文件.bat文件的方法解決它:X

static void OpenWordDocument() 
{ 
System.Diagnostics.Process proc = new System.Diagnostics.Process(); 
proc.EnableRaisingEvents = false; 
proc.StartInfo.FileName = @"fileName.bat"; 
proc.Start(); 
} 

這不是一個理想的解決方案,所以任何其他解決方案將是偉大的!