Discussion:
Type mismatch 'MailMerge.OpenDataSource
(too old to reply)
e***@gmail.com
2015-02-27 06:09:54 UTC
Permalink
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
Mayayana
2015-02-27 14:08:56 UTC
Permalink
I don't use Word, but it looks like this
is the problem:

wordDoc.MailMerge.OpenDataSource Name

OpenDataSource is a UDT, so it should
probably be:

wordDoc.MailMerge.OpenDataSource.Name

I don't think VBS can handle a UDT directly.
Also, that member is a String data type, which
VBS doesn't recognize. I expect the feasibility
probably depends on whether MS has provided
a Dispatch interface for the object hierarchy.
The page I found only showed .Net info.
GS
2015-02-27 20:33:38 UTC
Permalink
Not understanding why you require a vbs script to mail merge in Word
since Word has its own automated MailMerge feature.

Adding to Mayayana's input, vba is equipped to handle the object
hierarchy of MS Office apps; vbs is not! You might be better off to
setup the MailMerge feature in Word, then use vbs to have Word execute
it.
--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
e***@gmail.com
2015-02-28 15:23:11 UTC
Permalink
What should be the exact syntax ??

wordDoc.MailMerge.OpenDataSource.Name=("D:\shared\programs\vbscript\Audi\From_Mail_Merge\final\Mail_Merge_Data_Form.xls", _
SQLStatement="SELECT * FROM `Sheet1$`")

I need to automate the MailMerge through a batch file and vbs file and save each Merged generated file to a different name .
GS
2015-02-28 19:27:55 UTC
Permalink
Post by e***@gmail.com
What should be the exact syntax ??
wordDoc.MailMerge.OpenDataSource.Name=("D:\shared\programs\vbscript\Audi\From_Mail_Merge\final\Mail_Merge_Data_Form.xls",
_ SQLStatement="SELECT * FROM `Sheet1$`")
I need to automate the MailMerge through a batch file and vbs file
and save each Merged generated file to a different name .
I'm not much of a fan of using Word and so I can't answer you about
syntax. I see, though, in the Object Browser that MailMerge's
OpenDataSource method accepts args which you are treating as though
these are properties...

wordDoc.MailMerge.OpenDataSource.Name=("...

should be more like...

wordDoc.MailMerge.OpenDataSource Name:=sFilename, SQLStatement:=sSQL

..in VBA, where your string values are stored in variables that you can
modify as required. I don't know how to rewrite this in vbs in terms of
passing args by specifying their name because Script56.chm doesn't
example doing so.

I see here that your opening an Excel file and so I assume it contains
the list[s] of mail recipients and the Word file contains the body of
mail. Seems awfully 'high maintenance' to me to add using a .bat and
.vbs to the process when either of the source files can do this
directly! Normally, I'd just use Excel for both mailing list[s] and
body text since there's nothing Word can do that I can't duplicate in
Excel. (But then.., that's just my personal preference<g>)
--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
Loading...