2017-09-11 297 views
1

我需要開發一個簡單的excel vba應用程序來執行一些數據驗證。我有excel文件與僱員信息,我需要對源文件(一個或多個pdf文件)驗證信息。 excel中的信息按員工姓名分組。打開PDF文件並執行文本搜索使用excel vba

是否可以編寫一個VBA腳本來打開指定的pdf文件,然後允許用戶執行員工姓名搜索,以便PDF跳轉到相應的員工頁面? (每位員工在pdf文檔中都有一個單獨的頁面。)進入員工頁面後,用戶將查看數據,對excel文件進​​行必要的編輯,然後搜索下一名員工。

我相信我可以使用:

Sub NavigatePDF() 

ThisWorkbook.FollowHyperlink "c:\user\target.pdf" 

End Sub 

打開PDF文件,但我不知道如何發出從Excel VBA搜索一次我打開該文件。

任何人都可以提供任何建議或指向我一些有用的資源。

回答

0

一些額外的研究產生了由Christos Samaras編寫的應用程序。該工具使用excel vba打開指定的PDF,然後突出顯示所提供的搜索詞。可能唯一的缺點是Adobe PDF Pro的要求 - 該代碼不適用於Acrobat Reader。

這裏是link和代碼:

Option Explicit 

Sub FindTextInPDF() 

    '---------------------------------------------------------------------------------------- 
    'This macro can be used to find a specific TEXT (more than one word) in a PDF document. 
    'The macro opens the PDF, finds the specified text (the first instance), scrolls so 
    'that it is visible and highlights it. 
    'The macro uses the FindText method (see the code below for more info). 

    'Note that in some cases it doesn't work (doesn't highlight the text), so in those 
    'cases prefer the SearchTextInPDF macro, if you have only ONE WORD to find! 

    'The code uses late binding, so no reference to external library is required. 
    'However, the code works ONLY with Adobe Professional, so don't try to use it with 
    'Adobe Reader because you will get an "ActiveX component can't create object" error. 

    'Written by: Christos Samaras 
    'Date:   04/05/2014 
    'e-mail:  [email protected] 
    'site:   http://www.myengineeringworld.net 
    '---------------------------------------------------------------------------------------- 

    'Declaring the necessary variables. 
    Dim TextToFind As String 
    Dim PDFPath  As String 
    Dim App   As Object 
    Dim AVDoc  As Object 

    'Specify the text you wawnt to search. 
    'TextToFind = "Christos Samaras" 
    'Using a range: 
    TextToFind = ThisWorkbook.Sheets("PDF Search").Range("C5").Value 

    'Specify the path of the sample PDF form. 
    'Full path example: 
    'PDFPath = "C:\Users\Christos\Desktop\How Software Companies Die.pdf" 
    'Using workbook path: 
    'PDFPath = ThisWorkbook.Path & "\" & "How Software Companies Die.pdf" 
    'Using a range: 
    PDFPath = ThisWorkbook.Sheets("PDF Search").Range("C7").Value 

    'Check if the file exists. 
    If Dir(PDFPath) = "" Then 
     MsgBox "Cannot find the PDF file!" & vbCrLf & "Check the PDF path and retry.", _ 
       vbCritical, "File Path Error" 
     Exit Sub 
    End If 

    'Check if the input file is a PDF file. 
    If LCase(Right(PDFPath, 3)) <> "pdf" Then 
     MsgBox "The input file is not a PDF file!", vbCritical, "File Type Error" 
     Exit Sub 
    End If 

    On Error Resume Next 

    'Initialize Acrobat by creating the App object. 
    Set App = CreateObject("AcroExch.App") 

    'Check if the object was created. In case of error release the object and exit. 
    If Err.Number <> 0 Then 
     MsgBox "Could not create the Adobe Application object!", vbCritical, "Object Error" 
     Set App = Nothing 
     Exit Sub 
    End If 

    'Create the AVDoc object. 
    Set AVDoc = CreateObject("AcroExch.AVDoc") 

    'Check if the object was created. In case of error release the objects and exit. 
    If Err.Number <> 0 Then 
     MsgBox "Could not create the AVDoc object!", vbCritical, "Object Error" 
     Set AVDoc = Nothing 
     Set App = Nothing 
     Exit Sub 
    End If 

    On Error GoTo 0 

    'Open the PDF file. 
    If AVDoc.Open(PDFPath, "") = True Then 

     'Open successful, bring the PDF document to the front. 
     AVDoc.BringToFront 

     'Use the FindText method in order to find and highlight the desired text. 
     'The FindText method returns true if the text was found or false if it was not. 
     'Here are the 4 arguments of the FindText methd: 
     'Text to find:   The text that is to be found (in this example the TextToFind variable). 
     'Case sensitive:  If true, the search is case-sensitive. If false, it is case-insensitive (in this example is True). 
     'Whole words only:  If true, the search matches only whole words. If false, it matches partial words (in this example is True). 
     'Search from 1st page: If true, the search begins on the first page of the document. If false, it begins on the current page (in this example is False). 
     If AVDoc.FindText(TextToFind, True, True, False) = False Then 

      'Text was not found, close the PDF file without saving the changes. 
      AVDoc.Close True 

      'Close the Acrobat application. 
      App.Exit 

      'Release the objects. 
      Set AVDoc = Nothing 
      Set App = Nothing 

      'Inform the user. 
      MsgBox "The text '" & TextToFind & "' could not be found in the PDF file!", vbInformation, "Search Error" 

     End If 

    Else 

     'Unable to open the PDF file, close the Acrobat application. 
     App.Exit 

     'Release the objects. 
     Set AVDoc = Nothing 
     Set App = Nothing 

     'Inform the user. 
     MsgBox "Could not open the PDF file!", vbCritical, "File error" 

    End If 

End Sub