所以我想要做的是創建一個程序,選擇一個地圖.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.
你爲什麼要做word.ActiveDocument.Close();?不能你打電話document.Close(true);?你也不是破壞你的COM對象,你應該在文檔等上調用Marshal.ReleaseComObject(Object)... –
給我同樣的錯誤,也找不到最後部分thingy中的引用。 – user3596433
這個問題有一些重複,如果你搜索錯誤消息btw:http://stackoverflow.com/questions/8303969/how-to-eliminate-warning-about-ambiguity – Deruijter