0

I am trying to open a pdf file through MS Word, perform certain action such as evaluating calculations, printing the files, etc. and then proceed with closing the file. The error message I received is "Microsoft Excel is waiting for another application to complete an OLE action."

I have previously tried hyperlinkfollow and Shell MyPath & " " & MyFile, vbNormalFocus method, it doesn't work. I am still at the starting phase of opening the pdf files, please advice. Thanks!

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
Nick
  • 1
  • 3
  • Are you running the code in Excel or in word? Why do you want to open a .PDF in MS-Word? – Lucas Raphael Pianegonda Dec 12 '18 at 07:59
  • Hi! i am running the code through excel. Is there a more efficient method to do this, whereby i can manipulate the data within the pdf files – Nick Dec 12 '18 at 08:09
  • Well it depends on what you want to do? PDFs are thought to be files that are portable and should not be changed. A PDF is for example a great way to send someone a document they are supposed to read or print but not supposed to edit. The question is what you want a user to do? Should it just display data from your worksheet? Should it be printed? Should it be manipulated and then saved again? Please do add a little more detail on your problem and on your strategy to solve it. We will then try to help you with the code. – Lucas Raphael Pianegonda Dec 12 '18 at 08:13
  • Sorry for the lack of details. What i'm planning to do is to print according to specifications within the pdf files. For example performing calculations and if it is within the specifications i want, it will proceed to the next loop. I am also looking for specific words, if those words are within the pdf files, the program will proceed to PrintFile. Nothing will be changed in the original pdf file, it will close right after i perform the reading and printing. – Nick Dec 12 '18 at 08:21
  • Right now i am looking more to the framework of the code, like to open up, close and printing the file. – Nick Dec 12 '18 at 08:26
  • So the pdf file you open is already existing. You then want to scan the pdf file for keywords and perform some calculations on some data existing in the PDF file. Based on these criteria you want to decide if you print the file or not. Is that correct? – Lucas Raphael Pianegonda Dec 12 '18 at 08:27
  • Yes, that's correct sir. The program will open the pdf to determine if the file is supposed to be printed out, if Yes, print, if No, then proceed to closing the pdf file and moving on to the next file within the folder. – Nick Dec 12 '18 at 08:34

2 Answers2

0

Don't us the New keyword in the line that declares the object variable. This will "block" the object variable - it causes the error when the code laters tries to instantiate it. This method can work in VB.NET but not in VBA.

Do it more like this:

Dim wd As Word.Application
Set wd = New Word.Application. 'Or use CreateObject
Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
  • By the way, do you happen to know how to print a pdf file using vba? The printing setting i am look for is landscape and double sided flip to short edge. – Nick Dec 12 '18 at 08:12
  • Your original question is about opening the file, but getting an error message, @Nick. Does my suggestion solve that? If yes, it should be accepted as "the answer" (the checkmark next to it clicked" and any further problem asked in a new question. It might help you to understand how the site works by reading how to ask in the [help]. Questions should be narrowly scoped: a specific problem and only one problem / question. The scope of your request is expanding geometrically... – Cindy Meister Dec 12 '18 at 11:17
0

I think a combination of those three sources will lead to the answer:

How to open a pdf with Excel?

How to extract data from pdf using VBA?

How to open and print a pdf using VBA?

I think it will be something like this:

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

I have not been able to figure out how to extract the keywords, you could however use a user input as a first approach and later move on to a automated scan of the pdf.

I hope this gets you further on the way to the solution.

  • Hi! The code looks good, i'll try it out! But how do i change the printer setup? The format i am looking for is landscape, double sided and flip to short edge! – Nick Dec 14 '18 at 01:25
  • @Nick You then need to look up the shell commands for those print parameters and then add it to the shell code in the function `PrintPDF` in order to print it according to your parameters. – Lucas Raphael Pianegonda Dec 14 '18 at 07:11