Code ferme un pdf S'il le trouve ouvert avant d'exécuter OpenAfterPublish
Bonjour Forum,
SVP
J'ai besoin un code vba ferme le fichier groupé.PDF S'il le trouve ouvert avant d'exécuter la ligne suivante:
c.ExportAsFixedFormat xlTypePDF, chemin & "Groupé.pdf", OpenAfterPublish:=True
afin qu'il n'y ait pas de conflit entre l'ouverture de deux fichiers du même nom .
Merci d'avance.
- Messages
- 4'092
- Excel
- 2021 FR 64 bits
- Inscrit
- 13/06/2016
- Emploi
- bénévole associations Goutte d'Or
Bonjour,
ci-dessous un code via APIs Windows:
Option Explicit
#If VBA7 Then
'Api windows pour recherche fenêtre (pointeur)
Private Declare PtrSafe Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
Private Declare PtrSafe Function GetWindow Lib "user32.dll" (ByVal hwnd As LongPtr, ByVal wCmd As Long) As LongPtr
Private Declare PtrSafe Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" (ByVal hwnd As LongPtr, ByVal lpString As String, ByVal nMaxCount As LongPtr) As LongPtr
Private Declare PtrSafe Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As LongPtr) As LongPtr
'Api windows pour émuler les actions sur une fenêtre
Private Declare PtrSafe Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As LongPtr, ByVal wMsg As LongPtr, ByVal wParam As LongPtr, lParam As Any) As LongPtr
'variables API
Private ptr_wd As LongPtr
#Else
'Api windows pour recherche de la fenetre au premier plan (pointeur)
Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindow Lib "user32.dll" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal nMaxCount As Long) As Long
Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
'Api windows pour émuler les actions sur une fenêtre
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
'variables API
Private ptr_wd As Long
#End If
'constantes API
Private Const GW_HWNDNEXT = 2
Private Const WM_CLOSE = &H10 'fermeture fenêtre
Sub fermer_pdf()
Dim chemin As String
'fermeture éventuelle PDF
ptr_wd = récup_pdf("Groupé.pdf") 'récupération pointeur de la fenêtre PDF
If ptr_wd <> 0 Then Call SendMessage(ptr_wd, WM_CLOSE, 0&, 0&)
End Sub
Function récup_pdf(nom_fichier)
Dim titre As String, SLength As Variant
récup_pdf = 0
ptr_wd = FindWindow(vbNullString, vbNullString) 'PARENT WINDOW
Do While ptr_wd <> 0
SLength = GetWindowTextLength(ptr_wd) + 1
titre = Space(SLength)
GetWindowText ptr_wd, titre, Len(titre)
titre = Left$(titre, Len(titre) - 1)
If titre Like "*" & nom_fichier & "*" Then
récup_pdf = ptr_wd
Exit Do
End If
ptr_wd = GetWindow(ptr_wd, GW_HWNDNEXT)
Loop
End Function
Bonjour thev, Bonjour Forum
Merci de l'intérêt que vous portez à mon sujet
Où vais-je mettre ce code et vais-je tout mettre ou juste la partie qui concerne la fermeture?
Merci infiniment
- Messages
- 4'092
- Excel
- 2021 FR 64 bits
- Inscrit
- 13/06/2016
- Emploi
- bénévole associations Goutte d'Or
Où vais-je mettre ce code et vais-je tout mettre ou juste la partie qui concerne la fermeture?
1- vous insérez un nouveau module que vos appelez par exemple : Fonctions
2- dans ce module "Fonctions", vous copiez ce code :
Option Explicit
#If VBA7 Then
'Api windows pour recherche fenêtre (pointeur)
Private Declare PtrSafe Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
Private Declare PtrSafe Function GetWindow Lib "user32.dll" (ByVal hwnd As LongPtr, ByVal wCmd As Long) As LongPtr
Private Declare PtrSafe Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" (ByVal hwnd As LongPtr, ByVal lpString As String, ByVal nMaxCount As LongPtr) As LongPtr
Private Declare PtrSafe Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As LongPtr) As LongPtr
'Api windows pour émuler les actions sur une fenêtre
Public Declare PtrSafe Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As LongPtr, ByVal wMsg As LongPtr, ByVal wParam As LongPtr, lParam As Any) As LongPtr
'variables API
Public ptr_wd As LongPtr
#Else
'Api windows pour recherche de la fenetre au premier plan (pointeur)
Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindow Lib "user32.dll" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal nMaxCount As Long) As Long
Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
'Api windows pour émuler les actions sur une fenêtre
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
'variables API
Public ptr_wd As Long
#End If
'constantes API
Public Const GW_HWNDNEXT = 2
Public Const WM_CLOSE = &H10 'fermeture fenêtre
Function récup_pdf(nom_fichier)
Dim titre As String, SLength As Variant
récup_pdf = 0
ptr_wd = FindWindow(vbNullString, vbNullString) 'PARENT WINDOW
Do While ptr_wd <> 0
SLength = GetWindowTextLength(ptr_wd) + 1
titre = Space(SLength)
GetWindowText ptr_wd, titre, Len(titre)
titre = Left$(titre, Len(titre) - 1)
If titre Like "*" & nom_fichier & "*" Then
récup_pdf = ptr_wd
Exit Do
End If
ptr_wd = GetWindow(ptr_wd, GW_HWNDNEXT)
Loop
End Function
3- dans votre procédure Sub, vous ajoutez ces instructions :
'fermeture éventuelle PDF
ptr_wd = récup_pdf("Groupé.pdf") 'récupération pointeur de la fenêtre PDF
If ptr_wd <> 0 Then Call SendMessage(ptr_wd, WM_CLOSE, 0&, 0&)
autre solution, n'ouvrez pas le pdf si cela n'est pas nécessaire
>>>>>>> "OpenAfterPublish:=false"