Fermer un fichier PDF

Résolu/Fermé
Utilisateur anonyme - 2 févr. 2015 à 09:43
 Utilisateur anonyme - 2 févr. 2015 à 15:14
Bonjour,

J'ai un problème pour fermer un PDF, j'ai trouvé des exemples sur le net mais aucun des exemples que j'ai trouvés fonctionne chez moi...en gros ce que je veux faire c'est fermer un fichier PDF car je viens en créer un pardessus ce PDF qui a le même nom donc je peux pas le créer si ce PDF est déjà ouvert.
Voilà la fonction que j'ai pour l'instant:
  Sub Tst_2007(Nom)
Set fso = CreateObject("Scripting.FileSystemObject")
If (fso.FileExists("C:\Users\Documents\TestNode2\code2\" & Nom & ".pdf")) Then
Kill ("C:\Users\Documents\TestNode2\code2\" & Nom & ".pdf")
'Set objFSO = CreateObject("Scripting.FileSystemObject")
'objFSO.DeleteFile ("C:\Users\Documents\TestNode2\code2\" & Nom & ".pdf")
'Call Fermer_Un_Programme("AcroRd32.exe")
'AppActivate MonApp, True
'SendKeys ("%{F4}") 'envoi altF4"
End If
Dim sNomFichierPDF As String
sNomFichierPDF = ThisWorkbook.Path & "\" & Nom
Application.ScreenUpdating = False
Sheets.Select

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=sNomFichierPDF _
, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=False
End Sub


J'ai testé kill mais ça ne fonctionne pas non plus...
Si quelqu'un aurait une solution se serait géniale merci d'avance.

A voir également:

3 réponses

pijaku Messages postés 12263 Date d'inscription jeudi 15 mai 2008 Statut Modérateur Dernière intervention 4 janvier 2024 2 745
2 févr. 2015 à 11:50
Bonjour,

J'y vais de ma contribution, en utilisant l'api user32 :

    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 Boucle_Sur_Fenetres()
Dim pointeur As Long
Dim titre As String

pointeur = FindWindow(vbNullString, "")
Do While pointeur <> 0
    titre = String(100, Chr$(0))
    GetWindowText pointeur, titre, 100
    titre = Left$(titre, InStr(titre, Chr$(0)) - 1)
    If titre Like "*" & Nom & ".pdf" & "*" Then FermerFenetre pointeur: Exit Sub
    pointeur = GetWindow(pointeur, 2)
Loop
End Sub

Sub FermerFenetre(handWindow As Long)
PostMessage handWindow, 16, 0, 0
End Sub

2
Utilisateur anonyme
2 févr. 2015 à 12:12
Bonjour, merci pour ta réponse mais j'ai une erreur...il me dit qu'il manque des end sub pourtant quand je les rajoute j'ai toujours l'erreur...
0
pijaku Messages postés 12263 Date d'inscription jeudi 15 mai 2008 Statut Modérateur Dernière intervention 4 janvier 2024 2 745 > Utilisateur anonyme
2 févr. 2015 à 12:19
Peux tu nous copier coller ton code intégralement?
0