Ouvrir un pdf converti avec PDF Creator depuis VBA

thom02

XLDnaute Junior
Bonjour,

J'ai trouvé ce code qui me permet de convertir mon fichier word depuis excel avec VBA.

Mais j'aimerais bien que le document pdf fraichement crée s'ouvre une fois la procédure terminée.

J'ai essayé via FollowHyperlink mais cela ne fonctionne pas.

Auriez vous des idées?

Un grand merci !!

Code:
Sub WordToPdf()

Set pdfjob = CreateObject("PDFCreator.clsPDFCreator")
NomWord = ActiveDocument.Name
NomPdf = Left(NomWord, Len(NomWord) - 4) & ".pdf"

With pdfjob
If .cStart("/NoProcessingAtStartup") = False Then
MsgBox "Can't initialize PDFCreator.", vbCritical + vbOKOnly, "PrtPDFCreator"
Exit Sub
End If
.cOption("UseAutosave") = 1
.cOption("UseAutisaveDirectory") = 1
.cOption("AutosaveDirectory") = ActiveDocument.Path
.cOption("AutosaveFilename") = NomPdf
.cOption("AutosaveFormat") = 0
.cOption("Autolaunch") = True
.cClearCache
End With
Application.ActivePrinter = "PDFCreator"
Application.ActiveDocument.PrintOut copies:=1
Do Until pdfjob.cCountOfPrintjobs = 1
DoEvents
Loop
pdfjob.cPrinterStop = False
Do Until pdfjob.cCountOfPrintjobs = 0
DoEvents
Loop
With pdfjob
.cDefaultPrinter = DefaultPrinter
.cClearCache
.cClose
End With
Set pdfjob = Nothing

Dim detailPDF As String
detailPDF = ActiveDocument.Path & "\" & NomPdf & ".pdf"
'ActiveWorkbook.FollowHyperlink detailPDF

End Sub
 
Dernière édition:

YANN-56

XLDnaute Barbatruc
Re : Ouvrir un pdf converti avec PDF Creator depuis VBA

Bonjour thom, et à ceux qui passeront par ici,

Je tenterais le coup avec:

Code:
ThisWorkbook.FollowHyperlink ThisWorkbook.Path & "\NomPdf.pdf"

A adapter si le Classeur est dans un autre répertoire.
(Ne l'ayant fait qu'avec Excel… Je n'ai pas testé)

Amicalement.

Yann
 
Dernière édition:

thom02

XLDnaute Junior
Re : Ouvrir un pdf converti avec PDF Creator depuis VBA

le fichier converti etant un Word j'ai essayé avec

ThisDocument.FollowHyperlink ThisDocument.Path & "\NomPdf.pdf"

mais cela ne marche pas :-(
 
Dernière édition:

James007

XLDnaute Barbatruc
Re : Ouvrir un pdf converti avec PDF Creator depuis VBA

Bonjour,

Ci-joint un code à tester :
PHP:
Sub WordToPdf()
Dim myShell As Object
Dim pdfjob As Object
Dim detailPDF As String

Set myShell = CreateObject("WScript.Shell")
Set pdfjob = CreateObject("PDFCreator.clsPDFCreator")
NomWord = ActiveDocument.Name
NomPdf = Left(NomWord, Len(NomWord) - 4) & ".pdf"

    With pdfjob
            If .cStart("/NoProcessingAtStartup") = False Then
                MsgBox "Can't initialize PDFCreator.", vbCritical + vbOKOnly, "PrtPDFCreator"
                Exit Sub
            End If
        .cOption("UseAutosave") = 1
        .cOption("UseAutisaveDirectory") = 1
        .cOption("AutosaveDirectory") = ActiveDocument.Path
        .cOption("AutosaveFilename") = NomPdf
        .cOption("AutosaveFormat") = 0
        .cOption("Autolaunch") = True
        .cClearCache
    End With
Application.ActivePrinter = "PDFCreator"
Application.ActiveDocument.PrintOut copies:=1
    Do Until pdfjob.cCountOfPrintjobs = 1
        DoEvents
    Loop
pdfjob.cPrinterStop = False
    Do Until pdfjob.cCountOfPrintjobs = 0
        DoEvents
    Loop
    With pdfjob
        .cDefaultPrinter = DefaultPrinter
        .cClearCache
        .cClose
    End With
Set pdfjob = Nothing

detailPDF = ActiveDocument.Path & "\" & NomPdf & ".pdf"
myShell.Run detailPDF

End Sub

A +
:)
 

RENAUDER

Nous a quitté
Repose en paix
Re : Ouvrir un pdf converti avec PDF Creator depuis VBA

Bonjour,
Si j'ai bien compris, tu veux ouvrir un fichier PDF depuis VBA.
Code:
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
 

Discussions similaires

Statistiques des forums

Discussions
312 348
Messages
2 087 508
Membres
103 568
dernier inscrit
NoS