0

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.

tshepang
  • 12,111
  • 21
  • 91
  • 136
user3596433
  • 5
  • 1
  • 2
  • 1
    why do you do word.ActiveDocument.Close();? cant you just call document.Close(true);? Also you are not destroying your COM object, you should be calling Marshal.ReleaseComObject(Object) on document etc... – Mo Patel May 23 '14 at 09:45
  • Gives me the same error, also it can't find the reference in the finally part thingy. – user3596433 May 23 '14 at 09:47
  • There are some duplicates of this question if you search for the error message btw: http://stackoverflow.com/questions/8303969/how-to-eliminate-warning-about-ambiguity – Deruijter May 23 '14 at 10:20
  • Oh, Sorry. My bad. Googled it, but couldn't find the error ;/ – user3596433 May 23 '14 at 10:49

1 Answers1

0

The problem is that there is a method Close(), and an event Close.

See this thread. As the poster there states, you probably want to use the method Close() instead of the event. In this case try to cast word.ActiveDocument to _Document, and call Close() on that.

Edit:
You can also set the type to _Application, instead of Application:

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

(note that I'm unable to test this at the moment, I don't have office installed on my current machine)

Deruijter
  • 2,077
  • 16
  • 27