Forum

Save selected sheet...
 
Notifications
Clear all

Save selected sheets as PDF

2 Posts
2 Users
0 Reactions
88 Views
(@cucharon)
Posts: 29
Trusted Member
Topic starter
 

Hello,

I would like to be able to select some sheets of the excel file attached, lets say 1,2 and 8, and save it as an only PDF in an especific file. The problem is that when I run the macro, it appears the message "It looks like there is an existed file with the same name to save open. Please close the file and try again" and nothing is saved.

Could anyone tell me what's wrong with the code?

 

Sub SaveAsPDF()

Dim FileName As String
Dim MyDN As String
Dim MyDate As String
Dim MyTime As String
Dim ReplaceFile As VbMsgBoxResult
Dim OverWrite(1) As Boolean
Dim NewPath As String

OverWrite(0) = False
OverWrite(1) = True

Call Entry_Point

MyDN = ThisWorkbook.Sheets("1").Range("C1")
MyDate = ThisWorkbook.Sheets("1").Range("S11")
MyTime = ThisWorkbook.Sheets("1").Range("Z1")
FileName = ThisWorkbook.Path & "2_PDFs_DeliveryNote_Valladolid" & MyDate & "_" & MyTime & "_DeliveryNote_Valladolid"
NewPath = FileName & ".pdf"

If Len(Dir(NewPath)) > 0 Then 'file exists
ReplaceFile = MsgBox("File for " & MyDN & "already exists. do you want to overwrite?", vbOKCancel, "Overwrite?")
If ReplaceFile = vbCancel Then
OverWrite(0) = False
Exit Sub
End If
End If

On Error GoTo Handle:

ActiveSheet.ExportAsFixedFormat xlTypePFD, FileName

MsgBox "PDF file for D.N. " & MyDN & "at " & MyTime & "was created" & vbNewLine & "It has been save in the file 02_PDFs_DeliveryNote_Valladolid on the same directory as this workbook.", , "Well done"

Call Exit_Point
Exit Sub

Handle:
MsgBox "It looks like there is an existed file with the same name to save open. Please close the file and try again."

Call Exit_Point
End Sub

 

Thanks in advance,

Cristina

 
Posted : 09/02/2022 1:48 pm
(@debaser)
Posts: 836
Member Moderator
 

I think this:

ActiveSheet.ExportAsFixedFormat xlTypePFD, FileName

should be:

ActiveSheet.ExportAsFixedFormat xlTypePDF, NewFile

 
Posted : 12/02/2022 7:37 am
Share: