Mr Ancovy
New member
- Local time
- Today, 15:35
- Joined
- Oct 26, 2023
- Messages
- 12
Hi all,
I am just at my wits end. Sorry I am quite a noob with this VBA and have been going round in circles for days now, scouring forums and asking for AIs help (that was painful).
I am trying to create a button in Access (Office 16 version) which:
- Mailmerges fields from my query into a .docx (where a yes / no field is unchecked)
- Saves each record as a pdf
- Saves each Pdf as "my text <Field1> - <Field 2>"
- After performing this routine, each unticked box becomes ticked, then also the date field is auto filled.
So far I have:
AFAIK at the moment my problem is
Sorry but I have no idea how to fix that. My brain is turning to sludge after trying 6 or so different methods / patching bits of code from different sources, all on a hiding to nothing.
I have created a new sample database and word doc with MM setup. I even notice this new one behaves differently against the old original one. Would I even need to "flush / clean / purge - I'm not sure the correct term some old "dirty residual" code somewhere somehow?
Hopefully I have provided enough info, but please feel free to ask anything and I will try my best to give anything else needed.
TIA
I am just at my wits end. Sorry I am quite a noob with this VBA and have been going round in circles for days now, scouring forums and asking for AIs help (that was painful).
I am trying to create a button in Access (Office 16 version) which:
- Mailmerges fields from my query into a .docx (where a yes / no field is unchecked)
- Saves each record as a pdf
- Saves each Pdf as "my text <Field1> - <Field 2>"
- After performing this routine, each unticked box becomes ticked, then also the date field is auto filled.
So far I have:
Code:
Private Sub RunQueryToPdfCmd_Click()
' Set the file paths for your data source and template
Dim dataSourcePath As String
Dim templatePath As String
Dim outputFolder As String
' Update these paths with your actual file locations
dataSourcePath = "C:\Users\Administrator\Downloads\Access Mailmerge Pdf VBA\New Microsoft Access Database.accdb"
templatePath = "C:\Users\Administrator\Downloads\Access Mailmerge Pdf VBA\New Microsoft Word Document.docx"
outputFolder = "C:\Users\Administrator\Downloads\Access Mailmerge Pdf VBA" ' Define the output folder
' Create a Word Application object
Dim objWord As Object
Set objWord = CreateObject("Word.Application")
' Open the Word template
objWord.Documents.Open templatePath
' Connect to the Access data source
objWord.MailMerge.OpenDataSource _
Name:=dataSourcePath, LinkToSource:=True, AddToRecentFiles:=False, _
Connection:="Provider=Microsoft.ACE.OLEDB.16.0;Data Source=" & dataSourcePath & ";", _
SQLStatement:="SELECT * FROM AgenciesToContactQ"
' Perform the mail merge
On Error Resume Next
objWord.ActiveDocument.MailMerge.Execute
If Err.Number <> 0 Then
' Handle the error here (e.g., display a message)
MsgBox "Error: " & Err.Description
Exit Sub
End If
On Error GoTo 0 ' Disable error handling
' Loop through the merged records
Do While objWord.ActiveDocument.MailMerge.DataSource.RecordCount > 0
' Customize the file name based on your field values
Dim fileName As String
fileName = outputFolder & objWord.ActiveDocument.MailMerge.DataSource.DataFields("Agency").Value & " - " & objWord.ActiveDocument.MailMerge.DataSource.DataFields("Branch").Value & ".pdf"
' Save the current record as a PDF
objWord.ActiveDocument.ExportAsFixedFormat OutputFileName:=fileName, ExportFormat:=17 ' 17 corresponds to PDF format
' Move to the next record
objWord.ActiveDocument.MailMerge.DataSource.ActiveRecord = wdNextRecord
Loop
' Close Word
objWord.Quit
Set objWord = Nothing
End Sub
AFAIK at the moment my problem is
Code:
' Move to the next record
objWord.ActiveDocument.MailMerge.DataSource.ActiveRecord = wdNextRecord
Sorry but I have no idea how to fix that. My brain is turning to sludge after trying 6 or so different methods / patching bits of code from different sources, all on a hiding to nothing.
I have created a new sample database and word doc with MM setup. I even notice this new one behaves differently against the old original one. Would I even need to "flush / clean / purge - I'm not sure the correct term some old "dirty residual" code somewhere somehow?
Hopefully I have provided enough info, but please feel free to ask anything and I will try my best to give anything else needed.
TIA