Anyone seen this odd Word Interop behavior?
-
Using C# and Word 16.0.
var wordApp = new Microsoft.Office.Interop.Word.Application(); wordApp.Visible = false; Document wordDoc1 = wordApp.Documents.Add(); Document wordDoc2 = wordApp.Documents.Open(@"c:\\users\\robertsf\\desktop\\doc2.docx");
When this code executes,
wordDoc1
gets clobbered, everything returning COM Exception, andwordApp.Documents
contains only one object, the opened document.var wordApp = new Microsoft.Office.Interop.Word.Application(); wordApp.Visible = false; Document wordDoc1 = wordApp.Documents.Add(); Document wordDoc1a = wordApp.Documents.Add(); Document wordDoc2 = wordApp.Documents.Open(@"c:\\users\\robertsf\\desktop\\doc2.docx");
When this code executes,
wordDoc1
gets clobbered again, butwordDoc1a
is fine, andwordApp.Documents
contains two objects.var wordApp = new Microsoft.Office.Interop.Word.Application(); wordApp.Visible = false; Document wordDoc2 = wordApp.Documents.Open(@"c:\\users\\robertsf\\desktop\\doc2.docx"); Document wordDoc1a = wordApp.Documents.Add();
When this code executes, however, everything is fine.
wordApp.Documents
contains two objects, and each object is accessible, as expected. I'm trying to programmatically create a new document and copy the text of a variable number of documents into it. I suppose I could open all the source documents before creating the destination document. Or I could try to create two instances ofWord.Application
, one for the source documents and one for the destination document, and then see if I can copy between the two instances. Here's the MS documation. Add: Documents.Add(Object, Object, Object, Object) Method (Microsoft.Office.Interop.Word) | Microsoft Learn[^] Open: Documents.Open Method (Microsoft.Office.Interop.Word) | Microsoft Learn[^< -
Using C# and Word 16.0.
var wordApp = new Microsoft.Office.Interop.Word.Application(); wordApp.Visible = false; Document wordDoc1 = wordApp.Documents.Add(); Document wordDoc2 = wordApp.Documents.Open(@"c:\\users\\robertsf\\desktop\\doc2.docx");
When this code executes,
wordDoc1
gets clobbered, everything returning COM Exception, andwordApp.Documents
contains only one object, the opened document.var wordApp = new Microsoft.Office.Interop.Word.Application(); wordApp.Visible = false; Document wordDoc1 = wordApp.Documents.Add(); Document wordDoc1a = wordApp.Documents.Add(); Document wordDoc2 = wordApp.Documents.Open(@"c:\\users\\robertsf\\desktop\\doc2.docx");
When this code executes,
wordDoc1
gets clobbered again, butwordDoc1a
is fine, andwordApp.Documents
contains two objects.var wordApp = new Microsoft.Office.Interop.Word.Application(); wordApp.Visible = false; Document wordDoc2 = wordApp.Documents.Open(@"c:\\users\\robertsf\\desktop\\doc2.docx"); Document wordDoc1a = wordApp.Documents.Add();
When this code executes, however, everything is fine.
wordApp.Documents
contains two objects, and each object is accessible, as expected. I'm trying to programmatically create a new document and copy the text of a variable number of documents into it. I suppose I could open all the source documents before creating the destination document. Or I could try to create two instances ofWord.Application
, one for the source documents and one for the destination document, and then see if I can copy between the two instances. Here's the MS documation. Add: Documents.Add(Object, Object, Object, Object) Method (Microsoft.Office.Interop.Word) | Microsoft Learn[^] Open: Documents.Open Method (Microsoft.Office.Interop.Word) | Microsoft Learn[^<I was going to delete this, but I'll leave it up in case someone else has the same problem. Here's the solution -- the call to
Documents.Open
has to have more arguments. The MS documentation says they're optional, but there are side-effects to not providing them.var wordApp = new Microsoft.Office.Interop.Word.Application();
wordApp.Visible = false;
Document wordDoc1 = wordApp.Documents.Add();
Document wordDoc1a = wordApp.Documents.Add();
Document wordDoc2 = wordApp.Documents.Open(@"c:\users\robertsf\desktop\doc2.docx",
false, false, false, "", "", false, "", "", WdOpenFormat.wdOpenFormatAuto,
MsoEncoding.msoEncodingUTF8, false, false, none, none, none);Now it works as expected. I haven't experimented to see exactly which argument caused the issue.