通过MS-Word打开pdf文件

时间:2018-12-12 03:32:03

标签: vba pdf ms-word

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

我以前尝试过超链接跟随和Shell MyPath & " " & MyFile, vbNormalFocus方法,但是它不起作用。我仍处于打开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 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 个答案:

答案 0 :(得分:0)

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

更像这样:

Dim wd As Word.Application
Set wd = New Word.Application. 'Or use CreateObject

答案 1 :(得分:0)

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

How to open a pdf with Excel?

How to extract data from pdf using VBA?

How to open and print a pdf using VBA?

我认为将会是这样:

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的自动扫描。

我希望这能使您进一步了解解决方案。