Link to home
Start Free TrialLog in
Avatar of radshaw_longer
radshaw_longer

asked on

In VB.Net how to check for to see if Word is already running and if so, open new document without opening new instance of Word

I'm very new to VB.Net but I'm asking the users to fill out a form i.e. name, address, etc. than pass that information into a word file placing the data from vb.net into word using bookmarks.  My program is running fine, but it keeps opening a new instance of Word everytime.  I would like to check to see if Word is open and then just open that document as well.  Here is my current code, any help would be greatly appreciated.  Thanks

If rbGiftReturn.Checked = True Then
            Dim BM As Word.Bookmark
            myWord = CreateObject("Word.application")
            With myWord
                .Documents.Open("C:\engagement letter\templates\gift tax return.doc")
                .ActiveDocument.Bookmarks.Item("bmSalutation").Range.Text = cbSalutation.Text
                .ActiveDocument.Bookmarks.Item("bmContact").Range.Text = txtContact.Text
                .ActiveDocument.Bookmarks.Item("bmCompany").Range.Text = txtCompany.Text
                .ActiveDocument.Bookmarks.Item("bmStreet").Range.Text = txtStreet.Text
                .ActiveDocument.Bookmarks.Item("bmCity").Range.Text = txtCity.Text
                .ActiveDocument.Bookmarks.Item("bmState").Range.Text = txtState.Text
                .ActiveDocument.Bookmarks.Item("bmZip").Range.Text = txtZIP.Text
                .ActiveDocument.Bookmarks.Item("bmContact1").Range.Text = txtContact.Text
                .ActiveDocument.Bookmarks.Item("bmMonth").Range.Text = cbYearEnd.Text
                .ActiveDocument.Bookmarks.Item("bmYear").Range.Text = cbTaxYear.Text
                .ActiveDocument.Bookmarks.Item("bmPreparer").Range.Text = cbPreparer.Text
                .ActiveDocument.Bookmarks.Item("bmEntities").Range.Text = clbEntities.Text
                .ActiveDocument.Bookmarks.Item("bmSalutation1").Range.Text = cbSalutation.Text
                .ActiveDocument.Bookmarks.Item("bmContact2").Range.Text = txtContact.Text
                .Visible = True
                .Activate()
            End With
            'myDoc = Nothing
            myWord = Nothing
            GC.Collect()
End If

Thanks again
ASKER CERTIFIED SOLUTION
Avatar of arif_eqbal
arif_eqbal

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial