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

Program Command Button to Open MS Word Document 1

Status
Not open for further replies.

penndro

Technical User
Jan 9, 2005
108
US
I have a command button on a form that I need to OPEN a specific MS WORD document when CLICKed. Can someone help me with the VB code to initiate this action. Right now the button will open MS Word – but it only opens the application and does not open a specific file.

File name = U:/ThankYouLtr
Command Button = Word_Doc

Current Code =
Private Sub WORD_DOC_Click()
On Error GoTo Err_WORD_DOC_Click

Dim oApp As Object

Set oApp = CreateObject("Word.Application")
oApp.Visible = True

Exit_WORD_DOC_Click:
Exit Sub

Err_WORD_DOC_Click:
MsgBox Err.Description
Resume Exit_WORD_DOC_Click

End Sub
 
Code:
[navy]Private Sub [/navy] WORD_DOC_Click()
On [navy]Error Goto[/navy] Err_WORD_DOC_Click

    [navy]Dim[/navy] oApp[navy] As Object[/navy]

    Set oApp = [b]GetObject("U:/ThankYouLtr.doc")[/b]
    oApp.Visible = [navy]True[/navy]

[navy]Exit[/navy]_WORD_DOC_Click:
    [navy]Exit Sub [/navy]

Err_WORD_DOC_Click:
    MsgBox Err.Description
    Resume [navy]Exit[/navy]_WORD_DOC_Click
    
[navy]End Sub [/navy]

Funny thing about being unemployed, weekends don't mean quite so much, just means you get to hang out with your working friends. Primus
 
I tried the code - but it is not working.

I am getting this error msg: "File name or file not found during Automation Operation".

here is the exact code using:
Private Sub WORD_DOC_Click()
On Error GoTo Err_WORD_DOC_Click

Dim oApp As Object

Set oApp = GetObject("U:\Computer\Working Databases\IntrviweThankyouMerge")
oApp.Visible = True

Exit_WORD_DOC_Click:
Exit Sub

Err_WORD_DOC_Click:
MsgBox Err.Description
Resume Exit_WORD_DOC_Click

End Sub
 
File extension (and spellign) for IntrviweThankyouMerge?

If there is no extension the [tt]GetObject()[/tt] function has no way of knowing how to process the file (same clicking on a file in Windows Explorer, you would get a dialog asking what program you want to use to open the file).

CMP

Funny thing about being unemployed, weekends don't mean quite so much, just means you get to hang out with your working friends. Primus
 
Or, you could use the word object direct

Private Sub WORD_DOC_Click()
On Error GoTo Err_WORD_DOC_Click

Dim oApp As Object
Set oApp = CreateObject("Word.Application")
oApp.Documents.Open ("U:\Computer\Working Databases\IntrviweThankyouMerge.doc")
oApp.Visible = True
Set oApp = Nothing
End Sub

This would however be better written as a procedure, passing in the filename to open, that way, the function is generic
 
Adding the file extension worked; however, because my underlying document is a word mail merge document tied to a MS Access Parameter Query the application did not handle the request very well. Word would hang up for a long time and then the prompt box for the parameter query was getting lost.

I figured the next best thing was to create the document as a report in MS Access. Unless you have a better idea.

Thanks for your help.
Penndro
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top