Link to home
Start Free TrialLog in
Avatar of Ralph Gould
Ralph GouldFlag for United States of America

asked on

MS Access 2016 vba code to close Adobe Acrobat Reader DC Application

ms app that displays a pdf file in an access sub form. For years had been using an adobe activex
add-in to run this by providing a pdf file path as the control source. This has been redacted or never legit in the first place after 2010 version. Now I have to use a MS web browser control in place of the adobe activex control. The app let's the user update info in another sub form and then "Refresh" the pdf by recreating the file. During this process, I'm getting rt error 2501 the outputto operation has been cancelled when I try to refresh. It appears that adobe reader is still running in the background, which I think is causing the error.
I'm looking for a code snippet to close the adobe reader app.

The vba in question is
DoCmd.OpenReport "Work Order Report", acViewReport, , "JOB.JOB_NO='" & CurJobNo & "'"   ', acHidden
DoCmd.OutputTo acOutputReport, "Work Order Report", acFormatPDF, Temp1Path, False
DoCmd.Close acReport, "Work Order Report"

the docmd.Outputto.. item is where it fails
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

so basically you need to kill a running program...

try look into thread below for some discussions:

How can I kill task manager processes through VBA code?
https://stackoverflow.com/questions/26303173/how-can-i-kill-task-manager-processes-through-vba-code
If you wish to close the reader the code is :
Dim oServ As Object
Dim cProc As Variant
Dim oProc As Object

Set oServ = GetObject("winmgmts:")
Set cProc = oServ.ExecQuery("Select * from Win32_Process")
For Each oProc In cProc
    If oProc.Name = TheNameOfTheProcessYouAreInterested Then
     
      oProc.Terminate  ' With this command you can kill it
    End If
Next

Open in new window

I am afraid i don't know the name of the Adobe Acrobat Reader...just replace this :
   If oProc.Name = TheNameOfTheProcessYouAreInterested Then
with the name of it (Acroread ?)
If its hard to "locate" it just put a a breakpoint in this line
For Each oProc In cProc

Open in new window

and put a watch on cProc and search for it.
Otherwise i am a bit troubled on what you are after...based on the code you posted...you just exporting your report as PDF and it crashes on it.....this is a builtin functionality so better to check if something else is missing or replace the PDF exporting functionality with some other way like Leban's solution.
But...wouldn't it be better to have the report displayed as subreport and when everything is up and ready export it to PDF.
If I understand your use-case correctly, then the problem is that the web browser control has the file open/locked. You have two options:

1. Try to convince the web browser to release that file (hard).
2. Use temporary files for the browser (much simpler).
TemporaryFileName.vba
ASKER CERTIFIED SOLUTION
Avatar of Gustav Brock
Gustav Brock
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ralph Gould

ASKER

Gustav, works flawlessly
Thanks all for ur responses
Probably I need to make a list of applications executables names...it's seems that it would be more significant than it would expect and some cases it would make the "difference"