I'm having some trouble performing a mail merge using an embedded Word file. Essentially
I'm trying to build a macro to:
Here's the code that I'm upto so far, but it won't close the WINWORD application afterwards:
Public Sub fExportSVF()
Dim WdObj As Object
Dim WdApp As Word.Application
Dim WdDoc As Word.Document
Dim intIndex As Integer
Dim strPeril As String
Dim strClaimNumber As String
Dim strPHName As String
Dim strSaveLoc As String
Dim strWbName As String
Dim strTempLoc As String
Dim xlObj As Object
Application.ScreenUpdating = False
Call fUnhideSheet("EXPORT_DATA")
Application.DisplayAlerts = False
ThisWorkbook.Save
Application.DisplayAlerts = True
strTempLoc = Environ("TEMP") & Int((9999 - 1 + 1) * Rnd + 1) & ".xlsm"
strWbName = Worksheets("Settings").Range("B4").Value
strPeril = Worksheets("Settings").Range("B3").Value
strClaimNumber = Worksheets("Settings").Range("B1").Value
strPHName = Worksheets("Settings").Range("B2").Value
If Dir(strTempLoc) <> "" Then Kill strTempLoc
Set xlObj = CreateObject("Scripting.FileSystemObject")
xlObj.CopyFile ThisWorkbook.FullName, strTempLoc, True
strSaveLoc = ActiveWorkbook.Path & "\" & strClaimNumber & _
" - " & strPHName & " - " & strPeril & ".pdf"
Select Case strPeril
Case "Acc"
intIndex = 2
Case "Acci"
intIndex = 3
Case "AD"
intIndex = 4
Case "Es"
intIndex = 5
Case "Fi"
intIndex = 6
Case "Fld"
intIndex = 7
Case "Impt"
intIndex = 8
Case "St"
intIndex = 9
Case "Th"
intIndex = 10
End Select
Set WdObj = Worksheets("Settings").OLEObjects(intIndex)
WdObj.Activate
WdObj.Object.Application.Visible = False
Set WdApp = GetObject(, "Word.Application")
Set WdDoc = WdApp.ActiveDocument
WdApp.Visible = True
WdDoc.MailMerge.MainDocumentType = wdFormLetters
WdDoc.MailMerge.OpenDataSource Name:= _
strWbName _
, ConfirmConversions:=False, ReadOnly:=False, LinkToSource:=True, _
AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:="", _
WritePasswordDocument:="", WritePasswordTemplate:="", Revert:=False, _
Format:=wdOpenFormatAuto, Connection:= _
"Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;Data Source=" & strTempLoc & _
";Mode=Read;Extended Properties=""HDR=YES;IMEX=1;"";Jet " & _
"OLEDB:System database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Engine " _
, SQLStatement:="SELECT * FROM `EXPORT_DATA$`", SQLStatement1:="", _
SubType:=wdMergeSubTypeAccess
WdDoc.MailMerge.ViewMailMergeFieldCodes = wdToggle
WdDoc.TablesOfContents(1).Update
WdDoc.ExportAsFixedFormat outputfilename:=strSaveLoc, _
ExportFormat:=wdExportFormatPDF, OpenAfterExport:=True, OptimizeFor:= _
wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=1, To:=1, _
Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
BitmapMissingFonts:=True, UseISO19005_1:=False
WdApp.ActiveDocument.Close wdDoNotSaveChanges
Set WdApp = Nothing
Set WdObj = Nothing
Kill strTempLoc
Call fHideSheet("EXPORT_DATA")
Application.ScreenUpdating = True
End Sub
So it does everything apart from close the WINWORD application. Apart from that, I've noticed that if there's another document already open, it will make that invisible as well.
Any help please?
Cheers
EDIT:
Also, the machines that this code will be running on will have both Word 97 and Word 2007 on them. The document will need to open and edited in Word 2007.
Change this line (which sets WdApp to an existing instance of Word, if any):
Set WdApp = GetObject(, "Word.Application")
to this, which creates a new instance of Word, to prevent closing other documents which might be open:
Set WdApp = CreateObject("Word.Application")
To close wdApp
use the Quit method:
wdApp.Quit