View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
urkec urkec is offline
external usenet poster
 
Posts: 131
Default close pdf file before closing excel

"Steve Yandl" wrote:

I tested this same approach earlier and found that on my system it shut down
all instances of acrobat reader, even though I specified the command line as
you did. I also suspect that it might not work if a user is running the
full Adobe program as opposed to opening pdf files with acrobat reader
(something I can't check on my system).

Steve


"urkec" wrote in message
...
"Ken" wrote:

I have an excel model from where users can open a user guide in pdf
format.
When a user closes the excel model, I would like excel to check whether
the
pdf file is open, and if it is, then excel should close the pdf file
first.


Private Sub Workbook_BeforeClose(Cancel As Boolean)

Set objSWBemServices = GetObject _
("WinMgmts:Root\Cimv2")

Set colPDFs = objSWBemServices.ExecQuery _
("Select * From Win32_Process " & _
"Where Name = 'acrord32.exe' " & _
"And CommandLine Like '%C:\\Docs\\MyGuide.pdf%'")

For Each objPDF In colPDFs
objPDF.Terminate
Next

End Sub



You are right. This doesn't work with more than one PDF document open
because there is only one acrord32.exe process. Command line contains the
path to the first document open, so it is also possible not to close any
documents. Sorry, I should have tested this more carefully.

--
urkec