Главное, чтобы проверить, что папка (в которую вы экспортируете) уже существует и что имя файла является действительным. Мои чеки могут охватывать некоторые распространенные случаи, но они не являются исчерпывающими.
Option Explicit
Sub MakeaPDF()
Dim telecomSheet As Worksheet
Set telecomSheet = ThisWorkbook.Worksheets("TELECOM") ' Change sheet's codename and use that instead maybe?
Dim headerInfoSheet As Worksheet
Set headerInfoSheet = ThisWorkbook.Worksheets("Header Info")
Dim lastRowOnTelecomSheet As Long
lastRowOnTelecomSheet = telecomSheet.Cells(telecomSheet.Rows.Count, "A").End(xlUp).Row
' Could you export the range/selection of cells -- rather than assiging the print area?
headerInfoSheet.PageSetup.PrintArea = telecomSheet.Range("A1:F" & lastRowOnTelecomSheet).Address
Dim folderPathStartsWith As String
folderPathStartsWith = headerInfoSheet.Range("D11") & "\Design\_Common\Transmittals\"
Dim folderPathEndsWith As String
Dim filenameEndsWith As String
Select Case LCase$(headerInfoSheet.Range("A1"))
Case "30% design review"
folderPathEndsWith = "30% DESIGN REVIEW\COMM\"
filenameEndsWith = "30%_Design_Review_Xmittal.pdf"
Case "final design review"
folderPathEndsWith = "FINAL DESIGN REVIEW\COMM\"
filenameEndsWith = "Final_Design_Review_Xmittal.pdf"
Case "construction submittal"
folderPathEndsWith = "FINAL ISSUE\COMM\"
filenameEndsWith = "Final_Issue_Xmittal.pdf"
Case Else
MsgBox "Could not determine folder and filename of export. Code will stop running now to prevent unpredictable behaviour."
Exit Sub
End Select
Dim folderPath As String
folderPath = folderPathStartsWith & folderPathEndsWith
If Len(Dir$(folderPath, vbDirectory)) = 0 Then
MsgBox "'" & folderPath & "' is not a valid directory. Code will stop running now."
Exit Sub ' Or you could create the directory here, if it doesn't exist, with MkDir
End If
With headerInfoSheet
Dim pdfFilename As String
pdfFilename = .Range("D14") & "_" & .Range("D15") & "_" & .Range("D18") & "_" & "COMM" & "_" & filenameEndsWith
End With
' You may need to try to check if the filename is valid (if it's not, you may get an error when you go to export it)
If StringContainsReservedCharacters(pdfFilename) Or Len(pdfFilename) > 260 Then
' Or you could replace any illegal characters with a legal character
MsgBox "'" & pdfFilename & "' doesn't appear to be a valid filename. Code will stop running now."
Exit Sub
End If
headerInfoSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=folderPath & pdfFilename
End Sub
Private Function StringContainsReservedCharacters(ByVal stringToCheck As String) As Boolean
Const RESERVED_CHARACTERS As String = "<>:""/\|?*" 'https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file
Dim characterIndex As Long
For characterIndex = 1 To Len(RESERVED_CHARACTERS)
If InStr(1, stringToCheck, Mid$(RESERVED_CHARACTERS, characterIndex, 1), vbBinaryCompare) > 0 Then
StringContainsReservedCharacters = True
Exit Function
End If
Next characterIndex
End Function
Если вы столкнетесь с MsgBox
, надеюсь, вы поймете, почему он не работает.