• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 273
  • Last Modified:

Close template document once done with it

I have a button on a form that gnerates an word document letter. it opens a template then create a 2nd document using that template and inserts data from a table but it then leaves the template open. is there a way for me to tell it to close it in the sub routine? the template name is Preliminary_V1.doc. The 2 sub routines are below
Private Sub MailMergeInit_Click()

  Dim objWord As Word.Document
  Dim objDocument As Word.Document
  
  Set objWord = GetObject("\\Txnt38\puwtc\Business_Partner_Database\Preliminary_V1.doc", "Word.Document")
  ' Make word visible
  objWord.Application.Visible = True
  ' Set the mail merge data source as the current record
  objWord.MailMerge.OpenDataSource Name:=Access.CurrentDb.Name, LinkToSource:=True, Connection:="TABLE tblMain", SQLStatement:="SELECT * FROM [tblMain] WHERE [Notice Number]=" & Notice_Number.Value
  objWord.MailMerge.Execute
  
  ' Grab the newly opened document
  Set objDocument = objWord.Application.ActiveDocument
  Sleep 2000 ' sleep for 1000msec = 1 second
  ReplaceDocLinks objDocument
  

End Sub


and

Private Sub ReplaceDocLinks(oDoc As Word.Document)

  'On Error GoTo Err_ReplaceDocLinks
 
  ' Find next Document link: #\\
 
  With oDoc.Content.Find
 
    .ClearFormatting
    .Text = "#\\\\*#"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = True
   
    Do While .Execute(FindText:="#\\\\*#", Forward:=True, Format:=False) = True
      With .Parent
        .Select
        linkLocation = Selection.Text
        properLocation = Mid(linkLocation, 2, Len(linkLocation) - 2)
        .Cut
        oDoc.Hyperlinks.Add _
          Anchor:=Selection.Range, _
          Address:=properLocation
      End With
    Loop
   
    Do While .Execute(FindText:="#http://*#", Forward:=True, Format:=False) = True
      With .Parent
        .Select
        linkLocation = Selection.Text
        properLocation = Mid(linkLocation, 2, Len(linkLocation) - 2)
        .Cut
        oDoc.Hyperlinks.Add _
          Anchor:=Selection.Range, _
          Address:=properLocation
      End With
    Loop
    Set firstRange = ActiveDocument.Range(Start:=0, End:=1)
    firstRange.Select
  End With
 
  MsgBox "File export completed."
Resume_ReplaceDocLinks:
  Exit Sub
 
Err_ReplaceDocLinks:
  MsgBox "Warning: Could not perform text link replacement.  Try closing the document and re-exporting it.", vbCritical
  Resume Resume_ReplaceDocLinks
 
End Sub

Open in new window

0
jlcannon
Asked:
jlcannon
  • 3
1 Solution
 
danishaniCommented:
Are you planning to save the newly generated Mailmerge document?

Usely you can close a document with someting like:
objWord.Documents("Preliminary_V1.doc").Close SaveChanges:=wdDoNotSaveChanges

Hope this helps,
Daniel



0
 
GrahamSkanCommented:
You now have two documents open with separate object variables. You can close the Main document any time after the merge
'...
  objWord.MailMerge.Execute
  objWord.Close wdDonotsavechanges
  ' Grab the newly opened document
  Set objDocument = objWord.Application.ActiveDocument
'...
0
 
jlcannonAuthor Commented:
@GrahamSkan: this does close the template but it inturn creates another copy of the resulting letter called Letter2
0
 
jlcannonAuthor Commented:
nevermind I found out i just needed the one line not the whole thing. I just needed to add the

 objWord.Close wdDonotsavechanges
0
 
jlcannonAuthor Commented:
Perfection
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now