Guys, help me please. I don't understand anything about VBA, it's my first time using it, I put together this code by mixing tutorials I saw. I made a code that does mail merge using an excel base with data source, and in that same base I leave the macro with buttons, when I click on a button it selects my word document and merges with the database, however,
she can only do that when the .docx and .xlsm are allocated directly on the local disk C: when I allocate them in some folder and try to repeat the following problem appears to me:
Sub mailmerge()
Call MergeRun("C:\Novapasta\2via\2v.docx", "C:\Novapasta\cup\Pastinha11.xlsm", "SELECT * FROM [Pastinha1$]")
End Sub
Sub MergeRun(frmFile As String, datFile As String, SQL As String)
Dim wdApp As Word.Application
Dim myDoc As Word.Document
On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Set wdApp = CreateObject("Word.Application")
End If
With wdApp
.Visible = True
'wdapp.application.DisplayAlerts=wdAlertsNone
'open word and apply datasource
Set myDoc = .Documents.Open(frmFile, False, False, False)
.ActiveDocument.MailMerge.MainDocumentType = wdFormLetters
.ActiveDocument.MailMerge.OpenDataSource Name:=datFile, _
ConfirmConversions:=False, ReadOnly:=False, LinkToSource:=False, _
AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:="", _
WritePasswordDocument:="", WritePasswordTemplate:="", Revert:=False, _
Format:=wdOpenFormatAuto, Connection:="", SQLStatement:=SQL, SQLStatement1 _
:="", SubType:=wdMergeSubTypeOther
'mail marge with document
With wdApp.ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = 1
.LastRecord = -16
End With
.Execute Pause:=False
End With
wdApp.Application.DisplayAlerts = wdAlertsNone
End With
' save as pdf
ActiveDocument.ExportAsFixedFormat OutputFileName:= _
"C:\Users\binho\Desktop\Novapasta\Inputpdf\doc.pdf", ExportFormat:=wdExportFormatPDF, _
OpenAfterExport:=True, OptimizeFor:=wdExportOptimizeForPrint, Range:= _
wdExportAllDocument, From:=1, To:=1, Item:=wdExportDocumentContent
' savecopy
ActiveDocument.SaveAs2 "C:\Users\binho\Desktop\Novapasta\Copia.docx"
errorExit:
On Error Resume Next
myDoc.Close False
Set myDoc = Nothing
Set wdApp = Nothing
Exit Sub
End Sub
she can only do that when the .docx and .xlsm are allocated directly on the local disk C: when I allocate them in some folder and try to repeat the following problem appears to me:
Sub mailmerge()
Call MergeRun("C:\Novapasta\2via\2v.docx", "C:\Novapasta\cup\Pastinha11.xlsm", "SELECT * FROM [Pastinha1$]")
End Sub
Sub MergeRun(frmFile As String, datFile As String, SQL As String)
Dim wdApp As Word.Application
Dim myDoc As Word.Document
On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Set wdApp = CreateObject("Word.Application")
End If
With wdApp
.Visible = True
'wdapp.application.DisplayAlerts=wdAlertsNone
'open word and apply datasource
Set myDoc = .Documents.Open(frmFile, False, False, False)
.ActiveDocument.MailMerge.MainDocumentType = wdFormLetters
.ActiveDocument.MailMerge.OpenDataSource Name:=datFile, _
ConfirmConversions:=False, ReadOnly:=False, LinkToSource:=False, _
AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:="", _
WritePasswordDocument:="", WritePasswordTemplate:="", Revert:=False, _
Format:=wdOpenFormatAuto, Connection:="", SQLStatement:=SQL, SQLStatement1 _
:="", SubType:=wdMergeSubTypeOther
'mail marge with document
With wdApp.ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = 1
.LastRecord = -16
End With
.Execute Pause:=False
End With
wdApp.Application.DisplayAlerts = wdAlertsNone
End With
' save as pdf
ActiveDocument.ExportAsFixedFormat OutputFileName:= _
"C:\Users\binho\Desktop\Novapasta\Inputpdf\doc.pdf", ExportFormat:=wdExportFormatPDF, _
OpenAfterExport:=True, OptimizeFor:=wdExportOptimizeForPrint, Range:= _
wdExportAllDocument, From:=1, To:=1, Item:=wdExportDocumentContent
' savecopy
ActiveDocument.SaveAs2 "C:\Users\binho\Desktop\Novapasta\Copia.docx"
errorExit:
On Error Resume Next
myDoc.Close False
Set myDoc = Nothing
Set wdApp = Nothing
Exit Sub
End Sub