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

Closing a Word Document from Access 1

Status
Not open for further replies.

rj51cxa

Technical User
Mar 16, 2006
216
GB
The following code runs a macro and then opens a word document -"Macros.doc" which contains an autorun macro. This inserts files into a Word template which is then saved as a new document.

Code:
Dim stDocName As String
    

    stDocName = "OutputResults"
    DoCmd.SetWarnings False
    DoCmd.RunMacro stDocName
    DoCmd.SetWarnings True
    
    Dim LWordDoc As String
    Dim oApp As Object

    LWordDoc = "c:\zzz\Macros.doc"
    
    If Dir(LWordDoc) = "" Then
    MsgBox "Document Not Found"
    Else
    Set oApp = CreateObject("Word.Application")
    oApp.Visible = True
    oApp.Documents.Open Filename:=LWordDoc
    DoCmd.Maximize
    
    End If

When the macro has run and the new document has been created, I am left with the new document and Macros.doc open.

Can anyone please advise the code I should use to close Macros.doc but leave Word open, with the new document visible.

Best Regards
John
 
rj51cxa,
Something like this should work.
Code:
...
Dim oMacroDoc As Object
...
Set oMacroDoc = oApp.Documents.Open Filename:=LWordDoc
oMacroDoc.Close
...

Hope this helps,
CMP

(GMT-07:00) Mountain Time (US & Canada)
 
Thanks CautionMP,

However, I'm afraid we have a small problem -
when I insert the line "Set ...." the code changes to red and shows me the error box "Expected end of Statement"

I think there must be something missing.

I inserted the code after the "End If". Is this where it should go, or should it go before it?

John
 
rj51cxa,
Sorry, I forgot the punctuation that's needed since an object is being returned.
Code:
Set oMacroDoc = oApp.Documents.Open[red]([/red]Filename:=LWordDoc[red])[/red]

CMP
;-)

(GMT-07:00) Mountain Time (US & Canada)
 
Sorry CautionMP,

I'm afraid I cannot see where your bit of code fits into mine. I placed it after the End If part of mine but all it does is open a second version of the macro page which runs all over again.

Could you please be very kind and copy my code, indicating where yours should fit in.

John
 
Code:
Dim stDocName As String
    

    stDocName = "OutputResults"
    DoCmd.SetWarnings False
    DoCmd.RunMacro stDocName
    DoCmd.SetWarnings True
    
    Dim LWordDoc As String
    Dim oApp As Object
    [b]Dim oMacroDoc As Object[/b]

    LWordDoc = "c:\zzz\Macros.doc"
    
    If Dir(LWordDoc) = "" Then
    MsgBox "Document Not Found"
    Else
    Set oApp = CreateObject("Word.Application")
    oApp.Visible = True
    '[s]oApp.Documents.Open Filename:=LWordDoc[/s]
    [b]Set oMacroDoc = oApp.Documents.Open(Filename:=LWordDoc)[/b]
    DoCmd.Maximize
    [b][green]'Hopefully DoEvents will allow the macros in [i]c:\zzz\Macros.doc[/i]
    'to run, creating the new document, before Close is called on [i]c:\zzz\Macros.doc[/i][/green]
    DoEvents
    oMacroDoc.Close
    Set oMacroDoc = Nothing[/b]
    
    End If

(GMT-07:00) Mountain Time (US & Canada)
 
Thanks CautionMP,

That did the trick exactly. I really appreciate your help.

Best Regards
John
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top