54

I have this warning:

Warning 3 Ambiguity between method 'Microsoft.Office.Interop.Word._Application.Quit(ref object, ref object, ref object)' and non-method 'Microsoft.Office.Interop.Word.ApplicationEvents4_Event.Quit'. Using method group.

on my line

wordApplication.Quit();

I have tried replacing it with:

wordApplication.Quit(false); // don't save changes

and

wordApplication.Quit(false, null, null); // no save, no format

but it keeps giving me this warning. It's not a huge problem because the code compiles perfectly and functions as expected, but I'd like to get rid of the warnings. What can I do?

user1002358
  • 2,852
  • 6
  • 22
  • 32
  • 1
    @HansPassant `Microsoft.Office.Interop.Word.Application` – user1002358 Nov 29 '11 at 01:03
  • 1
    +1 for persistent relevance. This still comes up after updates to the latest VS for me, in Nov 2013. Only 12 other people had this problem and found this entry? In 2 years? Come on folks, vote stuff up. – Shavais Nov 11 '13 at 17:43
  • 1
    Still relevant for VS2013. +1 –  Nov 03 '15 at 08:14

4 Answers4

71

Explicitly cast the reference to the type _Application:

((_Application)wordApplication).Quit(); 
phoog
  • 42,068
  • 6
  • 79
  • 117
  • There is still the same ambiguity when I do this. – user1002358 Nov 28 '11 at 23:55
  • Did you include the underscore at the beginning of "_Application"? I would expect that to happen if you left it off, since the "Application" interface inherits from both "_Application" and "ApplicationEvents4_Event". I'll look into it in some more detail tomorrow. – phoog Nov 29 '11 at 04:09
  • Weird. When I originally tried your suggestion, VS put a red underline under `_Application` and said it had no idea what it was. When I typed it again just now, so I could give you the exact error details, it worked fine. Now that it's accepted the cast, it all works fine. Thanks! :) – user1002358 Nov 29 '11 at 05:21
  • 2
    You can also declare your wordApplication variable to be of type _Application. – Aidan Ryan Aug 07 '12 at 19:19
  • 2
    This doesn't work for me, it tells me the type or namespace '_Application' could not be found. – Shavais Nov 11 '13 at 17:42
  • 1
    Oops, I'm aliasing the namespace since I use both Word and Excel in the same program. So in my case the cast needs to be to Word._Application. – Shavais Nov 11 '13 at 17:48
  • @Shavais good catch. This would be easy to overlook when using more than one office library. – phoog Dec 09 '13 at 21:25
  • 1
    Same applies for the document too, just for the further information. ((_Document)WordDoc).Close(true); ((_Application)WordApp).Quit(); – Alper Apr 20 '18 at 08:08
0

I used this

   object oMissing = System.Reflection.Missing.Value;
   ((Microsoft.Office.Interop.Word._Application)wordApp).Quit(ref oMissing, ref oMissing, ref oMissing);
               wordApp = null;
               GC.Collect();
               GC.WaitForPendingFinalizers();
user3397383
  • 222
  • 5
  • 10
0

I believe you need to define the type of the parameters to Quit. I use the following, which seems to work.

using Microsoft.Office.Interop.Word;
...
Application wordApplication = new Application();
...
    object paramMissing = Type.Missing;
    object saveOptionsObject = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
        wordApplication.Quit(ref saveOptionsObject, ref paramMissing, ref paramMissing);
        wordApplication = null;
Kim Crosser
  • 413
  • 8
  • 13
0

It's saying there are two quit methods in the included namespace you can if you like change quit to Microsoft.Office.Interop.Word._Application.Quit to remove the message or (haven't personally tried this) use a using statement.

smitec
  • 3,049
  • 1
  • 16
  • 12
  • 1
    No, it's not! It's saying there is a `Quit()` method and a `Quit` non-method. The non-method is an event. Also, you can't use a `using` in this case as it's only for namespaces and `IDisoposable`s. – user1002358 Nov 28 '11 at 23:58
  • 1
    sorry perhaps `two methods` was phrased poorly I mean to say there are two 'things' named quit in the namespace and it does not know how to tell them apart so it is going to guess. – smitec Nov 29 '11 at 00:01
  • 1
    I know what it's saying. What I don't know is how to remove the ambiguity :) – user1002358 Nov 29 '11 at 00:13