I can't open PDF file after save

tubrak

Board Regular
Joined
May 30, 2021
Messages
216
Office Version
  1. 2019
Platform
  1. Windows
Hello
I suppose this code will create folders current year and month and save file as pdf inside month based on current month , but suddenly shows error message can't open this file !
VBA Code:
Sub DateFolderSave()
Application.DisplayAlerts = False
' Check for year folder and create if needed
If Len(Dir("c:\" & Year(Date), vbDirectory)) = 0 Then
 MkDir "c:\" & Year(Date)
End If
' Check for month folder and create if needed
If Len(Dir("c:\" & Year(Date) & "\" & MonthName(Month(Date), False), vbDirectory)) = 0 Then
 MkDir "c:\" & Year(Date) & "\" & MonthName(Month(Date), False)
End If
' Save File
ActiveSheet.SaveAs Filename:= _
"c:\" & Year(Date) & "\" & MonthName(Month(Date), False) & "\" & Format(Date, "mm.dd.yy") & ".PDF" _
, CreateBackup:=False
Application.DisplayAlerts = True
' Popup Message
MsgBox "File Saved As:" & vbNewLine & "c:\" & Year(Date) & _
"\" & MonthName(Month(Date), False) & "\" & Format(Date, "mm.dd.yy") & ".PDF"
End Sub
so what's the problem?
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Instead of ActiveSheet.SaveAs, use ActiveSheet.ExportAsFixedFormat:

VBA Code:
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:="c:\" & _
        Year(Date) & "\" & MonthName(Month(Date), False) & "\" & Format(Date, _
        "mm.dd.yy") & ".PDF", Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, IgnorePrintAreas:=False, _
        OpenAfterPublish:=False
 
Upvote 1
Solution
I hope that this helps.

VBA Code:
Option Explicit

Sub DateFolderSave()

    Dim sPath As String
    
    Dim sPathAndFileName As String

    Application.DisplayAlerts = False
    
'   Check for year folder and create if needed
    If Len(Dir("C:\" & Year(Date), vbDirectory)) = 0 _
     Then MkDir "C:\" & Year(Date)
    
'   Check for month folder and create if needed
    If Len(Dir("C:\" & Year(Date) & "\" & MonthName(Month(Date), False), vbDirectory)) = 0 _
     Then MkDir "C:\" & Year(Date) & "\" & MonthName(Month(Date), False)
     
    sPath = "C:\" & Year(Date) & "\" & MonthName(Month(Date)) & "\"
    
    sPathAndFileName = sPath & Format(Date, "mm.dd.yy") & ".PDF"
    
'   Save file as pdf
    ActiveSheet.ExportAsFixedFormat _
        Type:=xlTypePDF, _
        Filename:=sPathAndFileName, _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=False      '<= change this to true to open after it is created.
        
    Application.DisplayAlerts = True
    
'   Message to user.
    MsgBox "File Saved As:" & vbNewLine & sPathAndFileName

End Sub
 
Upvote 1
thanks guys !
all of your solutions have fixed my problem .;)
 
Upvote 0

Forum statistics

Threads
1,215,077
Messages
6,122,991
Members
449,094
Latest member
masterms

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top