close pdf file before closing excel
"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.
Can anyone help me with this?
Thanks
I don't know how you open the pdf file from Excel.For test I used Shell
function:
Shell "cmd /c C:\docs\MyGuide.pdf"
The pdf is opened by Acrobat Reader 7 and the created process contains the
path to the file (in CommandLine property). Then I can use this code to check
if the pdf file is open and close it:
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
This code first connects to WMI service on local computer, checks for
processes named Acrord32.exe that have the path to the pdf file in their
command line (C:\docs\myguide.pdf) and terminates such processes. I didn't
test this much, but it seems to work. (this will not work in Windows 2000 and
earlier, you would need to use Instr function instead of Like operator to
check for the file path)
I hope this helps.
--
urkec
|