Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Anyone seen this odd Word Interop behavior?

Anyone seen this odd Word Interop behavior?

Scheduled Pinned Locked Moved C#
csharpcomjsonquestion
2 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    RobertSF
    wrote on last edited by
    #1

    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.Documentscontains 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[^<

    R 1 Reply Last reply
    0
    • R RobertSF

      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.Documentscontains 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[^<

      R Offline
      R Offline
      RobertSF
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups