VBA to open PDF from Form with search parameter

matt beamish

Registered User.
Local time
Today, 16:41
Joined
Sep 21, 2000
Messages
208
Hi people,
I've been working at this for too many hours in my amateurish way, and it's turned into:banghead:. I have searched for bits of code to copy other people better than I, and had some success but I am failing to complete, so it is now time for me to visit you all.

I am trying to open a PDF from a button on a subform. The location of the PDF is specified in a field. I want to open the PDF and search for a Member ID criteria also listed in a field on the Form.

This is my code:

Code:
Private Sub Text12_Click()

Dim strPath As String
Dim Searchmem As String

strPath = [Notetxt]
Searchmem = [MemID]
            
pat1 = """C:\Program Files (x86)\Adobe\Reader 11.0\Reader\AcroRd32.exe"""
pat2 = " /A " & Chr(34) & search = Searchmem & Chr(34)
pat3 = strPath

Debug.Print pat1 & "; " & pat2 & "; " & pat3
Shell pat1 & " " & pat2 & " " & pat3, vbNormalFocus
End Sub

In my Immediates I get
"C:\Program Files (x86)\Adobe\Reader 11.0\Reader\AcroRd32.exe"; False; \\uol.le.ac.uk\root\departments\ULAS\WebAccess\LAHS\2014\201409_Mailmerge_Final\LAHS_HistorianNewsletter.pdf

Adobe Reader reports "There was an error opening this document. The file cannot be found." but then opens the file (that's something).

So it's the "search" bit that is the problem, and I cannot for the life of me get my search parameter to be used.

Help appreciated, thanks
 
Amended:
Code:
Private Sub Text12_Click()
    Dim strPath As String
    Dim Searchmem As String
    
    strPath = [Notetxt]
    Searchmem = [MemID]
                
    pat1 = Chr(34) & "C:\Program Files (x86)\Adobe\Reader 11.0\Reader\AcroRd32.exe" & Chr(34)
    pat2 = "/A " & Chr(34) & "search=" & Searchmem & Chr(34)
    pat3 = strPath
    
    Debug.Print pat1 & "; " & pat2 & "; " & pat3
    Shell pat1 & " " & pat2 & " " & pat3, vbNormalFocus
End Sub
 
Success! Lovely job:)

thanks very much
 
Hi there. I am using the proceedure above on several projects now and finding it really useful helping colleagues in making a direct link between a database and a PDF with related information.

I would like to be able to use it to search an already open PDF but my attempts are failing.
One option I tried was to refocus on the PDF - which does work, but I still cannot get the search proceedure to start.

I did ask this question shortly after vbaInet helped me previously a few months back when I realised that the search could not be re-activated with the same code, so apoogies for any bad protocol.

I guess I am needing to be told that this is a non-starter.

thanks
 
Ok I looked a bit harder for a function to close down Adobe Reader, and here
https://social.msdn.microsoft.com/forums/office/en-US/9a26bda2-6cb1-4024-bcf5-464acdc96170/closing-pdf-files-that-are-open-via-vba
I found this:

Code:
Sub TerminateProcess()
    Dim strTerminateThis As String
    Dim objWMIcimv2 As Object
    Dim objProcess As Object
    Dim objList As Object
    Dim intError As Integer
     
    'Process to terminate
    strTerminateThis = "AcroRd32.exe"
     
    'Connect to CIMV2 Namespace
    Set objWMIcimv2 = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
     
    'Find the process to terminate
    Set objList = objWMIcimv2.ExecQuery("select * from win32_process where name='" & strTerminateThis & "'")
   
    'Terminates a process and all of its threads.
    For Each objProcess In objList
        intError = objProcess.Terminate
    Next
             
    Set objWMIcimv2 = Nothing
    Set objList = Nothing
    Set objProcess = Nothing
End Sub

So I created a new module in my database with this code, and then adapted my existing code to include
Code:
TerminateProcess

And it all works fine. It's a bit crude as it will close down every PDF file open, but it serves my purpose and the searching from within the VBA can continue

cheers
Matt
 

Users who are viewing this thread

Back
Top Bottom