MatthewBiersay
New Member
- Joined
- Nov 24, 2024
- Messages
- 5
- Office Version
- Prefer Not To Say
- Platform
- Windows
Hello all!
Been trying to figure out this code to do the following:
Save an open pdf file by searching for text within that pdf that matches text in column g, once this match is found it should save the already open pdf file using the adjacent cell in column R. Tried to use co-pilot but pdf won't save:
This is the code below, all help would be appreciated, or alternative ideas. Thank you!
Sub SearchTextInPDFAndSaveToDesktop()
Dim AcroApp As Object
Dim AcroAVDoc As Object
Dim AcroPDDoc As Object
Dim jsObj As Object
Dim ws As Worksheet
Dim searchText As String
Dim pdfName As String
Dim folderPath As String
Dim cell As Range
Dim pdfText As String
Dim pageNum As Integer
Dim wordNum As Integer
On Error GoTo ErrorHandler
' Set the worksheet and search range
Set ws = ThisWorkbook.Sheets("APUpload") ' Change to your sheet name
' Set the folder path to the desktop
folderPath = Environ("USERPROFILE") & "\Desktop\" ' Path to the desktop
' Create Acrobat application object
Set AcroApp = CreateObject("AcroExch.App")
Set AcroAVDoc = AcroApp.GetActiveDoc
' Check if there is an active PDF document
If Not AcroAVDoc Is Nothing Then
Set AcroPDDoc = AcroAVDoc.GetPDDoc
Set jsObj = AcroPDDoc.GetJSObject
' Loop through each cell in column G to find the text in the PDF
For Each cell In ws.Range("G1:G100") ' Adjust the range as needed
searchText = cell.Value
Debug.Print "Search Text: " & searchText
' Loop through pages and words to find the search text
For pageNum = 0 To AcroPDDoc.GetNumPages - 1
For wordNum = 0 To jsObj.getPageNumWords(pageNum) - 1
pdfText = jsObj.getPageNthWord(pageNum, wordNum, True)
If pdfText = searchText Then
' Get the PDF name from the cell in column R adjacent to the found text
pdfName = cell.Offset(0, 11).Value & ".pdf" ' Column R is 11 columns to the right of column G
Debug.Print "PDF Name: " & pdfName
' Save the active PDF with the new name
AcroPDDoc.Save 1, folderPath & pdfName
MsgBox "PDF has been saved successfully as " & pdfName
Exit Sub
End If
Next wordNum
Next pageNum
Next cell
MsgBox "Text not found in the PDF."
Else
MsgBox "No active PDF document found."
End If
' Clean up
Set jsObj = Nothing
Set AcroPDDoc = Nothing
Set AcroAVDoc = Nothing
Set AcroApp = Nothing
Exit Sub
ErrorHandler:
MsgBox "An error occurred: " & Err.Description
' Clean up
Set jsObj = Nothing
Set AcroPDDoc = Nothing
Set AcroAVDoc = Nothing
Set AcroApp = Nothing
End Sub
Been trying to figure out this code to do the following:
Save an open pdf file by searching for text within that pdf that matches text in column g, once this match is found it should save the already open pdf file using the adjacent cell in column R. Tried to use co-pilot but pdf won't save:
This is the code below, all help would be appreciated, or alternative ideas. Thank you!
Sub SearchTextInPDFAndSaveToDesktop()
Dim AcroApp As Object
Dim AcroAVDoc As Object
Dim AcroPDDoc As Object
Dim jsObj As Object
Dim ws As Worksheet
Dim searchText As String
Dim pdfName As String
Dim folderPath As String
Dim cell As Range
Dim pdfText As String
Dim pageNum As Integer
Dim wordNum As Integer
On Error GoTo ErrorHandler
' Set the worksheet and search range
Set ws = ThisWorkbook.Sheets("APUpload") ' Change to your sheet name
' Set the folder path to the desktop
folderPath = Environ("USERPROFILE") & "\Desktop\" ' Path to the desktop
' Create Acrobat application object
Set AcroApp = CreateObject("AcroExch.App")
Set AcroAVDoc = AcroApp.GetActiveDoc
' Check if there is an active PDF document
If Not AcroAVDoc Is Nothing Then
Set AcroPDDoc = AcroAVDoc.GetPDDoc
Set jsObj = AcroPDDoc.GetJSObject
' Loop through each cell in column G to find the text in the PDF
For Each cell In ws.Range("G1:G100") ' Adjust the range as needed
searchText = cell.Value
Debug.Print "Search Text: " & searchText
' Loop through pages and words to find the search text
For pageNum = 0 To AcroPDDoc.GetNumPages - 1
For wordNum = 0 To jsObj.getPageNumWords(pageNum) - 1
pdfText = jsObj.getPageNthWord(pageNum, wordNum, True)
If pdfText = searchText Then
' Get the PDF name from the cell in column R adjacent to the found text
pdfName = cell.Offset(0, 11).Value & ".pdf" ' Column R is 11 columns to the right of column G
Debug.Print "PDF Name: " & pdfName
' Save the active PDF with the new name
AcroPDDoc.Save 1, folderPath & pdfName
MsgBox "PDF has been saved successfully as " & pdfName
Exit Sub
End If
Next wordNum
Next pageNum
Next cell
MsgBox "Text not found in the PDF."
Else
MsgBox "No active PDF document found."
End If
' Clean up
Set jsObj = Nothing
Set AcroPDDoc = Nothing
Set AcroAVDoc = Nothing
Set AcroApp = Nothing
Exit Sub
ErrorHandler:
MsgBox "An error occurred: " & Err.Description
' Clean up
Set jsObj = Nothing
Set AcroPDDoc = Nothing
Set AcroAVDoc = Nothing
Set AcroApp = Nothing
End Sub