Sorry to bother with a silly question, but this is driving me nuts. I have a sub that print a letter using word mailmerge. The source data used to be in an Excel sheet, and everything was fine, For a number oif reasons, I exported all data (including the excel file) into SQL Express and changed the code accordingly. Unfortunately, after a number of corrections to fix sayntx errors, I am stuck with this runtime data type mismatch I cannot get rid of. Is there a way to understand what causes the error? The error is in the opendatasource statemewnt
Sub PrintLetter2(Numerotessera As Double) 'Mail merge e stampa su stampante di default 'On Error GoTo ErrorHandler ' open template in Word Dim filepath As String Dim WordApp As Word.Application Dim WordDoc As Word.Document Dim strConnection As String, myquery As String Dim bPrintBackgroud As Boolean Dim oMerged As Document Set WordApp = New Word.Application filepath = CurrentProject.path Debug.Print filepath, Numerotessera With WordApp .Visible = True Set WordDoc = .Documents.Open(filepath & "\LetteraNuoviSoci.docx") End With
'MailMerge selected records from table to Word document myquery = "SELECT * FROM [LibroSoci] WHERE [Numero tessera] = " & Numerotessera With WordApp .ActiveDocument.MailMerge.MainDocumentType = wdFormLetters .ActiveDocument.MailMerge.OpenDataSource _ Name:="C:\Testhyp4\RicevuteConPrimaNotaFE.accdb", _ ConfirmConversions:=False, ReadOnly:=False, LinkToSource:=True, _ AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:="", _ WritePasswordDocument:="", WritePasswordTemplate:="", Revert:=False, _ Format:=wdOpenFormatAuto, Connection:=RicevuteCPNCompleto, _ SQLStatement:=myquery, SQLStatement1:="", _ SubType:=wdMergeSubTypeOther bPrintBackgroud = Options.PrintBackground Options.PrintBackground = False .Application.DisplayAlerts = wdAlertsNone With .ActiveDocument.MailMerge .Destination = wdSendToNewDocument .SuppressBlankLines = True 'filepath & "\" & CurrentProject.Name, _ With .DataSource .FirstRecord = wdDefaultFirstRecord .LastRecord = wdDefaultLastRecord End With .Execute Pause:=False End With Set oMerged = .ActiveDocument 'Show the Print dialog box .Visible = True .Activate .Dialogs(wdDialogFilePrint).Show oMerged.Close 0 'Restore all the alerts .Application.DisplayAlerts = wdAlertsAll Options.PrintBackground = bPrintBackgroud End With WordDoc.Close SaveChanges:=False WordApp.Quit 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