So what I want to do is create a program that selects a map with .doc documents open that up save it as a docx then close word. I got it all but when I try to close Word it gives me an error.
Main code:
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();
}
}
}
}
Code that gets the .doc files:
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);
}
}
The error it gives me:
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.