; ---------------------------------------------------------------------------------------------------------------------------------------
; Merge data of asset into corresponding fields and generate a PDF file
; ---------------------------------------------------------------------------------------------------------------------------------------
Function GenerateAssetPage($strPath, $strDataSource, $strTemplate)
Dim $Ret
Dim $objDoc
Dim $objWordApp
Dim $wdDoNotSaveChanges
$wdDoNotSaveChanges = 0 ;Do not save pending changes
$objWordApp = CreateObject("Word.Application")
If @ERROR <> 0
$Ret = MessageBox(@SERROR + " at CreateObject(Word.Application), terminating", "Problem with Office", 16)
$objWordApp.quit($wdDoNotSaveChanges)
Quit
EndIf
; ---------------------------------------------------------------------------------------------------------------------------------------
; Start Word using mailmerge template
; ---------------------------------------------------------------------------------------------------------------------------------------
$objDoc = $objWordApp.Documents.Add(@SCRIPTDIR + "\" + $strTemplate)
If @ERROR <> 0
$Ret = MessageBox("Unable to open template " + $strPath + $strTemplate + ", terminating", "Problem with Office", 16)
$objWordApp.quit($wdDoNotSaveChanges)
Quit
EndIf
; ---------------------------------------------------------------------------------------------------------------------------------------
;Do the mail merge to a new document.
; ---------------------------------------------------------------------------------------------------------------------------------------
$objDoc.MailMerge.OpenDataSource($strPath + $strDataSource)
If @ERROR <> 0
$Ret = MessageBox("Unable to open datasource " + $strPath + $strDataSource + ", terminating", "Problem with Office", 16)
$objWordApp.quit($wdDoNotSaveChanges)
Quit
EndIf
; ---------------------------------------------------------------------------------------------------------------------------------------
;checking value of named constant: wdSendToNewDocument
; ---------------------------------------------------------------------------------------------------------------------------------------
$objDoc.MailMerge.Destination = 0
$objDoc.MailMerge.SuppressBlankLines = 1
; ---------------------------------------------------------------------------------------------------------------------------------------
;setting to values of named constants: wdDefaultFirstRecord and wdDefaultLastRecord
; ---------------------------------------------------------------------------------------------------------------------------------------
Dim $wdDefaultFirstRecord, $wdDefaultLastRecord
$wdDefaultFirstRecord = 1
$wdDefaultLastRecord = -16
$objDoc.MailMerge.DataSource.FirstRecord = $wdDefaultFirstRecord
$objDoc.MailMerge.DataSource.LastRecord = $wdDefaultLastRecord
; ---------------------------------------------------------------------------------------------------------------------------------------
;checking value of named constant: wdMainAndDataSource
; ---------------------------------------------------------------------------------------------------------------------------------------
If $objDoc.MailMerge.State = 2
$objDoc.MailMerge.Execute
If @ERROR <> 0
$Ret = MessageBox("Unable to execute mailmerge, terminating", "Problem with Office", 16)
$objWordApp.quit($wdDoNotSaveChanges)
Quit
EndIf
EndIf
; ---------------------------------------------------------------------------------------------------------------------------------------
;Do not display the mail merge document
; ---------------------------------------------------------------------------------------------------------------------------------------
$objWordApp.Visible = False
; ---------------------------------------------------------------------------------------------------------------------------------------
;Save mail merge document as PDF
; ---------------------------------------------------------------------------------------------------------------------------------------
Dim $wdFormatPDF
$wdFormatPDF = 17
$objWordApp.ActiveDocument.SaveAs($strPath + @WKSTA + ".pdf", $wdFormatPDF)
If @ERROR <> 0
$Ret = MessageBox("Unable to save PDF, terminating", "Problem with Office", 16)
$objWordApp.quit($wdDoNotSaveChanges)
Quit
EndIf
$objWordApp.quit($wdDoNotSaveChanges)
EndFunction