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!

Open Word doc form Userform cmdbutton

Status
Not open for further replies.

Pudsters

Technical User
Mar 16, 2006
151
US
The following works fine from Excel with a macro button but how do I modify it to work from a cmd button click on a user form, assuming the button name is cmdButton1?

Private Sub OpenWordDoc()

Dim wdApp As Word.Application, wdDoc As Word.Document

On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then 'Word isn't already running
Set wdApp = CreateObject("Word.Application")
End If
On Error GoTo 0

Set wdDoc = wdApp.Documents.Open("C:\Filename.doc")
wdApp.Visible = True

End Sub
 
Copy your code in the Click event procedure of cmdButton1 ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Okay, I tried that but I get an error message. I commented out the extra subs. I don't know the proper way to do this?

Private Sub cmdButton1_Click()

'Private Sub OpenWordDoc()

Dim wdApp As Word.Application, wdDoc As Word.Document

On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then 'Word isn't already running
Set wdApp = CreateObject("Word.Application")
End If
On Error GoTo 0

Set wdDoc = wdApp.Documents.Open("C:\Filename.doc")
wdApp.Visible = True

'End Sub



End Sub
 
I get an error message
Any chance you could post the whole message and which line is highlighted when in debug mode ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Yes, when mentioning error messages we absolutely need to know what it says!

Also, could you use the code tags to post code? Thanks. If you are not sure how to do this, click the Process TGML link at the bottom of the message area.

Gerry
My paintings and sculpture
 

The two lines in bold are highlighted. Compile error - User defined type not defined:



Private Sub cmdButton1_Click()

'Private Sub OpenWordDoc()

Dim wdApp As Word.Application, wdDoc As Word.Document

On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then 'Word isn't already running
Set wdApp = CreateObject("Word.Application")
End If
On Error GoTo 0

Set wdDoc = wdApp.Documents.Open("C:\Filename.doc")
wdApp.Visible = True

'End Sub



End Sub
 
Either reference the Microsoft Word library or use late binding:
Code:
Private Sub cmdButton1_Click()
Dim wdApp As Object
On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then 'Word isn't already running
  Set wdApp = CreateObject("Word.Application")
End If
On Error GoTo 0
wdApp.Documents.Open "C:\Filename.doc"
wdApp.Visible = True
Set wdApp = Nothing
End Sub

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hey! That was the first thing I did, referencing MS WORD. But I went back and the checkbox was unchecked. I rechecked it and saved the file.

Now it works fine. Thanks for all of your help!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top