Option Explicit
Sub Créer_PDF_04()
Dim monDossier As String, monFichier As String
Dim sCommande As String, FSO As Object
monDossier = "M:\O_DIP\6_Execution_comptable\liquidation_lyon\Bons_commande\Demande\Fiches_liaisons_et_devis" & "\" & Feuil2.[k1]
monDossier = NomDossierValide(monDossier)
monFichier = Feuil2.[k1]
sCommande = Environ("comspec") & " /c mkdir " & monDossier
Shell sCommande, 0
Delai 250
Set FSO = CreateObject("Scripting.FileSystemObject")
If Not (FSO.FolderExists(monDossier)) Then
MsgBox "Dossier : " & monDossier & " n'existe pas", vbOKOnly + vbCritical
Exit Sub
End If
Set FSO = Nothing
If NomFichierValide(monFichier) = False Then
Feuil2.[k1].Select
MsgBox "Nom de Fichier Invalide", vbOKOnly + vbCritical
Exit Sub
End If
With Feuil2
.PageSetup.BlackAndWhite = False
.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=monDossier & "\" & monFichier, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
End With
End Sub
Private Function Delai(ByVal ms As Long)
Delai = Timer + ms / 1000
While Timer < Delai: DoEvents: Wend
End Function
Private Function NomDossierValide(sChaine As String) As String
Dim i As Long
For i = 1 To Len(sChaine)
sChaine = Replace(sChaine, Mid$(" ", i, 1), "_")
Next i
NomDossierValide = sChaine
End Function
Private Function NomFichierValide(sChaine As String) As Boolean
Dim i As Long
Const sCaracInterdits As String = """*/:<>?\|"
NomFichierValide = True
If Len(sChaine) = 0 Then
NomFichierValide = False
Exit Function
End If
For i = 1 To Len(sCaracInterdits)
If InStr(sChaine, Mid$(sCaracInterdits, i, 1)) > 0 Then
NomFichierValide = False
Exit Function
End If
Next i
End Function