首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过MS-Word打开pdf文件

通过MS-Word打开pdf文件
EN

Stack Overflow用户
提问于 2018-12-12 11:32:03
回答 2查看 494关注 0票数 0

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

我之前尝试过hyperlinkfollow和Shell MyPath & " " & MyFile, vbNormalFocus方法,它不工作。我还处于打开pdf文件的开始阶段,请指教。谢谢!

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

发布于 2018-12-12 15:54:30

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

更像这样做:

代码语言:javascript
复制
Dim wd As Word.Application
Set wd = New Word.Application. 'Or use CreateObject
票数 0
EN

Stack Overflow用户

发布于 2018-12-12 17:25:35

我认为这三个来源的组合将导致答案:

How to open a pdf with Excel?

How to extract data from pdf using VBA?

How to open and print a pdf using VBA?

我想应该是这样的:

代码语言:javascript
复制
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的自动扫描。

我希望这能让您在解决方案的道路上走得更远。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53735654

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档