gregtgonzalez
New Member
- Joined
- Dec 16, 2016
- Messages
- 29
Hello All-
So i have been trying to think of how i would go about accomplishing my enhancement to my code. Essentially the code allows for a custom mail merge, that then creates a .pdf file. However, what i want to be able to do is make it so the user can be prompted at the beginning of the macro to define where they want the file to go to
I want this section to be dynamic, so the user can determine if they want to save to "genericfilename1" or "generic filename1" or if the user has the ability to just specify where they want to store the resulting document.
any coaching would be greatly appreciated as i am still trying to understand VBA sytax when writing for a process. Thank you!
So i have been trying to think of how i would go about accomplishing my enhancement to my code. Essentially the code allows for a custom mail merge, that then creates a .pdf file. However, what i want to be able to do is make it so the user can be prompted at the beginning of the macro to define where they want the file to go to
VBA Code:
DocResult.SaveAs "P:\GenericFilename" & sFirmFileName & ".pdf", wdFormatPDF
' Path and File Name to save. can use other formats like wdFormatPDF too
DocResult.Close False
VBA Code:
Dim WithEvents wdapp As Application
Dim bCustomProcessing As Boolean
Private Sub Document_Open()
Set wdapp = Application
bCustomProcessing = False
ThisDocument.MailMerge.DataSource.ActiveRecord = 1
ThisDocument.MailMerge.ShowWizard 1
With ActiveDocument.MailMerge
If .MainDocumentType = wdFormLetters Then
.ShowSendToCustom = "Custom Letter Processing"
End If
End With
End Sub
Private Sub wdapp_MailMergeWizardSendToCustom(ByVal Doc As Document)
bCustomProcessing = True
Doc.MailMerge.Destination = wdSendToNewDocument
With Doc.MailMerge
For rec = 1 To .DataSource.RecordCount
.DataSource.ActiveRecord = rec
.DataSource.FirstRecord = rec
.DataSource.LastRecord = rec
.Execute
Next
End With
MsgBox "Merge Finished"
End Sub
Private Sub wdapp_MailMergeAfterMerge(ByVal Doc As Document, ByVal DocResult As Document)
If bCustomProcessing = True Then
With Doc.MailMerge.DataSource.DataFields
sFirmFileName = .Item(8).Value & .Item(9) & "-" & .Item(1).Value ' First Column of the data - CHANGE
End With
DocResult.SaveAs "P:\Genericfilename" & sFirmFileName & ".pdf", wdFormatPDF
' Path and File Name to save. can use other formats like wdFormatPDF too
DocResult.Close False
End If
End Sub
any coaching would be greatly appreciated as i am still trying to understand VBA sytax when writing for a process. Thank you!