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

Custom Access to Word VBA Problem, Question

Status
Not open for further replies.

Jack58

Technical User
May 6, 2002
91
0
0
US
I am using the Code listed below in Access 2000 with a Command Button to Open Word, Open a Document and then run a Word Macro.

Private Sub Command7_Click()
On Error GoTo Err_OpenGuide

Dim Oapp As Object

Set Oapp = CreateObject("Word.Application")
Oapp.Documents.Add "C:\Divisions\PrintOut.doc"
Oapp.Run "PrintOut"
ActiveDocument.ActiveWindow.Close
Set Oapp = Nothing
Exit_OpenGuide:
Exit Sub



Err_OpenGuide:
MsgBox Err.Description
Resume Exit_OpenGuide

End Sub

The problem is when I have created a Second Command Button( to open another Doc. File) with the following Code,

Private Sub cmdReport_Click()

Call ReportCreation(Me![cboDiv], Me![cboBuyer])

On Error GoTo Err_OpenGuide

Dim Oapp As Object

Set Oapp = CreateObject("Word.Application")
Oapp.Documents.Add "C:\Divisions\Report.doc"
Oapp.Run "PrintOut"
ActiveDocument.ActiveWindow.Close
Set Oapp = Nothing
Exit_OpenGuide:
Exit Sub

Err_OpenGuide:
MsgBox Err.Description
Resume Exit_OpenGuide



End Sub

The Macro did not run. The Document did open.

I sure could use some help with this.


Thanks



Jack
 
Anyway, replace this:
ActiveDocument.ActiveWindow.Close
By this:
Oapp.ActiveDocument.ActiveWindow.Close

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Jack,

Some additional comments:

When I run your code (albeit from Excel) the Oapp.Documents.Add lines create new, blank documents, rather than open existing ones. According to the Word VBA documentation, the method to use is Open, as in
Oapp.Documents.Open filename

Also, your code leaves instances of Word running (which you wouldn't see without setting Oapp.Visible = True). Insert Oapp.Quit before you set Oapp = Nothing.

Regards,
Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top