Macro enregistrer en PDF avec nom repris depuis une cellule

Bonjour,

j'ai une question qui a déjà été posée ici et solutionnée notamment sur le post de a_loic.

Cependant j'aimerai une aide sur une formule (formule donnée par un certain Ron) que j'utilise dans un document excel composé de 3 feuilles.

cette formule me permet de mettre des boutons sur mes feuilles donnant pour l'un d'eux la possibilité d'enregistrer en PDF.

Mais j'aimerais que lors de l'enregistrement le nom se renseigne automatiquement avec le nom qui se trouve dans un cellule.

Dans mon projet VBA j'ai renseigné 3 modules :

  • CreateMail
  • CreatePDFMAIL
  • FunctionsModule

Dans CreateMail voilà le code :

Sub RDB_Worksheet_Or_Worksheets_To_PDF()
    Dim FileName As String

    If ActiveWindow.SelectedSheets.Count > 1 Then
        MsgBox "There is more then one sheet selected," & vbNewLine & _
               "be aware that every selected sheet will be published"
    End If

    'Call the function with the correct arguments
    'Tip: You can also use Sheets("YourSheetName") instead of ActiveSheet in the code(sheet not have to be active then)
    FileName = RDB_Create_PDF(Source:=ActiveSheet, _
                              FixedFilePathName:="", _
                              OverwriteIfFileExist:=True, _
                              OpenPDFAfterPublish:=True)

    'For a fixed file name use this in the FixedFilePathName argument
    'FixedFilePathName:="C:\Users\Ron\Test\YourPdfFile.pdf"

    If FileName <> "" Then
        'Ok, you find the PDF where you saved it
        'You can call the mail macro here if you want
    Else
        MsgBox "Not possible to create the PDF, possible reasons:" & vbNewLine & _
               "Microsoft Add-in is not installed" & vbNewLine & _
               "You Canceled the GetSaveAsFilename dialog" & vbNewLine & _
               "The path to Save the file in arg 2 is not correct" & vbNewLine & _
               "You didn't want to overwrite the existing PDF if it exist"
    End If
End Sub

Je suis débutant, je vous joins mon fichier, j'ai testé 3 boutons qui fonctionnent bien.

Mais j'aimerai vraiment pouvoir renseigner automatiquement le nom du pdf

Merci de votre aide

Bonjour,

il te suffit de rajouter NomFichierPDF qui sera la variable contenant le nom que tu veux donner, et dans la fonction qui est appelé après mettre FixedFilePathName:=NomFichierPDF :

Sub RDB_Workbook_To_PDF()
    Dim FileName As String

    NomFichierPDF = Sheets("nom onglet").Range("A1 (par exemple, case où se trouve le nom)").Value

    'Call the function with the correct arguments
    FileName = RDB_Create_PDF(Source:=ActiveWorkbook, _
                              FixedFilePathName:=NomFichierPDF, _
                              OverwriteIfFileExist:=True, _
                              OpenPDFAfterPublish:=True)

Cindy

Bonjour et merci Cindy de ton aide.

Alors mon bouton renvoie sur la seconde fonction qui est Sub RDB_Worksheet_Or_Worksheets_To_PDF()

Sub RDB_Worksheet_Or_Worksheets_To_PDF()
    Dim FileName As String

        NomFichierPDF = ActiveSheet.Range("F10").Value

    If ActiveWindow.SelectedSheets.Count > 1 Then
        MsgBox "There is more then one sheet selected," & vbNewLine & _
               "be aware that every selected sheet will be published"
    End If

    'Call the function with the correct arguments
    'Tip: You can also use Sheets("YourSheetName") instead of ActiveSheet in the code(sheet not have to be active then)
    FileName = RDB_Create_PDF(Source:=ActiveSheet, _
                              FixedFilePathName:=NomFichierPDF, _
                              OverwriteIfFileExist:=True, _
                              OpenPDFAfterPublish:=True)

    'For a fixed file name use this in the FixedFilePathName argument
    'FixedFilePathName:="C:\Users\Ron\Test\YourPdfFile.pdf"

    If FileName <> "" Then
        'Ok, you find the PDF where you saved it
        'You can call the mail macro here if you want
    Else
        MsgBox "Not possible to create the PDF, possible reasons:" & vbNewLine & _
               "Microsoft Add-in is not installed" & vbNewLine & _
               "You Canceled the GetSaveAsFilename dialog" & vbNewLine & _
               "The path to Save the file in arg 2 is not correct" & vbNewLine & _
               "You didn't want to overwrite the existing PDF if it exist"
    End If
End Sub

Cela me met erreur de compilation, variable non définie.

J'ai aussi mis

NomFichierPDF = Sheets("Devis").Range("F10").Value

, même erreur

Vous pouvez essayer au début du code :

Dim FileName As String
Dim NomFichierPDF As String

Cindy

Bonjour Cindy,

en rajoutant ce code je n'ai plus l'erreur mais sur la variable.

La création du pdf se fait bien avec le nom mais je n'ai pas d'enregistrement

J'ai le message de la msgbox suivant qui apparait :

"Not possible to create the PDF, possible reasons:" & vbNewLine & _

"Microsoft Add-in is not installed" & vbNewLine & _

"You Canceled the GetSaveAsFilename dialog" & vbNewLine & _

"The path to Save the file in arg 2 is not correct" & vbNewLine & _

"You didn't want to overwrite the existing PDF if it exist"

Je ne comprends pas où cela pèche.

Avec mon code du début, j'ai bien la demande d'enregistrement du pdf généré mais sans le nom de fichier.

Avec votre aide, le nom est bien saisi lors de la création du pdf mais je n'ai pas d'enregistrement du fichier proposé

Effectivement, il y avait aussi un petit oubli. Il faut utiliser ce bout de code :

FileName = RDB_Create_PDF(Source:=ActiveSheet, _
                              FixedFilePathName:="C:\Users\Cindy\Desktop\" & NomFichierPDF & ".pdf", _
                              OverwriteIfFileExist:=True, _
                              OpenPDFAfterPublish:=True)

Dans la partie FixedFilePathName, il faut renseigner le chemin de votre document, où l'enregistrer. Par exemple, là, je l'enregistre sur mon bureau.

Si vous voulez savoir le chemin pour le PDF, vous pouvez créer un excel dans le fichier souhaité et lancer la macro suivante :

Sub Trouver Chemin()
MsgBox (ThisWorkbook.Path)
End sub

J'espère que ça marchera comme ça!

Cindy

Bonjour Cindy,

avec ce bout de lignes cela fonctionne parfaitement maintenant.

Encore un grand merci à toi.

De rien !

N'oublie pas de valider ma réponse et donc clôturer le sujet en cliquant sur le check à côté de mon nom

Bonne journée !

Cindy

Rechercher des sujets similaires à "macro enregistrer pdf nom repris"