Ambiguity in Word Interop code

IEnumerable picture IEnumerable · Oct 18, 2012 · Viewed 11k times · Source

I recently posted a question about reading Word files here.

The app runs fine however I get this Warning message;

Warning 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.

There seems to be some ambiguity from some using namespace and I would like to know how to resolve this. Although the app runs, I would like to minimize warning/errors.

I have provided the code below for the class; The line it refers to are these two lines

docs.Close(ref nullobject, ref nullobject, ref nullobject);
wordObject.Quit(ref nullobject, ref nullobject, ref nullobject);

The whole code:

namespace Wizard.Classes
{
    class MSWordReader
    {
        public void read(String filename)
        {
            String buffer = "";
            try
            {
                Microsoft.Office.Interop.Word.Application wordObject = new   
                Microsoft.Office.Interop.Word.Application();
                object file = filename; //this is the path
                object nullobject = Type.Missing;
                object visible = false;
                object readonlyp = true;
                object addtorecent = false; //add to words recent filelist

                Microsoft.Office.Interop.Word.Document docs = wordObject.Documents.Open
                    (ref file,ref nullobject, ref readonlyp, ref addtorecent,
                    ref nullobject, ref nullobject, ref nullobject, ref nullobject,
                    ref nullobject, ref nullobject, ref nullobject, ref visible,
                    ref nullobject, ref nullobject, ref nullobject, ref nullobject
                                    );
                docs.ActiveWindow.Selection.WholeStory();
                docs.ActiveWindow.Selection.Copy();
                IDataObject data = Clipboard.GetDataObject();
                buffer = data.GetData(DataFormats.Text).ToString();

                docs.Close(ref nullobject, ref nullobject, ref nullobject);

                wordObject.Quit(ref nullobject, ref nullobject, ref nullobject);
                MessageBox.Show(buffer);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}

Answer

Avi Shmidman picture Avi Shmidman · Oct 18, 2012

To resolve the ambiguity, use:

((Microsoft.Office.Interop.Word._Document)docs).Close(ref nullobject, ref nullobject, ref nullobject);    
((Microsoft.Office.Interop.Word._Application)wordObject).Quit(ref nullobject, ref nullobject, ref nullobject);