VBA将文件夹中的多个PDF转换为文本文件 [英] vba converting multiple pdf in folder to text file

查看:426
本文介绍了VBA将文件夹中的多个PDF转换为文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图运行vba将一个文件夹中的多个pdf文件转换为另一个文件夹中的txt文件.但是,我遇到以下无法解决的错误.

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.

错误发生在Set jsObj = AcroXPDDoc.GetJSObject,这给了我一个运行时错误91:对象变量或With块变量未设置"

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.

:已使用新代码更新

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

推荐答案

我认为我已经开始了解您的问题了,它与Adobe无关.您列出文件的代码无法正常工作.

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.

一方面,您要结合两种不同的文件列表方法. (DirFileSystemObject不能一起使用.

For one thing, you're combining 2 different methods of listing files. (Dir and FileSystemObjects are not used together.

为什么还要在工作表中列出文件,然后从工作表中获取名称?

Also why are you listing the files on the sheet and then getting the name from the sheet?

我错误地认为您已按照FAQ中有关发帖示例的步骤进行操作. 最小,完整和可验证的示例.

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.

因此,首先查看您自己的转换PDF的代码是否有效:

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

替换字符串,为FileName提供一个现有文件的名称.运行代码.它行得通吗?它转换了文件吗?

Replace the string to give FileName the name of one existing file. Run the code. Does it work? Did it convert the file?

除了删除所有非用于转换PDF的代码外,我也没有发表评论.AcroXApp.Show ...您应该从不隐藏任何您要进行故障排除的内容!另外,我不确定您为什么要在String上使用Variant,但是我也更改了它们.

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 using Variant for Strings 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.

它什么都不会转换,只是暂时假装成.

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:

它将是独立的,并采用sFile& dFile,每次我们要转换文件时都可以调用它.

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


一旦两个程序都可以工作,就可以将其添加到Test_Listfiles中的MsgBox下:


Once you have the two procedures working , you can add this under the MsgBox in Test_Listfiles:

ConvertOnePDF sName, dName

应该就是这样!

这篇关于VBA将文件夹中的多个PDF转换为文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆