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, and wordApp.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, but wordDoc1a
is fine, and wordApp.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 of Word.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[^<