我试图运行vba将文件夹中的多个pdf文件转换为另一个文件夹中的txt文件。但是,我遇到了以下错误,我无法解决。
错误发生在Set jsObj = AcroXPDDoc.GetJSObject,它给了我一个运行时错误91:“对象变量或没有设置块变量”
有人可以帮忙吗? 感谢。
:更新了新代码
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
答案 0 :(得分:0)
我想我已经开始理解你的问题而且它与Adobe无关。列出文件的代码无法正常工作。
首先,您要结合使用两种不同的列出文件的方法。 (Dir
和FileSystemObject
不会一起使用。
另外,为什么要在工作表上列出文件,然后从工作表中获取名称?
我错误地认为你已经跟随了steps in the FAQ about postings example。 最小,完整且可验证的示例。
当您的代码出现问题时,您需要从头开始
创建一个新程序,仅添加查看问题所需的内容。对于您认为已经知道问题根源的大型系统,这可能会更快。
那么,首先看看你的代码转换PDF是否有效地工作:
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
替换字符串以向FileName
提供一个现有文件的名称。运行代码。它有用吗?它转换了文件吗?
除了取出所有不用于转换PDF的代码外,我还取消注释
AcroXApp.Show
...你应该永远隐藏你正在排除故障的内容!此外,我不确定您为Variant
使用String
的原因,但我也更改了这些内容。
一旦上述工作正常,我们就会对下一步的故障排除工作:一次循环一个文件。
我没有试图解释你的代码部分有什么问题,而是给你写了一个更简单的程序来循环遍历源文件夹,返回所有文件名以及目标文件名。
它不会转换任何内容,它现在只是假装。
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
一旦该部分本身正常运行,您可以将两段代码一起添加。
如果上述程序正确列出了来源和目的地,我们可以 思考 将它们放在一起。
我想,实际上,为了保持这些漂亮,整洁的程序分开,让我们改变第一个这样的:
它将是独立的,并采用sFile&amp;的参数。 dFile,我们每次想要转换文件时都可以调用它。
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
一旦你有两个程序工作,你可以在Test_Listfiles
的MsgBox下添加它:
ConvertOnePDF sName, dName
那应该是它!