Sub PrintLetter2(numerorecord As Integer)
On Error GoTo ErrorHandler
' open template in Word
Dim str As String
Dim WordApp As Word.Application
Dim WordDoc As Word.Document
Set WordApp = CreateObject("Word.Application")
str = ThisWorkbook.Path
Debug.Print str, numerotessera
With WordApp
.Visible = True
Set WordDoc = .Documents.Open(str & "formletter.docx")
End With
'MailMerge selected records from table to Word document
WordDoc.MailMerge.OpenDataSource Name:=str & "Book1test.xlsx", _
ConfirmConversions:=False, ReadOnly:=False, LinkToSource:=False, AddToRecentFiles:=False, _
PasswordDocument:="", PasswordTemplate:="", WritePasswordDocument:="", _
WritePasswordTemplate:="", Revert:=False, Format:=wdOpenFormatAuto, _
Connection:=str & "Book1test.xlsx", _
SQLStatement:="SELECT * FROM [ElencoCompleto] WHERE [Numero record] = numerorecord", _
SQLStatement1:=""
'send merged document into new Word document
WordDoc.MailMerge.Destination = wdSendToPrinter
WordDoc.MailMerge.Execute
'close without saving
WordDoc.Close SaveChanges:=False
ErrorHandlerExit:
Exit Sub
ErrorHandler:
If Err = 429 Then
'word is not running; open word with CreateObject
Set appWord = CreateObject(Class:="Word.Application")
End If
End Sub |