Ask if you want to replace file

Jhowss7

New Member
Joined
Aug 11, 2021
Messages
1
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hello everyone,

I have a code that serves its purpose well. Export a selection to pdf. However it replaces a file with the same name. I need it to ask if you want to replace or not
If it is, don't loop until something is done.

The code:

Sub PDF()

lr = Range("M:M").Find("*", , xlValues, , xlByRows, xlPrevious).Row
Range("M1:AE" & lr).Select


Dim pas
pas = Application.GetSaveAsFilename(InitialFileName:=Range("M1") & " " & Format(Now(), "DD-MM-YY hhmm"), _
fileFilter:="PDF, *.pdf", _
Title:="Exportar em PDF")

If TypeName(pas) = "Boolean" Then
Else
Selection.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=pas, Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=True

End If
End Sub
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
VBA Code:
Sub PDF()

    Dim lR&, pas$, fName$, ret

    lR = Range("M:M").Find("*", , xlValues, , xlByRows, xlPrevious).Row
    Range("M1:AE" & lR).Select

    fName = Range("M1") & " " & Format(Now(), "DD-MM-YY hhmm") & ".pdf"

    pas = Application.GetSaveAsFilename(InitialFileName:=fName, _
                                        fileFilter:="PDF, *.pdf", _
                                        Title:="Exportar em PDF")

    If pas <> "False" Then
        al = Dir(fName)
        If Dir(fName) <> "" Then
            ret = MsgBox("There is a file with this name, do you want to overwrite it?", vbYesNo + vbCritical, "Attention !")
            If ret = vbNo Then
                MsgBox "Cancelled..", vbInformation, "File Found!"
                Exit Sub
            Else
                Kill fName
            End If
        End If
        Selection.ExportAsFixedFormat Type:=xlTypePDF, _
                                      Filename:=pas, Quality:=xlQualityStandard, _
                                      IncludeDocProperties:=True, IgnorePrintAreas:=False, _
                                      OpenAfterPublish:=False

    Else
        MsgBox "Stopped..", vbInformation, "Cancelled..."
    End If

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,889
Messages
6,175,223
Members
452,620
Latest member
dsubash

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