vba - Check if a certain pdf file is open and close it

Tornado1981

Board Regular
Joined
Apr 1, 2010
Messages
248
Hi,
I use this code to export a pdf file from a word document.
Before exporting I need to check first if a file with the same name is already open, and if so close it then export.
I tried many things but had no luck.
Any Suggestions would be really appreciated.
Thank u in advance.

Code:
Dim adbApp As Acrobat.AcroApp
Dim adbDoc As Acrobat.AcroAVDoc
Dim adbPageView As Acrobat.AcroAVPageView
                
Set adbApp = CreateObject("AcroExch.App")
Set adbDoc = CreateObject("AcroExch.AVDoc")

If adbDoc.Open(FilePath & "Current Letter Preview.pdf", "") = True Then '==> Here if the file is not open, it opens it.
   adbDoc.Close (1)
                
   If adbDoc Is Nothing Then '==> Here it need to check if any pdf files are open, but it doesn't work.
         adbApp.Exit
   End If
                
   Set adbApp = Nothing
End If
            
Dim wordApp As Word.Application
Dim wordDoc As Word.Document
                        
If IsFileOpen(FilePath & "TemporaryLetter.docx") Then
   Set wordApp = GetObject(, "Word.Application")
   wordApp.Documents(FilePath & "TemporaryLetter.docx").Close
Else
   Set wordApp = CreateObject("Word.Application")
                
   With wordApp
         .Visible = True
         .WindowState = 2
   End With
End If
                
Set wordDoc = wordApp.Documents.Open(FilePath & "TemporaryLetter.docx")
            
wordDoc.ExportAsFixedFormat OutputFileName:=FilePath & "Current Letter Preview.pdf", _
ExportFormat:=wdExportFormatPDF
            
wordDoc.Close savechanges:=wdDoNotSaveChanges
            
Set wordDoc = Nothing
    
If wordDoc Is Nothing Then
    wordApp.Quit
End If
        
Set wordApp = Nothing
                        
Call adbDoc.Open(FilePath & "Current Letter Preview.pdf", "")
            
adbDoc.BringToFront
            
Set adbPageView = adbDoc.GetAVPageView()

Call adbPageView.ZoomTo(0, 100)
            
Set adbDoc = Nothing
Set adbPageView = Nothing
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"

Forum statistics

Threads
1,214,585
Messages
6,120,390
Members
448,957
Latest member
Hat4Life

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top