Private Declare Function DeleteFile Lib "kernel32" Alias "DeleteFileA" (ByVal lpFileName As String) As Long
Private Declare Function FindExecutable Lib "shell32.dll" Alias "FindExecutableA" (ByVal lpFile As String, ByVal lpDirectory As String, ByVal lpResult As String) As Long
Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
Sub OuvrePDF()
' En remplaçant 1 par une autre valeur cela permet
' d'ouvrir le PDF à une page donnée
Call OpenPdf("C:\Excel\2-Excel Downloads\Frédéric Martin-TSF\Site téléchargement\fichiers\Catalogue client Novembre 2010.pdf", 1)
End Sub
Function OpenPdf(ByVal sPath As String, Optional ByVal lNumPage As Long = 1, Optional ByVal WindowStyle As VbAppWinStyle = vbNormalFocus) As Boolean
' Cette fonction nécessite la fonction GetExePathFileFromExtention disponible ici :
' http://www.codyx.org/snippet_recuperer-chemin-programme-associe-fichier-apres-son-extension_544.aspx#1708
Dim sExe As String
sExe = GetExePathFileFromExtention("pdf")
If LenB(sExe) Then
On Error Resume Next
OpenPdf = (Shell(sExe & " /A page=" & CStr(lNumPage) & " " & sPath, WindowStyle) > 0)
End If
End Function
Function GetExePathFileFromExtention(ByVal sExtension As String) As String
Dim sPath As String, lRet As Long, sBuffer As String, FF As Integer
' on récupère le chemin TEMP
sBuffer = String$(512, vbNullChar)
lRet = GetTempPath(512, sBuffer)
sPath = Left$(sBuffer, lRet)
If Not (RightB$(sPath, 2) = "\") Then sPath = sPath & "\"
' on crée un fichier temporaire
sPath = sPath & Format$(Now, "MMDDHHNNSS") & "." & sExtension
FF = FreeFile
Open sPath For Output As #FF
Print #FF, vbNullString
Close #FF
' on récupère l'exe associé
sBuffer = String$(260, vbNullChar)
lRet = FindExecutable(sPath, vbNullString, sBuffer)
' retour
If lRet > 32 Then
GetExePathFileFromExtention = Left$(sBuffer, InStr(sBuffer, vbNullChar) - 1)
Else
GetExePathFileFromExtention = vbNullString
End If
' supprime fichier temp
Call DeleteFile(sPath)
End Function