killMessageFilter
Dim vv
Dim sOriFFPath As String
sOriFFPath = "D:\Root Parent\Developing templates\Bank confirmation form\HSBC.pdf"
Dim sDistinationFFPath As String
sDistinationFFPath = "D:\Root Parent\Production\WIP1 - Preliminary\Mega Honest International Trading Limited^Statutory audit@SME&190101-191231=10000\Doc issued\Testing.pdf"
copyFile sOriFFPath, sDistinationFFPath
'fields[0].page1[0].sfCustomerDetails[0].txtCustomerAccName[0]
Dim xApp As Acrobat.AcroApp
Dim xAvDoc As Acrobat.AcroAVDoc
Dim xPDDoc As Acrobat.AcroPDDoc
Dim oJs As Object
Dim xAForm As AFORMAUTLib.AFormApp
Dim xTotalFields As AFORMAUTLib.Fields
Dim lFieldsCount As Long
Dim oHighlightList As Object
Dim lPageCount As Long
Dim xTempField As AFORMAUTLib.Field
Dim oPage As Object
Dim oPageRect As Object
Dim xSelectedText As CAcroPDTextSelect
Dim lSelectedTextLength As Long
Dim oAnnot As Object
Dim sTempAnnotName As String
Dim sTempAnnotType As String
Dim lTempAnnotPage As Long
Dim oProps As Object
Dim lAnnotRectX As Long
Dim lAnnotRectY As Long
Dim lAnnotRectH As Long
Dim lAnnotRectW As Long
Dim vJSCode As Variant
Dim sJSCode As String
Dim bSave As Boolean
Dim oAnnotDict As New Dictionary
Set xApp = CreateObject("AcroExch.App")
Set xAvDoc = CreateObject("AcroExch.AVDoc")
If xAvDoc.Open(sDistinationFFPath, "Opened by VBA") = False Then
LOG "The file cannot be found"
Exit Sub '//If cannot open file, exit
End If
Set xPDDoc = CreateObject("AcroExch.PDDoc")
xPDDoc.Open (sDistinationFFPath)
Set oJs = xPDDoc.GetJSObject
Set xAForm = CreateObject("AFormAut.App")
Set xTotalFields = xAForm.Fields
lFieldsCount = xTotalFields.Count
Public Sub testingPDFManupulate()
killMessageFilter
Dim sOriFFPath As String
sOriFFPath = "D:\Root Parent\Developing templates\Bank confirmation form\HSBC.pdf"
Dim sDistinationFFPath As String
sDistinationFFPath = "D:\Root Parent\Production\WIP1 - Preliminary\Mega Honest International Trading Limited^Statutory audit@SME&190101-191231=10000\Doc issued\Testing.pdf"
copyFile sOriFFPath, sDistinationFFPath
Dim xApp As Acrobat.AcroApp
Dim xAvDoc As Acrobat.AcroAVDoc
Dim xPDDoc As Acrobat.AcroPDDoc
Dim oJs As Object
Dim ws As Worksheet
Dim rowNum As Long
' Create or get the worksheet
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
' Clear existing data and add headers
ws.Cells.Clear
ws.Range("A1").Value = "Field Name"
ws.Range("B1").Value = "Field Type"
ws.Range("C1").Value = "Full Path"
rowNum = 2
Set xApp = CreateObject("AcroExch.App")
Set xAvDoc = CreateObject("AcroExch.AVDoc")
If xAvDoc.Open(sDistinationFFPath, "Opened by VBA") = False Then
LOG "The file cannot be found"
Exit Sub
End If
Set xPDDoc = xAvDoc.GetPDDoc
Set oJs = xPDDoc.GetJSObject
' Check if it's an XFA form
If Not oJs Is Nothing Then
If Not oJs.xfa Is Nothing Then
' Get XFA field names using JavaScript
Dim jsCode As String
Dim fieldList As Variant
Dim i As Long
' JavaScript code to traverse XFA form and get field names
jsCode = "var fieldNames = [];" & vbCrLf & _
"function traverseXFA(node, path) {" & vbCrLf & _
" if (node.className == 'field') {" & vbCrLf & _
" var fullPath = path ? path + '.' + node.name : node.name;" & vbCrLf & _
" fieldNames.push({name: node.name, type: node.ui.oneOfChild.className, fullPath: fullPath});" & vbCrLf & _
" }" & vbCrLf & _
" if (node.nodes) {" & vbCrLf & _
" for (var i = 0; i < node.nodes.length; i++) {" & vbCrLf & _
" var childPath = path ? path + '.' + node.name : node.name;" & vbCrLf & _
" traverseXFA(node.nodes.item(i), childPath);" & vbCrLf & _
" }" & vbCrLf & _
" }" & vbCrLf & _
"}" & vbCrLf & _
"traverseXFA(xfa.form, '');" & vbCrLf & _
"fieldNames;"
' Execute JavaScript and get results
On Error Resume Next
fieldList = oJs.eval(jsCode)
On Error GoTo 0
' Process the results
If IsArray(fieldList) Then
For i = 0 To UBound(fieldList)
If Not IsEmpty(fieldList(i)) Then
ws.Cells(rowNum, 1).Value = fieldList(i).Name
ws.Cells(rowNum, 2).Value = fieldList(i).Type
ws.Cells(rowNum, 3).Value = fieldList(i).fullPath
rowNum = rowNum + 1
End If
Next i
End If
' Alternative method - get field names directly
If rowNum = 2 Then ' If no fields found with the above method
Dim xfaFields As Object
Dim fieldName As String
' Try to get fields using resolveNodes
jsCode = "var result = [];" & vbCrLf & _
"var fields = xfa.resolveNodes('$form..field[*]');" & vbCrLf & _
"if (fields) {" & vbCrLf & _
" for (var i = 0; i < fields.length; i++) {" & vbCrLf & _
" result.push(fields.item(i).somExpression);" & vbCrLf & _
" }" & vbCrLf & _
"}" & vbCrLf & _
"result;"
On Error Resume Next
fieldList = oJs.eval(jsCode)
On Error GoTo 0
If IsArray(fieldList) Then
For i = 0 To UBound(fieldList)
ws.Cells(rowNum, 1).Value = fieldList(i)
ws.Cells(rowNum, 3).Value = fieldList(i)
rowNum = rowNum + 1
Next i
End If
End If
End If
End If
' Format the worksheet
ws.Columns("A:C").AutoFit
ws.Range("A1:C1").Font.Bold = True
' Clean up
xPDDoc.Close
xAvDoc.Close True
xApp.Exit
Set oJs = Nothing
Set xPDDoc = Nothing
Set xAvDoc = Nothing
Set xApp = Nothing
MsgBox "Field extraction complete. Found " & (rowNum - 2) & " fields.", vbInformation
End Sub