Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

new ord document out of VB6

Status
Not open for further replies.

Tapeworm

Programmer
Mar 6, 2001
8
DE
I know how to make a new document in word out of my VB App.

My Problem is that my code always starts Word new, despite it is open.
How can I determine wether word is already open or not (and only then open it new) and how can I add a new document to this already open Word ?


My Code:
Set wdApp = New Word.Application
wdApp.Caption = "MyName"
wdApp.ShowMe
Set newDoc = wdApp.Documents.Add
newdoc.activate
 
You can use the GetObject and CreateObject methods:
Code:
    On Error Resume Next
    Set wdApp = GetObject(, "Word.Application")
    If Err.Number <> 0 Then
        Err.Clear
        Set wdApp = CreateObject(&quot;Word.Application&quot;)
    End If
    wdApp.Visible = True
    wdApp.WindowState = wdWindowStateMaximize
    On Error GoTo ErrHndlr
Or, you can do a New Word.Application like you have.
Code:
    On Error Resume Next
    Set wdApp = GetObject(, &quot;Word.Application&quot;)
    If Err.Number <> 0 Then
        Err.Clear
        Set wdApp = New Word.Application
    End If
    wdApp.Visible = True
    wdApp.WindowState = wdWindowStateMaximize
    On Error GoTo ErrHndlr
If GetObject() returns an error, Word is not running, therefore, handle the error locally.

NOTE: I have run into problems if Outlook is running on the computer. Outlook starts an instance of winword.exe that runs in the background. It does not get real happy when you mess with that instance. If this is the case, I would recommend that you just create a new instance for your program.

Hope this helps! DimensionalSolutions@Core.com
While I welcome e-mail messages, please post all thread activity in these forums for the benefit of all members.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top