error can't save document when try save as PDF

Alaa mg

Active Member
Joined
May 29, 2021
Messages
343
Office Version
  1. 2019
hello
I try saving specific sheet as pdf but gives error can't save document in this line
VBA Code:
rng.ExportAsFixedFormat Filename:=strFilename, Type:=xlTypePDF, OpenAfterPublish:=True
VBA Code:
Sub SaveAsPDF()
    Dim rng As Range
    Dim strFilename As String
    Dim lr As Long
lr = Sheets("INVOICE").Cells(Rows.Count, 1).End(xlUp).Row
    With Sheets("INVOICE")
        strFilename = .Range("G6").Value & " " & "INVOICE NO "
    End With
   
    Set rng = Sheets("INVOICE").Range("a1:G" & lr)

   
    rng.ExportAsFixedFormat Filename:=strFilename, Type:=xlTypePDF, OpenAfterPublish:=True

End Sub
any idea to fix it ,guys?
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Try this. Let me know if it works.

VBA Code:
Option Explicit

Sub SaveInvoiceAsPDF()
    
    Dim rRangeToSave As Range
    
    Dim sFilename As String
    
    Dim sPathToFile As String
    
    Dim iRows As Long
    
'   Path to the file.
    sPathToFile = ThisWorkbook.Path & "\" '<= change this if the path is different than this workbook's.
    
'   Set name of file to save and range to include.
    With Sheets("INVOICE")
        iRows = Range("A1").Cells(Rows.Count, 1).End(xlUp).Row
        sFilename = "INVOICE NO " & .Range("G6").Value & ".pdf"
        Set rRangeToSave = .Range("a1:G" & iRows)
    End With
    
'   Delete the file if it already exists.
    On Error Resume Next
    Kill sPathToFile & sFilename
    On Error GoTo 0

    rRangeToSave.ExportAsFixedFormat _
        Type:=xlTypePDF, _
        Filename:=sPathToFile & sFilename, _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=False

End Sub
 
Upvote 0
Solution
I've found my bad !
there is character ,the excel prohibit save with this character .

thanks for organization for the code .:)
 
Upvote 0

Forum statistics

Threads
1,215,316
Messages
6,124,226
Members
449,148
Latest member
sweetkt327

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