excel vba save worksheet as pdf.

BRB1983

Board Regular
Joined
Aug 29, 2019
Messages
61
I'm having issues with this code not saving my file. I get an error 1004. it worked before. does any one see any issues with this code?
Code:
Option Explicit
Sub SaveAsPDF()

 Dim strDir As String
   strDir = CreateObject("WScript.Shell").specialfolders("Desktop") & "\Daily Reports\"
     
    If Dir(strDir, vbDirectory) = "" Then
        MkDir strDir
    Else
        'MsgBox "Directory exists."
    End If
    ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
        IgnorePrintAreas:=False
Dim fName As String
With ActiveSheet
    fName = .Range("C7").Value & " " & .Range("F46").Value
    .ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
            strDir & fName, Quality:=xlQualityStandard, _
            IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
End With
MsgBox "SAVE COMPLETE"
Range("C7") = ""
Range("C9:C45") = ""
End Sub
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
What the error message says and on which line the macro stops.
 
Upvote 0
message: document not saved. the document may be open, or an error may have been encountered when saving.

Code debug:
.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
strDir & fName, Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
 
Upvote 0
I'm going to ask the obvious, do you have the pdf open?
 
Upvote 0
.
Interesting issue ... your macro functions as designed here.
 
Upvote 0
Perform the following test to save the file in the same path where you have the Excel file with the macro.

Code:
Sub SaveAsPDF_2()
  Dim strDir As String, fName As String
  strDir = ThisWorkbook.Path & "\"
  With ActiveSheet
    '.PrintOut
    fName = .Range("C7").Value & " " & .Range("F46").Value
    .ExportAsFixedFormat Type:=xlTypePDF, _
        Filename:=strDir & fName, _
        Quality:=xlQualityStandard, IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, OpenAfterPublish:=False
  End With
  MsgBox "SAVE COMPLETE"
  Range("C7") = ""
  Range("C9:C45") = ""
End Sub
 
Last edited:
Upvote 0
i think i know the proble.. cell C7 is date format... can not save a file with "/". ill give it a try with out date.
 
Upvote 0
What version of Excel do you have?
You can save a file as pdf from the Excel Save As Pdf.
 
Upvote 0

Forum statistics

Threads
1,224,737
Messages
6,180,668
Members
452,992
Latest member
TokugawaIesuma

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