e***@gmail.com
2015-02-27 06:09:54 UTC
I have converted that script from VBA to VBS , But I have faced with that problem :
Dim msWord
Dim wordDoc
Dim wkbk
Dim headerRange
Dim headerValues
Dim i
Const wdFormLetters = 0
Const wdFieldMergeField = 59
Const wdSendToNewDocument = 0
Const wdDefaultFirstRecord = 1
Const wdDefaultLastRecord = -16
' grab MS Word
Set msWord = GetWordApp
' open mail merge document
If Not msWord Is Nothing Then
Set wordDoc = GetWordDoc(msWord, "D:\shared\programs\vbscript\Audi\From_Mail_Merge\final" & "\Mail_Merge_Difficult_Form.doc")
' link document to data source
wordDoc.MailMerge.MainDocumentType = wdFormLetters
wordDoc.MailMerge.OpenDataSource Name="D:\shared\programs\vbscript\Audi\From_Mail_Merge\final" & "\Mail_Merge_Data_Form.xls", _
SQLStatement="SELECT * FROM `Sheet1$`"
' populate body of document with fields from data source
' first get field names from worksheet
Set wkbk = Excel.Workbooks.Open("D:\shared\programs\vbscript\Audi\From_Mail_Merge\final" & "\Mail_Merge_Data_Form.xls")
Set headerRange = Excel.Range(wkbk.Sheets("Sheet1").Range("A1"), wkbk.Sheets("Sheet1").Range("IV1").End(xlToLeft))
headerValues = Application.Transpose(headerRange.Value)
wkbk.Close False
' put header values onto worksheet along with merge fields
'code For i = 1 To UBound(headerValues)
' field name
'code msWord.Selection.TypeText Text:=headerValues(i, 1) & ": "
' field value
'code wordDoc.Fields.Add Range:=msWord.Selection.Range, _
'code Type:=wdFieldMergeField, _
'code Text:="""" & Replace(headerValues(i, 1), " ", "_") & """"
' line break
'code msWord.Selection.TypeParagraph
'code Next i
' perform mail merge
With wordDoc.MailMerge
.Destination = wdSendToNewDocument ' wdSendToPrinter if you want to print instead
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute Pause=False
End With
' show merged document
msWord.Visible = True
End If
Function GetWordApp()
On Error Resume Next
Set GetWordApp = CreateObject("Word.Application")
End Function
Function GetWordDoc(wordApp , Filename )
Set GetWordDoc = wordApp.Documents.Open(Filename)
End Function
Dim msWord
Dim wordDoc
Dim wkbk
Dim headerRange
Dim headerValues
Dim i
Const wdFormLetters = 0
Const wdFieldMergeField = 59
Const wdSendToNewDocument = 0
Const wdDefaultFirstRecord = 1
Const wdDefaultLastRecord = -16
' grab MS Word
Set msWord = GetWordApp
' open mail merge document
If Not msWord Is Nothing Then
Set wordDoc = GetWordDoc(msWord, "D:\shared\programs\vbscript\Audi\From_Mail_Merge\final" & "\Mail_Merge_Difficult_Form.doc")
' link document to data source
wordDoc.MailMerge.MainDocumentType = wdFormLetters
wordDoc.MailMerge.OpenDataSource Name="D:\shared\programs\vbscript\Audi\From_Mail_Merge\final" & "\Mail_Merge_Data_Form.xls", _
SQLStatement="SELECT * FROM `Sheet1$`"
' populate body of document with fields from data source
' first get field names from worksheet
Set wkbk = Excel.Workbooks.Open("D:\shared\programs\vbscript\Audi\From_Mail_Merge\final" & "\Mail_Merge_Data_Form.xls")
Set headerRange = Excel.Range(wkbk.Sheets("Sheet1").Range("A1"), wkbk.Sheets("Sheet1").Range("IV1").End(xlToLeft))
headerValues = Application.Transpose(headerRange.Value)
wkbk.Close False
' put header values onto worksheet along with merge fields
'code For i = 1 To UBound(headerValues)
' field name
'code msWord.Selection.TypeText Text:=headerValues(i, 1) & ": "
' field value
'code wordDoc.Fields.Add Range:=msWord.Selection.Range, _
'code Type:=wdFieldMergeField, _
'code Text:="""" & Replace(headerValues(i, 1), " ", "_") & """"
' line break
'code msWord.Selection.TypeParagraph
'code Next i
' perform mail merge
With wordDoc.MailMerge
.Destination = wdSendToNewDocument ' wdSendToPrinter if you want to print instead
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute Pause=False
End With
' show merged document
msWord.Visible = True
End If
Function GetWordApp()
On Error Resume Next
Set GetWordApp = CreateObject("Word.Application")
End Function
Function GetWordDoc(wordApp , Filename )
Set GetWordDoc = wordApp.Documents.Open(Filename)
End Function