0

I have a class that creates a word document from an entity in my application. I'm using the NetOffice.Word package to interact with the document. My document is created perfectly, but another blank document also appears. Can anyone suggest what I might be doing wrong? Here is a cutdown version of the class that shows the problem:

Imports NetOffice.WordApi
Namespace Sales.CRM

    Friend Class CaseExporter

        Private Property App As Application
        Private Property Doc As Document
        '       Private Property Data As CaseData

        Public Sub New() 'data As CaseData)
            App = New Application
            Doc = App.Documents.Add
            Doc.Content.SetRange(0, 0)
            '           Me.Data = data
        End Sub

        Public Sub Export()
            EmitTitle()
            App.Visible = True
        End Sub

        Private Sub EmitTitle()
            Dim para = Doc.Content.Paragraphs.Add()
            para.Range.Style = Doc.Styles("Heading 1")
            para.Range.Text = "Some Text Here"
            para.Range.InsertParagraphAfter()
        End Sub
    End Class

End Namespace

So I'm creating a word application, adding a document to it, then adding a paragraph with some text to it. I then set the application object to visible to let the user see the document. My document becomes visible at this point, but so does another blank document. The application objects Documents.Count at this point is still returning 1

Kevin O'Donovan
  • 1,620
  • 1
  • 13
  • 24
  • Not sure what's going on, but you could always close all other documents with a for each loop before making the app visible... Does the same thing happen if you don't use NetOffice? – Jbjstam Feb 12 '17 at 14:51

1 Answers1

0

I was testing this scenario and it looks like the other document is a result of some issue during development.

You may be testing the app and and instance of Word may stay in background with existing document open. Next time you create a document and set Word visible, it will show the old and new document. Document count is just 1 because the old document exists in another instance of Word.

Another weird behavior I got when debugger killed Word and next time Word tried to recover a document. When it got visible, it has shown two documents.

Jozef Izso
  • 1,740
  • 15
  • 22