Of course. The key challenge with your PDF is that it's an XFA (XML Forms Architecture) form, not a standard AcroForm.
The AFormAut.App library you are trying to use is designed for standard AcroForms and will not work for XFA forms. It will simply report that there are 0 fields.
To interact with XFA forms, you must use the JavaScript Object (JSObject), which you have correctly initialized. This allows you to run Acrobat's internal JavaScript, which has full access to the XFA data model.
Here is a revised and focused version of your code that correctly extracts the XFA field names and lists them in the "tempsht" worksheet.
The Solution
This code uses the JSObject to execute a single line of JavaScript: xfa.host.fieldNames().join('|||').
xfa.host.fieldNames(): This is the JavaScript command within Acrobat to get an array of all field names in an XFA form.
.join('|||'): This JavaScript method converts the array of names into a single string, with each name separated by the ||| delimiter.
- VBA then takes this single string and uses the
Split function to turn it back into a VBA array, which can then be easily written to the worksheet.
The Code
Replace your entire testingPDFManupulate subroutine with the code below.
Instructions:
- Ensure you have a worksheet in your workbook named exactly
tempsht.
- Ensure the Adobe Acrobat reference is enabled in VBA (
Tools -> References... -> Adobe Acrobat [version] Type Library).
- Copy and paste this code into your module.
- Update the
sOriFFPath and sDistinationFFPath if needed.
' It is highly recommended to add a reference to the Adobe Acrobat Type Library
' Go to Tools -> References -> and check "Adobe Acrobat [version] Type Library"
Public Sub ListXfaFormFields()
Dim ws As Worksheet
Dim sOriFFPath As String
Dim sDistinationFFPath As String
Dim xApp As Acrobat.AcroApp
Dim xPDDoc As Acrobat.AcroPDDoc
Dim oJs As Object
Dim sAllFieldNames As String
Dim vFieldNames As Variant
Dim i As Long
' --- 1. SETUP ---
' Set the target worksheet. Create it if it doesn't exist.
On Error Resume Next
Set ws = ThisWorkbook.Worksheets("tempsht")
If ws Is Nothing Then
Set ws = ThisWorkbook.Worksheets.Add
ws.Name = "tempsht"
End If
On Error GoTo 0
ws.Cells.ClearContents ' Clear previous results
ws.Range("A1").Value = "XFA Form Field Name"
ws.Range("A1").Font.Bold = True
' Define file paths
sOriFFPath = "D:\Root Parent\Developing templates\Bank confirmation form\HSBC.pdf"
sDistinationFFPath = "D:\Root Parent\Production\WIP1 - Preliminary\Mega Honest International Trading Limited^Statutory audit@SME&190101-191231=10000\Doc issued\Testing.pdf"
' It's safer to work on a copy
On Error Resume Next
Kill sDistinationFFPath
On Error GoTo 0
copyFile sOriFFPath, sDistinationFFPath
' --- 2. INITIALIZE ACROBAT ---
' Use GetObject first in case Acrobat is already running
On Error Resume Next
Set xApp = GetObject(, "AcroExch.App")
If xApp Is Nothing Then
Set xApp = CreateObject("AcroExch.App")
End If
On Error GoTo 0
' We only need the PDDoc for this, not the visible AVDoc
Set xPDDoc = CreateObject("AcroExch.PDDoc")
If Not xPDDoc.Open(sDistinationFFPath) Then
MsgBox "Failed to open the PDF file at: " & vbCrLf & sDistinationFFPath, vbCritical, "File Open Error"
Set xApp = Nothing
Exit Sub
End If
' --- 3. GET FIELD NAMES USING JAVASCRIPT OBJECT ---
' This is the critical part for XFA forms.
' AFormAut will NOT work. We must use the JSObject.
Set oJs = xPDDoc.GetJSObject
If oJs Is Nothing Then
MsgBox "Could not get the JavaScript Object from the PDF. The file might be corrupt or secured.", vbCritical, "JSObject Error"
xPDDoc.Close
Set xPDDoc = Nothing
Set xApp = Nothing
Exit Sub
End If
' Execute a JS command to get all field names as a single, delimited string
' We use a unique delimiter '|||' to separate the names.
sAllFieldNames = oJs.eval("xfa.host.fieldNames().join('|||')")
' --- 4. PROCESS AND WRITE TO EXCEL ---
If Len(sAllFieldNames) > 0 Then
' Split the single string back into an array using our delimiter
vFieldNames = Split(sAllFieldNames, "|||")
' Loop through the array and write each field name to the worksheet
For i = LBound(vFieldNames) To UBound(vFieldNames)
ws.Cells(i + 2, 1).Value = vFieldNames(i)
Next i
ws.Columns("A").AutoFit
MsgBox "Found and listed " & UBound(vFieldNames) + 1 & " XFA fields.", vbInformation, "Success"
Else
MsgBox "No XFA form fields were found in the document.", vbInformation, "No Fields Found"
End If
' --- 5. CLEANUP ---
xPDDoc.Close
'xApp.Exit ' Uncomment this if you want to close the Acrobat application itself
Set oJs = Nothing
Set xPDDoc = Nothing
Set xApp = Nothing
Set ws = Nothing
End Sub
' You need this helper function if it's not already in your project
Private Sub copyFile(sourcePath As String, destinationPath As String)
Dim FSO As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
If Not FSO.FileExists(sourcePath) Then
MsgBox "Source file not found: " & sourcePath, vbExclamation
Exit Sub
End If
FSO.copyFile sourcePath, destinationPath, True ' True = Overwrite if exists
Set FSO = Nothing
End Sub