Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Type mismatch 'MailMerge.OpenDataSource

Type mismatch 'MailMerge.OpenDataSource

Type mismatch 'MailMerge.OpenDataSource


I have converted that script from VBA to VBS , But I have faced with that problem :
Type mismatch 'MailMerge.OpenDataSource

CODE --> VBscript

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 

RE: Type mismatch 'MailMerge.OpenDataSource

Do not use "=" after argument names in procedure call. ArgName=Value generates True/False result that it is passed to procedure.


RE: Type mismatch 'MailMerge.OpenDataSource

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$`")

RE: Type mismatch 'MailMerge.OpenDataSource

wordDoc.MailMerge.OpenDataSource "D:\shared\programs\vbscript\Audi\From_Mail_Merge\final" & "\Mail_Merge_Data_Form.xls",,,,,,,,,,,, SELECT * FROM `Sheet1$`",,


RE: Type mismatch 'MailMerge.OpenDataSource

Still I have a Sheet1 Dialog though I have the below syntax ???

doc.MailMerge.OpenDataSource strPath & strDataSource,,,,,,,,,,"SELECT * FROM `Sheet1$`"

RE: Type mismatch 'MailMerge.OpenDataSource

Also I tried the below syntax with the same error : Type Mismatch ???

doc.MailMerge.OpenDataSource strPath & strDataSource,false,false,false,false,"","","","",false,,"","SELECT * FROM `Sheet1$`","",""

Any suggestion ????

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close