当前位置: 首页 > 知识库问答 >
问题:

通过MS-Word打开pdf文件

吴靖
2023-03-14

我试图通过MS Word打开pdf文件,执行某些操作,例如评估计算、打印文件等,然后继续关闭文件。我收到的错误消息是“Microsoft Excel正在等待另一个应用程序完成OLE操作。”

我之前尝试过超链接跟踪和Shell MyPath

Sub Extract_PDF_Data()

Dim mainData As String
Dim strFile As String
Dim Oldname As String
Dim Newname As String
Dim Folderpath As String

Dim s As String
Dim t As Excel.Range
Dim wd As New Word.Application
Dim mydoc As Word.Document



Folderpath = InputBox("Folder path: ")
Folderpath = Folderpath & "\"
strFile = Dir(Folderpath & "", vbNormal)


Do While Len(strFile) > 0
Oldname = Folderpath & strFile
Set wd = CreateObject("Word.Application")
Set mydoc = Word.Documents.Open(Filename:=Oldname, Format:="PDF Files", 
ConfirmConversions:=False)

mainData = mydoc.Content.Text
mydoc.Close False
wd.Quit


strFile = Dir
Loop

End Sub

共有2个答案

端木鹏
2023-03-14

我认为这三个来源的结合将得出答案:

如何使用Excel打开pdf?

如何使用VBA从pdf中提取数据

如何使用VBA打开和打印pdf?

我认为会是这样:

Sub Extract_PDF_Data()

Dim mainData As String
Dim strFile As String
Dim Oldname As String
Dim Newname As String
Dim Folderpath As String
Dim s As String
Dim t As Excel.Range
Dim Appshell As Variant
Dim ap As String
Dim Browsedir As Variant
Dim f As Variant
Dim KeyWord As String

' This is a suggestion, I use it because it is more convenient than copy-pasting folder paths
Dim FSO As Object
Set FSO = CreateObject("Scripting.Filesystemobject")

 ' Get Folder over user input
Set Appshell = CreateObject("Shell.Application")
Set Browsedir = Appshell.BrowseForFolder(0, "Select a Folder", &H1000, "E:\Xample\Path")

' check if not cancalled
If Not Browsedir Is Nothing Then
      Folderpath = Browsedir.items().Item().Path
Else
    GoTo Quit
End If

KeyWord = "The_Materialist_Example"

' go through all files in the folder
For Each f In FSO.GetFolder(Folderpath).Files
    ' if file is a pdf , open, check for keyword, decide if should be printed
    If LCase(Right(f.Name, 3)) = "pdf" Then

        ' Here the methods suggest different answers.
        ' You can either use FollowHyperLink or use the Adobe Library to OPEN PDF

        ' I would write a function that checks the active pdf for the keyword : IsKeyFound
        Debug.Print Folderpath & "\" & f.Name

        Call PrintPDF(Folderpath & "\" & f.Name)
        If IsKeyFound(f, KeyWord) Then
            f.Print
        End If
    End If
Next f

Quit:
End Sub

Private Sub PrintPDF(strPDFFileName As String)
  Dim sAdobeReader As String 'This is the full path to the Adobe Reader or Acrobat application on your computer
  Dim RetVal As Variant
  sAdobeReader = "C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe"
  'Debug.Print sAdobeReader & "/P" & Chr(34) & strPDFFileName & Chr(34)
  RetVal = Shell(sAdobeReader & " /P " & Chr(34) & strPDFFileName & Chr(34), 0)
End Sub


Private Function IsKeyFound(PDF As Variant, KeyWord As String) As Boolean
   'Decide if file needs to be printed, insert your criteria and search algorithm here
End Function

我无法弄清楚如何提取关键字,但是您可以使用用户输入作为第一种方法,然后继续自动扫描pdf。

我希望这能让你在解决问题的道路上走得更远。

戚森
2023-03-14

不要在声明对象变量的行中使用New关键字。这将“阻止”对象变量-当代码延迟尝试实例化它时会导致错误。此方法可以在VB. NET中使用,但不能在VBA中使用。

更像这样做:

Dim wd As Word.Application
Set wd = New Word.Application. 'Or use CreateObject
 类似资料:
  • 我在sdcard中的某个文件夹中有一些pdf文件。我创建了一个将所有pdf显示为ListView的应用程序。当我单击任何pdf文件时,它会在officeSuite应用程序(不支持或损坏文件格式)中出现错误。代码有问题。这是代码。 //显示为ListVIew的项目代码 //打开文件的代码VIA Intent 错误: 损坏或不支持的文件格式

  • 问题内容: 我正在使用Apache PDFBox(http://pdfbox.apache.org/)从任意数量的文件(包括图像和其他PDF)中创建PDF。现在,我需要将MS Office文档(Word,Excel和Outlook MSG)添加到PDF。这些文件几乎可以具有每个Office版本,因此不能认为该文件是新的Office文件(例如docx)还是旧的办公室文件(例如doc)。 有什么办法只

  • 我正在使用Apache PDFBox(http://PDFBox.Apache.org/)从任意数量的文件(包括图像和其他PDF)中创建PDF。现在我需要将MS Office文档(Word、Excel和Outlook MSGs)添加到PDF中。这些文件几乎可以有每一个Office版本,因此不允许该文件是新的Office文件(例如docx)还是旧的Office文件(例如doc)。 有没有办法只使用免

  • 如何使图像可点击,以便打开附加的ms word文档?我这里有一些PDF文件,其中有一些图像(图标下方带有ms word文件名的ms word图标),可以通过单击这些图像打开附加的ms word文档,我想知道如何使用iText库实现这一点。我可以添加图片和附加ms word文档,但我还不知道如何应用操作(GoToE似乎仅适用于PDF附件)或链接之类的内容?

  • 问题内容: 我对编程和python语言非常陌生。 我知道如何在python中打开文件,但问题是如何将文件作为函数的参数打开? 例: 这是我写出代码的方式: 问题答案: 您可以轻松地传递文件对象。 然后在您的函数中,返回行列表 另一个技巧是,python文件对象实际上具有读取文件行的​​方法。像这样: 第二种方法,就像您的功能一样。您不必再次调用它。 更新 这里是您应该如何编写代码的方法: 第一种方

  • 问题内容: 通过使用如何将文件转换为? 我正在使用以下代码,但无法正常工作,提示错误,我想我导入了错误的类? 问题答案: 得到了解决