Reputation: 55
I was trying to run a vba to convert multiple pdf file in a folder into txt file in another folder. however, I encountered the following error which I was not able to resolve.
The error is at Set jsObj = AcroXPDDoc.GetJSObject which gave me a runtime error 91: "Object variable or With block variable not set"
Can anyone help? Thanks.
:Updated with new code
Option Explicit
Sub convertpdf5()
Dim AcroXApp As Acrobat.acroApp
Dim AcroXAVDoc As Acrobat.AcroAVDoc
Dim AcroXPDDoc As Acrobat.AcroPDDoc
Dim Filename As Variant
Dim jsObj As Object
Dim sfolder As String
Dim dfolder As String
Dim spath As String
Dim dpath As String
Dim SFilename As Variant
Dim DFilename As Variant
Dim objFolder As folder
Dim objFile As file
Dim NextRow As Long
sfolder = "C:\users\chanhc\desktop\test folder\"
spath = sfolder & "*.pdf"
SFilename = Dir(spath)
dfolder = "C:\users\chanhc\desktop\test folder after\"
dpath = dfolder & "*.txt"
DFilename = Dir(dpath)
'Creating a FileSystemObject
Dim fso As New FileSystemObject
'Specify the path of the folder
'Create the object of this folder
Set objFolder = fso.GetFolder(sfolder)
'Check if the folder is empty or not
If objFolder.Files.count = 0 Then
MsgBox "No files were found...", vbExclamation
Exit Sub
End If
NextRow = ActiveSheet.Cells(Rows.count, "A").End(xlUp).Row + 1
For Each objFile In objFolder.Files
Cells(NextRow, 1).Value = sfolder & objFile.Name
NextRow = NextRow + 1
Next objFile
For Each Filename In Sheet1.Range("a2:a4")
Set AcroXApp = CreateObject("AcroExch.App")
'AcroXApp.Show
Set AcroXAVDoc = CreateObject("AcroExch.AVDoc")
AcroXAVDoc.Open Filename, "Acrobat"
Set AcroXPDDoc = AcroXAVDoc.GetPDDoc
Set jsObj = AcroXPDDoc.GetJSObject
jsObj.SaveAs DFilename, "com.adobe.acrobat.plain-text"
AcroXAVDoc.Close False
AcroXApp.Hide
AcroXApp.Exit
Next Filename
End Sub
Upvotes: 1
Views: 6734
Reputation: 21639
I think I'm starting to understand your problem and it has nothing to do with Adobe. Your code to list the files is not working properly.
For one thing, you're combining 2 different methods of listing files. (Dir
and FileSystemObject
s are not used together.
Also why are you listing the files on the sheet and then getting the name from the sheet?
I mistakenly assumed that you had followed the steps in the FAQ about postings example. A Minimal, Complete, and Verifiable example.
When you're having a problem with a section if your code, you need to Start Over From Scratch
Create a new program, adding in only what is needed to see the problem. This can be faster for vast systems where you think you already know the source of the problem.
So, first see if your code to convert the PDF works BY ITSELF:
Sub ONLYConvertPDF()
Dim AcroXApp As Acrobat.acroApp
Dim AcroXAVDoc As Acrobat.AcroAVDoc
Dim AcroXPDDoc As Acrobat.AcroPDDoc
Dim Filename As String, DFilename As String, jsObj As Object
Filename = "C:\users\chanhc\desktop\test folder\__ENTER_FILENAME__.PDF" '<<ENTER A FILEMNAME HERE
DFilename = "C:\users\chanhc\desktop\test folder after\TEST_OUTPUT_FILE.TXT"
Set AcroXApp = CreateObject("AcroExch.App")
AcroXApp.Show
Set AcroXAVDoc = CreateObject("AcroExch.AVDoc")
AcroXAVDoc.Open Filename, "Acrobat"
Set AcroXPDDoc = AcroXAVDoc.GetPDDoc
Set jsObj = AcroXPDDoc.GetJSObject
jsObj.SaveAs DFilename, "com.adobe.acrobat.plain-text"
AcroXAVDoc.Close False
AcroXApp.Hide
AcroXApp.Exit
End Sub
Replace the string to give FileName
the name of one existing file. Run the code. Does it work? Did it convert the file?
Besides taking out all code that wasn't for converting the PDF, I also uncommented
AcroXApp.Show
... You should never hide something you're troubleshooting! Also, I'm not sure why you were usingVariant
forString
s but I changed those too.
Once the above works fine by itself then we work on troubleshooting the next step: looping through the files one at a time.
Instead of trying to explain what what wrong with that section of your code, I wrote you a simpler procedure to loop through the source folder, return all the filenames along with what the destination filename will be.
It doesn't convert anything, it just pretends to for now.
Option Explicit
Sub TEST_ListFiles()
Const sPath = "C:\users\chanhc\desktop\test folder\"
Const sExt = ".pdf"
Const dPath = "C:\users\chanhc\desktop\test folder after\"
Const dExt = ".txt"
Dim sName As String, dName As String, fCount As Long
'loop through all files in source
sName = Dir(sPath & "*" & sExt)
Do While sName <> ""
fCount = fCount + 1
'we have sName. Now figure out dName
dName = Left(sName, InStrRev(sName, ".") - 1) & dExt
'This will be the spot where you convert the PDF's to text, but NOT UNTIL
'this code lists all the files properly, we can add it the other code to
'actually convert the PDF's (which we know is working since we tested is BY ITSELF.)
'For TESTING we will ONLY show the name is a msgbox:
MsgBox "Converting PDF File #" & fCount & ": " & vbLf & _
sPath & sName & " -> " & dPath & dName
'find the next file
sName = Dir
Loop
MsgBox "Found " & fCount & " files."
End Sub
Once that section functions properly by itself, you can add the two pieces of code together.
Once the above procedure is properly listing the source and destination, we can think about putting them together.
I think, that actually, in order to keep these nice, neat procedures separate, let's change the first one like this:
It will be stand-alone, and take the parameters for sFile & dFile, and we can call it each time we want to convert a file.
Sub ConvertOnePDF(sFile As String, dFile As String)
Dim AcroXApp As Acrobat.acroApp, AcroXAVDoc As Acrobat.AcroAVDoc
Dim AcroXPDDoc As Acrobat.AcroPDDoc, jsObj As Object
Set AcroXApp = CreateObject("AcroExch.App")
'AcroXApp.Show
Set AcroXAVDoc = CreateObject("AcroExch.AVDoc")
AcroXAVDoc.Open sFile, "Acrobat"
Set AcroXPDDoc = AcroXAVDoc.GetPDDoc
Set jsObj = AcroXPDDoc.GetJSObject
jsObj.SaveAs dFile, "com.adobe.acrobat.plain-text"
AcroXAVDoc.Close False
AcroXApp.Hide
AcroXApp.Exit
End Sub
Once you have the two procedures working , you can add this under the MsgBox in Test_Listfiles
:
ConvertOnePDF sName, dName
and that should be it!
Upvotes: 1