0

The Word VBA macro that I am converting to C# has the following line. I am very specifically using the Close() method on the Document object. I have not found any question that exactly answers my question below. There are similar questions, such as this one: Compile time warning when using 'Microsoft.Office.Interop.Word._Document.Close'

But looking at it I was not able to figure out the exact cast I have to use.

ActiveDocument.Close wdDoNotSaveChanges

I converted this to C# like this:

using Microsoft.Office;
using Microsoft.Office.Interop;
using Word = Microsoft.Office.Interop.Word;

Object oMissing = System.Reflection.Missing.Value;
Word.Document oWordDoc = new Word.Document();

oWordDoc.Close(Word.WdSaveOptions.wdDoNotSaveChanges, oMissing, oMissing);

However at the last line I get the following ambiguity error:

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.

Obviously I can't rename the Close method of Word (I guess), so I tried to cast my oWordDoc but nothing seems to work.

Can someone shed some light on it how to do this right? Thank you.

Community
  • 1
  • 1
ib11
  • 2,530
  • 3
  • 22
  • 55

1 Answers1

2

Cast it to Microsoft.Office.Interop.Word._Document, which does not contain that event.

((Microsoft.Office.Interop.Word._Document)oWordDoc).Close(Word.WdSaveOptions.wdDo‌​NotSaveChanges, oMissing, oMissing);
Nikhil Vartak
  • 5,002
  • 3
  • 26
  • 32
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • Yeah, I tried this: `(Microsoft.Office.Interop.Word._Document)oWordDoc.Close(Word.WdSaveOptions.wdDoNotSaveChanges, oMissing, oMissing);` But it gives a compiler error: **"Cannot convert type 'void' to 'Microsoft.Office.Interop.Word._Document'"** – ib11 May 03 '16 at 03:25
  • 1
    @nikhilvartak Please either leave a comment with a suggestion asking SLaks to include the change, or post it as your answer - You shouldn't be editing in solutions to other answers (even though you are trying to help, and it's likely correct!) – Rob May 03 '16 at 03:36
  • 2
    @ib11 You should be casting like this: `((Microsoft.Office.Interop.Word._Document)oWordDoc).Close(Word.WdSaveOptions.wdDo‌​NotSaveChanges, oMissing, oMissing);` @Rob thanks for correcting me. – Nikhil Vartak May 03 '16 at 03:39
  • @nikhilvartak -- Well, I don't say new to you if I say: "it works" Dah... Thanks a lot! – ib11 May 03 '16 at 03:41
  • It linked in automatically this related post, though it is not a duplicate, but I put it here so it is linked: http://stackoverflow.com/questions/8303969/how-to-eliminate-warning-about-ambiguity – ib11 May 03 '16 at 04:20