循环遍历PDF文件并使用word将其转换为doc

时间:2017-08-25 22:30:47

标签: vba pdf ms-word

我正在尝试使用VBA编码 - 我很新手 - 从PDF(非图像)获取一系列.doc文档,也就是说,我试图遍历各种PDF文件并保存它们以MS Word格式。我的经验是,这个单词很好地读取了我所拥有的PDF文档:word大部分时间都保持了PDF文件的正确布局。我不确定这是否是解决这个问题的正确选择,我要求另一个建议 - 如果可能的话,使用R。

无论如何,这里是我找到here的代码:

Sub convertToWord()

   Dim MyObj As Object, MySource As Object, file As Variant

   file = Dir("C:\Users\username\work_dir_example" & "*.pdf") 'pdf path

   Do While (file <> "")

   ChangeFileOpenDirectory "C:\Users\username\work_dir_example"

          Documents.Open Filename:=file, ConfirmConversions:=False, ReadOnly:= _
        False, AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:= _
        "", Revert:=False, WritePasswordDocument:="", WritePasswordTemplate:="", _
        Format:=wdOpenFormatAuto, XMLTransform:=""

    ChangeFileOpenDirectory "C:\Users\username\work_dir_example"

    ActiveDocument.SaveAs2 Filename:=Replace(file, ".pdf", ".docx"), FileFormat:=wdFormatXMLDocument _
        , LockComments:=False, Password:="", AddToRecentFiles:=True, _
        WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _
         SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:= _
        False, CompatibilityMode:=15

    ActiveDocument.Close

     file = Dir

   Loop

End Sub

在开发人员窗口中粘贴后,我将代码保存在模块中 - &gt;我关闭了开发人员的窗口 - &gt;我点击“宏”按钮 - &gt;我执行“convertToWord”宏。我在弹出框中收到以下错误:“Sub或Function not defined”。我该如何解决?此外,之前由于某些原因我现在还不清楚,我得到了与函数ChangeFileOpenDirectory相关的错误,似乎也没有定义。

2017年8月27日更新

我将代码更改为以下内容:

Sub convertToWord()

   Dim MyObj As Object, MySource As Object, file As Variant

   file = Dir("C:\Users\username\work_dir_example" & "*.pdf")

   ChDir "C:\Users\username\work_dir_example"

   Do While (file <> "")

        Documents.Open Filename:=file, ConfirmConversions:=False, ReadOnly:= _
        False, AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:= _
        "", Revert:=False, WritePasswordDocument:="", WritePasswordTemplate:="", _
        Format:=wdOpenFormatAuto, XMLTransform:=""

        ActiveDocument.SaveAs2 Filename:=Replace(file, ".pdf", ".docx"), FileFormat:=wdFormatXMLDocument _
        , LockComments:=False, Password:="", AddToRecentFiles:=True, _
        WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _
         SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:= _
        False, CompatibilityMode:=15

    ActiveDocument.Close

     file = Dir

   Loop

End Sub

现在我没有在弹出框中收到任何错误消息,但我的工作目录中没有输出。现在可能出现什么问题?

2 个答案:

答案 0 :(得分:3)

正如其他人所说,问题似乎主要在于路径&amp;文件名。这是您发布的代码的第二个版本,但有一些更改。

不幸的是,弹出一条警告消息并将DisplayAlerts设置为false将不会抑制它。但是,如果您在第一次弹出时单击“不再显示此消息”复选框,则不会继续弹出每个文件。

Sub convertToWord()

    Dim MyObj       As Object
    Dim MySource    As Object
    Dim file        As String
    Dim path        As String

    path = "C:\Users\username\work_dir_example\"
    file = Dir(path & "*.pdf")

    Do While (file <> "")
        Documents.Open FileName:=path & file
        With ActiveDocument
            .SaveAs2 FileName:=Replace(path & file, ".pdf", ".docx"), _
                                FileFormat:=wdFormatXMLDocument
            .Close
        End With
        file = Dir
    Loop

End Sub

答案 1 :(得分:3)

任何可以读取PDF文件和编写Word文档(XML)的语言都可以执行此操作,但您喜欢的转换(Word打开PDF时所执行的转换)将需要为应用程序本身使用API​​。 VBA是您的简单选择。

您发布的片段(以及下面的示例)使用早期绑定和枚举常量,这意味着我们需要对Word对象库的引用。这已经为您在Word文档中编写的任何代码设置了,因此创建一个新的Word文档并将代码添加到标准模块中。 (如果您需要更多详细信息,请参阅此Excel tutorial,我们的流程步骤相同)。

您可以从VB编辑器(使用“运行”按钮)或普通文档窗口运行宏(单击Word 2010-2016中“视图”选项卡上的“宏”按钮)。如果要在不重新设置代码的情况下重复使用宏,请将文档另存为DOCM文件。

现在代码!

如评论中所述,如果您只是确保文件夹路径以反斜杠“\”字符结尾,则您的第二个代码段有效。修复之后,它仍然不是很好的代码,但这会让你开始运行。

我假设您想要更加努力,并且有一个精心编写的版本,您可以重新调整或扩展。为简单起见,我们将使用两个过程:主转换和抑制PDF转换警告对话框的过程(由注册表控制)。

主要程序:

Sub ConvertPDFsToWord2()
    Dim path As String
    'Manually edit path in the next line before running
    path = "C:\users\username\work_dir_example\"

    Dim file As String
    Dim doc As Word.Document
    Dim regValPDF As Integer
    Dim originalAlertLevel As WdAlertLevel

'Generate string for getting all PDFs with Dir command
    'Check for terminal \
    If Right(path, 1) <> "\" Then path = path & "\"
    'Append file type with wildcard
    file = path & "*.pdf"

    'Get path for first PDF (blank string if no PDFs exist)
    file = Dir(file)

    originalAlertLevel = Application.DisplayAlerts
    Application.DisplayAlerts = wdAlertsNone

    If file <> "" Then regValPDF = TogglePDFWarning(1)

    Do While file <> ""
        'Open method will automatically convert PDF for editing
        Set doc = Documents.Open(path & file, False)

        'Save and close document
        doc.SaveAs2 path & Replace(file, ".pdf", ".docx"), _
                    fileformat:=wdFormatDocumentDefault
        doc.Close False

        'Get path for next PDF (blank string if no PDFs remain)
        file = Dir
    Loop

CleanUp:
    On Error Resume Next 'Ignore errors during cleanup
    doc.Close False
    'Restore registry value, if necessary
    If regValPDF <> 1 Then TogglePDFWarning regValPDF
    Application.DisplayAlerts = originalAlertLevel

End Sub

注册表设置功能:

Private Function TogglePDFWarning(newVal As Integer) As Integer
'This function reads and writes the registry value that controls
'the dialog displayed when Word opens (and converts) a PDF file
    Dim wShell As Object
    Dim regKey As String
    Dim regVal As Variant

    'setup shell object and string for key
    Set wShell = CreateObject("WScript.Shell")
    regKey = "HKCU\SOFTWARE\Microsoft\Office\" & _
             Application.Version & "\Word\Options\"

    'Get existing registry value, if any
    On Error Resume Next 'Ignore error if reg value does not exist
    regVal = wShell.RegRead(regKey & "DisableConvertPdfWarning")
    On Error GoTo 0      'Break on errors after this point

    wShell.regwrite regKey & "DisableConvertPdfWarning", newVal, "REG_DWORD"

    'Return original setting / registry value (0 if omitted)
    If Err.Number <> 0 Or regVal = 0 Then
        TogglePDFWarning = 0
    Else
        TogglePDFWarning = 1
    End If

End Function