×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Starting Microsoft Word from Visual Basic

Starting Microsoft Word from Visual Basic

Starting Microsoft Word from Visual Basic

(OP)
Hi all,

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.
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?

thanks,
Henk

RE: Starting Microsoft Word from Visual Basic

There were good examples in threads called "Unauthorized Applications" (2/6/00 - posted 2/3) and "Disallowing Multple Instances...." (posted later). The answers aren't an exact solution but you should be easily able to bend the code to suit your exact needs.

RE: Starting Microsoft Word from Visual Basic

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:


Dim Words As Word.Application
Dim istr As String
Dim today As Date
Dim ires, inday As Integer
Dim docs As Word.Document

Screen.MousePointer = 11

Label1.Caption = "Connecting to Database...."
Label1.Refresh


'Call connect function which creates view for merge
connect (inday)

Label1.Caption = "Merging Data...Please Wait"

On Error GoTo trap

'Open an current instance of Word, if running
Set Words = GetObject(, "Word.Application")

Words.Visible = True

Word.Documents.Open FileName:=App.Path & "\mmletter.doc", ConfirmConversions:=False, _
ReadOnly:=False, AddToRecentFiles:=False, PasswordDocument:="", _
PasswordTemplate:="", Revert:=False, WritePasswordDocument:="", _
WritePasswordTemplate:="", Format:=wdOpenFormatAuto

'Restore the Word if it was minimized.
Words.WindowState = wdWindowStateMaximize

'Begin the merge process!!!!



MergeExit:

Screen.MousePointer = 1
Label1.Caption = "Finished Merge!"
Label1.Refresh
Set Words = Nothing 'Kill the word connection
Exit Sub

trap:
'minimal error trapping
If Err = 429 Then
'If we get an error of 429 - no instance running, create an
'instance and resume at the next line
Set Words = GetObject("", "Word.Application")
Resume Next
Else
'Else, report error code and description
ires = MsgBox("Error " & Err & " - " & Error, vbCritical, "Insular System")
End If

Resume MergeExit


End Sub


Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close