Hello
I'm stuck when try save file as pdf to current month but it gives error
can't save document or document is open in this line
Rng.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PDFfile, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
Sub ExportSheetToPDF()
'
Const openPDFwhenDone = True
'Set constant basePath to the file save path
Const basePath = "C:\Users\PC WORLD\Desktop\TextFolder\"
'Declare variable pdfPath which for the complete path & filename
Dim pdfPath As String
Dim lr As Long
Dim WS As Worksheet
Set WS = Sheets("ST")
lr = WS.Cells(Rows.Count, 1).End(xlUp).Row
Set Rng = WS.Range("a1:F" & lr)
'get the name of the "year" folder
pdfPath = basePath & Year(Date)
'if the "year" folder doesn't exist then create it
If Dir(pdfPath, vbDirectory) = "" Then MkDir pdfPath
'get the name of the "month" folder
pdfPath = pdfPath & "\" & Format(Month(Date), "00")
'if the "month" folder doesn't exist then create it
If Dir(pdfPath, vbDirectory) = "" Then MkDir pdfPath
'get the complete pdf filename
If Right(PDFfile, 1) <> "\" Then PDFfile = PDFfile & "\"
PDFfile = PDFfile & WS.Range("F6")
'export active worksheet as PDF to pdfPath
Rng.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PDFfile, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
'make sure the pdf file was created
If Dir(pdfPath) = "" Then
'file not found
MsgBox "Something went wrong. (PDF wasn't created.)"
Else
'Success! Show success message (unless PDF was set to auto-open)
If Not openPDFwhenDone Then MsgBox "File Saved As:" & vbLf & pdfPath
End If
End Sub
the code should save in folder 06 within folder 2023 and the name file is based on cell F6 and should save the range A1: F, not the whole sheet
thanks