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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Starting Microsoft Word from Visual Basic 1

Status
Not open for further replies.

henktenbos

IS-IT--Management
Feb 21, 2000
1
NL
Hi all,<br>
<br>
I have an example to start Word from my own Visual basic program. Unfortunately the program only runs when Word is not active. If there is already an instance of Word my VB app gives an error.<br>
Is there someone who has a working example of code to check if Word is active and if so, give a handle to the running instance I can use in my own program?<br>
<br>
thanks,<br>
Henk
 
There were good examples in threads called &quot;Unauthorized Applications&quot; (2/6/00 - posted 2/3) and &quot;Disallowing Multple Instances....&quot; (posted later). The answers aren't an exact solution but you should be easily able to bend the code to suit your exact needs.
 
You need to try the getobject function if this does not get a current instance of word you need to trap the error and start an instance of it yourself. Heres the VB:<br>
<br>
<FONT FACE=monospace><br>
Dim Words As Word.Application<br>
Dim istr As String<br>
Dim today As Date<br>
Dim ires, inday As Integer<br>
Dim docs As Word.Document<br>
<br>
Screen.MousePointer = 11<br>
<br>
Label1.Caption = &quot;Connecting to Database....&quot;<br>
Label1.Refresh<br>
<br>
<br>
'Call connect function which creates view for merge<br>
connect (inday)<br>
<br>
Label1.Caption = &quot;Merging Data...Please Wait&quot;<br>
<br>
On Error GoTo trap<br>
<br>
'Open an current instance of Word, if running<br>
Set Words = GetObject(, &quot;Word.Application&quot;)<br>
<br>
Words.Visible = True<br>
<br>
Word.Documents.Open FileName:=App.Path & &quot;\mmletter.doc&quot;, ConfirmConversions:=False, _<br>
ReadOnly:=False, AddToRecentFiles:=False, PasswordDocument:=&quot;&quot;, _<br>
PasswordTemplate:=&quot;&quot;, Revert:=False, WritePasswordDocument:=&quot;&quot;, _<br>
WritePasswordTemplate:=&quot;&quot;, Format:=wdOpenFormatAuto<br>
<br>
'Restore the Word if it was minimized.<br>
Words.WindowState = wdWindowStateMaximize<br>
<br>
'Begin the merge process!!!!<br>
<br>
<br>
<br>
MergeExit:<br>
<br>
Screen.MousePointer = 1<br>
Label1.Caption = &quot;Finished Merge!&quot;<br>
Label1.Refresh<br>
Set Words = Nothing 'Kill the word connection<br>
Exit Sub<br>
<br>
trap:<br>
'minimal error trapping<br>
If Err = 429 Then<br>
'If we get an error of 429 - no instance running, create an<br>
'instance and resume at the next line<br>
Set Words = GetObject(&quot;&quot;, &quot;Word.Application&quot;)<br>
Resume Next<br>
Else<br>
'Else, report error code and description<br>
ires = MsgBox(&quot;Error &quot; & Err & &quot; - &quot; & Error, vbCritical, &quot;Insular System&quot;)<br>
End If<br>
<br>
Resume MergeExit<br>
<br>
<br>
End Sub<br>
</font><br>
<br>

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top