Close a PDF file
Solved
Anonymous user
-
Anonymous user -
Anonymous user -
Hello,
I have a problem closing a PDF file. I've found examples online, but none of the examples I've found work for me... What I want to do is close a PDF file because I just created one over this PDF that has the same name, so I can't create it if this PDF is already open.
Here's the function I have for now:
I've tried kill, but that doesn't work either... If anyone has a solution, that would be great, thanks in advance.
Configuration: Windows 7 / Firefox 35.0
I have a problem closing a PDF file. I've found examples online, but none of the examples I've found work for me... What I want to do is close a PDF file because I just created one over this PDF that has the same name, so I can't create it if this PDF is already open.
Here's the function I have for now:
Sub Tst_2007(Name)
Set fso = CreateObject("Scripting.FileSystemObject")
If (fso.FileExists("C:\Users\Documents\TestNode2\code2\" & Name & ".pdf")) Then
Kill ("C:\Users\Documents\TestNode2\code2\" & Name & ".pdf")
'Set objFSO = CreateObject("Scripting.FileSystemObject")
'objFSO.DeleteFile ("C:\Users\Documents\TestNode2\code2\" & Name & ".pdf")
'Call Close_Application("AcroRd32.exe")
'AppActivate MyApp, True
'SendKeys ("%{F4}") 'send altF4"
End If
Dim sFileNamePDF As String
sFileNamePDF = ThisWorkbook.Path & "\" & Name
Application.ScreenUpdating = False
Sheets.Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=sFileNamePDF _
, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=False
End Sub
I've tried kill, but that doesn't work either... If anyone has a solution, that would be great, thanks in advance.
Configuration: Windows 7 / Firefox 35.0
3 answers
-
Hello,
Here is my contribution, using the user32 API:
Private Declare Function FindWindow Lib "user32" _ Alias "FindWindowA" _ (ByVal lpClassName As String, _ ByVal lpWindowName As String) As Long Private Declare Function PostMessage Lib "user32" _ Alias "PostMessageA" _ (ByVal hwnd As Long, _ ByVal wMsg As Long, _ ByVal wParam As Long, _ ByVal lParam As Long) As Long Declare Function GetWindowText Lib "user32" _ Alias "GetWindowTextA" _ (ByVal hwnd As Long, _ ByVal lpString As String, _ ByVal cch As Long) As Long Declare Function GetWindow Lib "user32" _ (ByVal hwnd As Long, _ ByVal wCmd As Long) As Long Sub Loop_Through_Windows() Dim pointer As Long Dim title As String pointer = FindWindow(vbNullString, "") Do While pointer <> 0 title = String(100, Chr$(0)) GetWindowText pointer, title, 100 title = Left$(title, InStr(title, Chr$(0)) - 1) If title Like "*" & Name & ".pdf" & "*" Then CloseWindow pointer: Exit Sub pointer = GetWindow(pointer, 2) Loop End Sub Sub CloseWindow(handWindow As Long) PostMessage handWindow, 16, 0, 0 End Sub
--
🎼 Best regards,
Franck 🎶