Quantcast
Channel: Excel Help Forum - Excel Programming / VBA / Macros
Viewing all articles
Browse latest Browse all 50186

vba mailmerge problem

$
0
0
This is a strange problem (for me, at least). I am using word mailmerge function to print a form letter from vba code in excel. After a few attempts, I managed to implement this function using the code as follows

Code:

Dim filepath As String
Dim WordApp As Word.Application
Dim WordDoc As Word.Document
Set WordApp = New Word.Application
filepath = ThisWorkbook.Path
Debug.Print filepath, numerotessera
With WordApp
    .Visible = True
    Set WordDoc = .Documents.Open(filepath & "\form letter.docx")
End With
'MailMerge selected records from table to Word document
With WordApp
    .ActiveDocument.MailMerge.OpenDataSource Name:=filepath & "\" & ThisWorkbook.Name, _
        ConfirmConversions:=False, ReadOnly:=False, LinkToSource:=True, _
        AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:="", _
        WritePasswordDocument:="", WritePasswordTemplate:="", Revert:=False, _
        Format:=wdOpenFormatAuto, _
        SQLStatement:="SELECT * FROM `rangesoci` WHERE [Card number] =" & cardnumber, SQLStatement1:="", _
        SubType:=wdMergeSubTypeAccess
    With .ActiveDocument.MailMerge
        .Destination = wdSendToPrinter
        .SuppressBlankLines = True
                     
        With .DataSource
            .FirstRecord = wdDefaultFirstRecord
            .LastRecord = wdDefaultLastRecord
        End With
        .Execute Pause:=False
    End With
         
End With

WordDoc.Close SaveChanges:=False
WordApp.Quit

this code is called passing the cardnumber parameter, which identifies the record whose fields fill the formletter. The mechanism works fine, except that the Date fields appear SOMETIMES in the letter as numbers (e.g. 28/08/1937 ===> 13755), i.e. the unformatted date number. I am still trying to find if particular dates behave in this way, but at the moment the pattern (if any) escapes me. In any case, if I perform the mailmerge directly in word, using the same formletter and the same excel table source data, the thing works fine (dates display correctly). Any hint? Thank you
Robert

Viewing all articles
Browse latest Browse all 50186

Trending Articles