Link to home
Start Free TrialLog in
Avatar of jlcannon
jlcannon

asked on

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

Avatar of danishani
danishani
Flag of United States of America image

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



ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jlcannon
jlcannon

ASKER

@GrahamSkan: this does close the template but it inturn creates another copy of the resulting letter called Letter2
nevermind I found out i just needed the one line not the whole thing. I just needed to add the

 objWord.Close wdDonotsavechanges
Perfection