PDA

View Full Version : Solved: Multiple instances of Word starting



Wekkel
03-28-2009, 05:20 AM
Dear fellow VB users,

I started with Vistual Studio 2005 and am slowly learning the basics of Visual Basic. Recently, I upped the ante and invested a lot of hours in learning my way around in Visual Studio 2008.

I am working on a Windows Form Application that interacts with Microsoft Word. First idea was coding it with VSTO, but VSTO gives me nasty security issues (code won't run outside of Visual Studio debugging). Therefore, I switched to the Interop method with a Windows Form Application.

Basically, I want to insert text generated by means of my application into a Word document. In practice, I would be working on a Word document, start my application and have text generated and inserted into the Word document I was working on by a click of a button in my application.

Before completely writing the logic of my application, I am testing the proper method to achieve the behaviour of my application versus Word as described above. The following code may explain what I am trying:


Imports Microsoft.Office.Interop
Public Class Form1
Public wrdApp As Word.Application
Public wrdDoc As Word.Document

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Dim wrdApp As New Word.Application
Dim wrdDoc As New Word.Document
Try
wrdApp = GetObject(, "Word.Application")

Catch ex As Exception
End Try
'if not then create one
If (wrdApp Is Nothing) Then wrdApp = CreateObject("Word.Application")


If (wrdApp IsNot Nothing) Then

If wrdApp.Documents.Count = 0 Then
wrdApp.Documents.Add()
wrdApp.ActiveWindow.Selection.TypeText("Tekstje toevoegen")
ElseIf wrdApp.Documents.Count >= 1 Then

wrdApp.ActiveWindow.Selection.TypeText("Tekstje toevoegen")


End If
wrdApp.Visible = True
wrdApp.ActiveDocument.Activate()

wrdDoc = Nothing
wrdApp = Nothing



End If

End Sub
I used the following logic:

When clicking the button on my form:
1. test whether a Word instance is running: if not, start a Word instance
2. test whether a Document is running: if not, open a new document
3. insert text in active document

I want to use this logic to make it foolproof (e.g., if no Word instance is running, my code would still work without errors).

The good thing: it works. Repeated button clicks lead to the text being written over and over again in the (same) active document. Excellent, just what I need.

The bad thing: every time I click the button, a new Word instance starts running in the background.

Somehow, a new Word instance start every time I click the button to insert text, staying hidden only for the task manager to see (gobbling up valuable memory). I have some pictures of the problem I'm running into, but with zero (0) posts I cannot post a link yet :rotlaugh:

Question: how can I avoid having a new (hidden) Word instance opening when clicking the button, while still having the text written in the active document?

TonyJollans
03-28-2009, 10:13 AM
I suspect that the line:
wrdDoc = Nothing
.. is triggering implicit instantiation of a new object

Wekkel
03-29-2009, 02:21 AM
I commented out the code as indicated
wrdDoc = Nothing

That didn't solve my problem.

It's the 'New' part in my code that kept bothering me. It seemed logical that the reference to New kept new Word instances being started, each time I clicked the button.

Therefore, I shuffled some lines of code while removing the New parts. I ended up with the following code, which works OK.

Imports Microsoft.Office.Interop

Public Class Form1

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click

Dim wrdApp As Word.Application
Try
wrdApp = GetObject(, "Word.Application")

Catch ex As Exception
End Try
'if not then create one
If (wrdApp Is Nothing) Then wrdApp = CreateObject("Word.Application")


If (wrdApp IsNot Nothing) Then

If wrdApp.Documents.Count = 0 Then
wrdApp.Documents.Add()
wrdApp.ActiveWindow.Selection.TypeText("Tekstje toevoegen")
ElseIf wrdApp.Documents.Count >= 1 Then

wrdApp.ActiveWindow.Selection.TypeText("Tekstje toevoegen")


End If
wrdApp.Visible = True
wrdApp.ActiveDocument.Activate()


wrdApp = Nothing
End If

End Sub
It feels great to solve something after hours of digging :devil2:
Moving on to the (inescapable) following petty problem :rotlaugh: