So I have a PDF document and I want to be able to automate the import the XML files in a folder into PDF documents, e.g., 5 XML files = save as 5 PDF documents. However, the problem is, there is a problem: my current VBA code forces me to "Select Data Containing Form Data" instead of selecting the files in order itself. I've pieced together some VBA code to achieve more or less what I want, except that the "jso.importXFAData myFile is not automating as I need to manually select the files (e.g., if I have two files in my folder, I would have to select the first file, then the VBA loops, and I would then have to select the second file):
Sub xml_to_pdf()
Dim AcroApp As New AcroApp
Dim theForma As New AcroAVDoc
Dim theform As AcroPDDoc
Dim jso As Object
Dim text1, text2 As String
Dim f As Object
Dim sig As Object
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog
Dim StrFile As String
Dim wb As Workbook
'Optimize Macro Speed
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
'Retrieve Target Folder Path From User
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
With FldrPicker
.Title = "Select A Target Folder"
.AllowMultiSelect = False
If .Show <> -1 Then GoTo NextCode
myPath = .SelectedItems(1) & "\"
End With
'In Case of Cancel
NextCode:
myPath = myPath
If myPath = "" Then GoTo ResetSettings
'Target File Extension (must include wildcard "*")
myExtenion = "*.xml"
'Target Path with Ending Extension
myFile = Dir(myPath & myExtension)
'Loop
Do While myFile <> ""
'Open PDF file
theForma.Open "PDFfile.pdf", "" 'PDF Template that accepts xml
Set theform = theForma.GetPDDoc
Set jso = theform.GetJSObject
'Select Import
jso.importXFAData myFile
'Save form
i = i + 1
theform.Save 1, "//FormPath" & "Form" & i & ".pdf"
theForma.Close True
'Get next file name
myFile = Dir()
Loop
'Message Box when tasks are completed
MsgBox "Task Complete!"
ResetSettings:
'Reset Macro Optimization Settings
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
Please let me know what I can do to revise this code!
Bookmarks