Hello,
This macro should open, one by one, the workbooks located in a folder and save a copy in pdf format.
To make this more flexible, I write the path to the destination folder in cell K5 of my master file.
Example path:
P:\01-Qualité\K - Qualité Usinage\05 - CREATION PCP PREMIER NIVEAU\GCU A VALIDER\CA VERSO\FAST\ESSAI COPIE" & "\"
My macro seems to work, but the PDFs are not saved in the “ESSAI COPIE” folder which contains the workbooks, but in the K - Qualité Usinage” file.
> Do you have a solution to save the pdf in the source file (“ESSAI COPIE”)?
> Additionally, how can I avoid blocking the macro if the file already exists?
This macro should open, one by one, the workbooks located in a folder and save a copy in pdf format.
To make this more flexible, I write the path to the destination folder in cell K5 of my master file.
Example path:
P:\01-Qualité\K - Qualité Usinage\05 - CREATION PCP PREMIER NIVEAU\GCU A VALIDER\CA VERSO\FAST\ESSAI COPIE" & "\"
My macro seems to work, but the PDFs are not saved in the “ESSAI COPIE” folder which contains the workbooks, but in the K - Qualité Usinage” file.
> Do you have a solution to save the pdf in the source file (“ESSAI COPIE”)?
> Additionally, how can I avoid blocking the macro if the file already exists?
VBA Code:
Sub enregistrer_copie_pdf()
Dim oFSO As Object
Dim oDossier As Object
Dim oFichier As Object
Dim I As Integer
Dim wb As Workbook
Dim wb1 As Workbook
Dim ws As Object
Dim chemin As String, pos&
Dim ws1, ws2 As Worksheet
' Application.ScreenUpdating = False
' MAJ de l'écran
Application.ScreenUpdating = False
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oDossier = oFSO.GetFolder(ThisWorkbook.Worksheets("IMPRESSION").Range("K5").Value & "\")
For Each oFichier In oDossier.Files
' Open each workbook contained in the folder // Ouvrir chaque classeur contenu dans le dossier
Set wb = Workbooks.Open(Filename:=oFichier)
' Get workbook name // Récupérer le nom du classeur
chemin = ActiveWorkbook.Name
pos = InStr(chemin, ".xlsm")
' Save a copy of the file as pdf // Enregistrer une copie du fichier au format pdf
Sheets(Array("PCP A3H", "Métrologie Saisie Manuscrite")).Select
Sheets("PCP A3H").Activate
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=Left(chemin, pos - 1) & ".pdf", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=False 'True
' Close the workbook and move on to the next one // Fermer le classeur et passer au suivant
wb.Close savechanges = False
Next oFichier
Application.ScreenUpdating = True
End Sub