MAIL MERGE EXCEL + WORD + VBA DIRECTORY PROBLEM

Cleitonis

New Member
Joined
Sep 3, 2023
Messages
2
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
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:
error.png


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
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
VBA Code:
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
 
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,114
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top