2014-05-23 26 views
0

所以我想要做的是創建一個程序,選擇一個地圖.doc文件打開,將它保存爲docx然後關閉單詞。我知道了,但是當我嘗試關閉Word時,它給了我一個錯誤。打開另存爲docx然後關閉word

主要代碼:

void FindWordFilesWithDoc(string SelectedDirection, string filter) 
    { 
     //get all files with the filter and add them to the list. 
     foreach (string d in Directory.GetDirectories(SelectedDirection)) 
     { 
      foreach (string f in Directory.GetFiles(SelectedDirection)) 
      { 
       DocFiles.Add(f); 
      } 
      //FindWordFilesWithDoc(d, filter); 
     } 
    } 

錯誤它給了我:那得到的.doc文件

public void ConvertAll(string docFilePathOriginal, string docFilePath, string outputDocxFilePath) 
    { 
     MessageBox.Show(docFilePathOriginal); 

     DocFiles = new List<string>(); 
     //calls the method that fills the list with the documents witht the filter. 
     FindWordFilesWithDoc(docFilePathOriginal, ".doc"); 
     //make a new word each time for max performance 
     Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application(); 

     foreach (string filename in DocFiles) 
     { 
      //exclude the .docx files, because the filter also accepts .docx files. 
      if (filename.ToLower().EndsWith(".doc")) 
      { 
       try 
       { 
       var srcFile = new FileInfo(filename); 

       var document = word.Documents.Open(srcFile.FullName); 
       string docxFilename = srcFile.FullName.Replace(".doc", ".docx"); 

       document.SaveAs2(FileName: docxFilename, FileFormat: WdSaveFormat.wdFormatXMLDocument); 
       } 
       finally 
       { 
        word.ActiveDocument.Close(); 
       } 
      } 
     } 
    } 

代碼

Ambiguity between method 'Microsoft.Office.Interop.Word._Document.Close(ref object, ref object, ref object)' and non-method 'Microsoft.Office.Interop.Word.DocumentEvents2_Event.Close'. Using method group.

+1

你爲什麼要做word.ActiveDocument.Close();?不能你打電話document.Close(true);?你也不是破壞你的COM對象,你應該在文檔等上調用Marshal.ReleaseComObject(Object)... –

+0

給我同樣的錯誤,也找不到最後部分thingy中的引用。 – user3596433

+0

這個問題有一些重複,如果你搜索錯誤消息btw:http://stackoverflow.com/questions/8303969/how-to-eliminate-warning-about-ambiguity – Deruijter

回答

0

的問題是,有一種方法關閉()和事件關閉。

參見this thread。正如海報所述,您可能想使用方法Close()而不是事件。在這種情況下,試着將word.ActiveDocument轉換爲_Document,然後調用Close()。

編輯:
您還可以設置類型_Application,而不是Application

Microsoft.Office.Interop.Word._Application word = new Microsoft.Office.Interop.Word.Application(); 

(請注意,我當時無法對此進行測試,我沒有安裝的Office我目前的機器)

+0

對不起,這個詞是我的新東西。你能輸入代碼嗎? ; $。 – user3596433

+0

我已經添加了一個額外的解決方案,有點整潔(雖然不幸的是未經測試)。 – Deruijter

+0

現在我正在休息,現在我已經爲這個錯誤掙扎了大約2個小時。我會在稍後回覆你。但是,謝謝!:) – user3596433

相關問題