Macro enregistrer PDF - "Faux.pdf" créé en annulant boîte de dialogue
Bonjour,
Je souhaitais trouver une macro pour enregistrer ma feuille active en PDF. Après plusieurs tentatives infructueuses et plusieurs recherches, j'ai repris cette macro sur le site "contextures.com" :
Sub EnregistrementFacture1()
Dim wsA As Worksheet
Dim wbA As Workbook
Dim strTime As String
Dim strName As String
Dim strPath As String
Dim strFile As String
Dim strPathFile As String
Dim myFile As Variant
On Error GoTo errHandler
Set wbA = ActiveWorkbook
Set wsA = ActiveSheet
strTime = Format(Now(), "yyyy-mm-dd")
'get active workbook folder, if saved
strPath = wbA.Path
If strPath = "" Then
strPath = Application.DefaultFilePath
End If
strPath = strPath & "\"
'replace spaces and periods in sheet name
strName = Replace(wsA.Name, " ", "")
strName = Replace(strName, ".", "_")
'create default name for savng file
strFile = strName & "_" & strTime & ".pdf"
strPathFile = strPath & strFile
'use can enter name and
' select folder for file
myFile = Application.GetSaveAsFilename _
(InitialFileName:=strPathFile, _
FileFilter:="PDF Files (*.pdf), *.pdf", _
Title:="Select Folder and FileName to save")
'export to PDF if a folder was selected
If myFile <> "False" Then
wsA.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=myFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
'confirmation message with file info
MsgBox "Le fichier PDF de la facture a été créé: " _
& vbCrLf _
& myFile
End If
exitHandler:
Exit Sub
errHandler:
MsgBox "Le fichier PDF n'a pas été créé"
Resume exitHandler
End SubLa macro fait son travail, elle me permet de choisir l'emplacement pour l'enregistrement et met par défaut un nom de fichier en fonction de la date, ce que je souhaiterai garder.
Petit problème par contre, si on fait annuler lorsque la fenêtre s'ouvre, un fichier s'enregistre quand même et se nomme "FAUX.pdf". a_loic évoquait le même problème sur un autre sujet ("Macro enregistrer en PDF avec nom cellule"), avec une solution proposée par Banzai64:
Sub PassationPDF()
Dim Chemin As String, NomFichier As String
NomFichier = Range("J2").Value & "_" & Format(Date, "dd_mm_yyyy") & "_Passation" & ".pdf"
'NomFichier = Range("J2").Value & "_" & Range("G2").Value & "_Passation" & ".pdf"
With Application.FileDialog(msoFileDialogFolderPicker)
If .Show = -1 Then ' Clic sur Ok
Chemin = .SelectedItems(1)
Else
' Clic sur Annuler
Exit Sub
End If
End With
Sheets("Passation").ExportAsFixedFormat Type:=xlTypePDF, Filename:=Chemin & "\" & NomFichier, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
End SubMais au vu de mes faibles compétences en VBA, je ne sais pas trop bien comment/où l'appliquer à ma macro actuelle.
Quelqu'un peut-il m'aider ? Merci !
Mayoche
Bonjour,
Essayer en remplaçant
If myFile <> "False" Thenpar
If myFile <> False ThenA+
Bonjour,
Merci pour la réponse, ça marche !