Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Type mismatch 'MailMerge.OpenDataSource

Status
Not open for further replies.

ehabaziz20012001

Programmer
Jan 13, 2006
10
EG

I have converted that script from VBA to VBS , But I have faced with that problem :
Type mismatch 'MailMerge.OpenDataSource
Code:
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
 
Do not use "=" after argument names in procedure call. ArgName=Value generates True/False result that it is passed to procedure.

combo
 
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$`")
 
wordDoc.MailMerge.OpenDataSource "D:\shared\programs\vbscript\Audi\From_Mail_Merge\final" & "\Mail_Merge_Data_Form.xls",,,,,,,,,,,, SELECT * FROM `Sheet1$`",,

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

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

 
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 ????
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top