close specific word document, leave others open

krowe

Registered User.
Local time
Today, 07:05
Joined
Mar 29, 2011
Messages
159
Hi

I have a script that does a mail merge. At the end of the script I get 2 docs, the original merge doc with all the form fields etc in, and the merged letter.

I would like to close the original document but leave the letter open so it can be edited/saved/printed.

here is my code:

Code:
DoCmd.SetWarnings False
Dim mypath As String
Dim mypath3 As String
Dim Wordpath As String
Dim folder As String
Dim sDBPath As String
Dim oApp As Word.Application
Dim ThisDB As String
Dim oWord As Word.Document
Dim oMainDoc As Word.Document
  
 
  
    Wordpath = Environ("office") & "\winword.exe"
    mypath = Left$(CurrentDb.Name, Len(CurrentDb.Name) - Len(Dir$(CurrentDb.Name)))
    mypath3 = ("" & mypath & "RIA-DG.doc""")
    ThisDB = CurrentDb.Name
        
    DoCmd.RepaintObject , ""
    DoCmd.OpenQuery "qryPersonAdviserAddress", acViewNormal, acEdit 'create PersonAdviserAddress Table
    DoCmd.OpenQuery "qryPAAStLetterLLAgent", acViewNormal, acEdit ' create RIALetterExport Table
    
    folder = CurrentProject.Path & "\"
    
    Set oApp = CreateObject("Word.Application")
    Set oWord = oApp.Documents.Open(FileName:=mypath3)
    oApp.Visible = True
    With oWord.MailMerge
            .MainDocumentType = wdFormLetters
            sDBPath = ThisDB
            .OpenDataSource Name:=sDBPath, _
            SQLStatement:="SELECT * FROM [tblRIALetterExport]"
    End With
    With oWord
        .MailMerge.Destination = wdSendToNewDocument
        .MailMerge.Execute
    End With
    
    oApp.Activate
    oApp.Documents.Parent.Visible = True
    oApp.Application.WindowState = 1
    oApp.ActiveWindow.WindowState = 1

I would like to close file RIA_DG.doc, but can only find methods of closing word, which closes all word docs i have open.

Thanks

Kev
 
I don't do word automation but the concept is similar. You have opened the word document you want to close via the oWord object. You can close it via that same object too (and it won't affect any others). So:

oWord.Close

Or

oWord.Exit

Or

something along those lines.
 
Thanks, that did it, easy when you know how!
 

Users who are viewing this thread

Back
Top Bottom