Confirm before overqriting files - VBA

broch

New Member
Joined
Jun 12, 2011
Messages
22
Hi there,

I'm afraid I'm a bit of VBA newbie, so this is probably quite a straightforward thing, I hope that means someone can help me.

I've created a macro (code below) in an excel workbook that saves the workbook as a pdf file and names it according to the contencts of cell J1.

All works fine, the only problem is that if the filename already exists there is no pop up to warn the user before overwriting. The file is simply overwritten.

How do I ensure that the user is prompted before overwriting?

Here is my code:


Code:
 Sub SaveAsPDF()
    '
    ' SaveAsPDF Macro
    '
 
        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
            Range("J1").Value _
            , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
            :=False, OpenAfterPublish:=True
    End Sub

All help will be very much appreciated.

Thanks
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Assuming that the filename in J1 contains the extension ".pdf", try...

Code:
[font=Verdana][color=darkblue]Option[/color] [color=darkblue]Explicit[/color]

[color=darkblue]Sub[/color] SaveAsPDF()

    [color=darkblue]Dim[/color] strPath [color=darkblue]As[/color] [color=darkblue]String[/color]
    [color=darkblue]Dim[/color] strFile [color=darkblue]As[/color] [color=darkblue]String[/color]
    [color=darkblue]Dim[/color] Ans [color=darkblue]As[/color] [color=darkblue]Integer[/color]

    strPath = "C:\Path\"  [color=green]'Or ActiveWorkbook.Path[/color]
    
    [color=darkblue]If[/color] Right(strPath, 1) <> "\" [color=darkblue]Then[/color] strPath = strPath & "\"
    
    strFile = Range("J1").Value  [color=green]'Or strFile = Range("J1").Value & ".pdf"[/color]
    
    [color=darkblue]If[/color] Dir(strPath & strFile) <> "" [color=darkblue]Then[/color]
        Ans = MsgBox("File already exists.  Overwrite?", vbQuestion + vbYesNo, "Overwrite?")
        [color=darkblue]If[/color] Ans = vbNo [color=darkblue]Then[/color] [color=darkblue]Exit[/color] [color=darkblue]Sub[/color]
    [color=darkblue]End[/color] [color=darkblue]If[/color]
    
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=strPath & strFile, _
        Quality:=xlQualityStandard, IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, OpenAfterPublish:=[color=darkblue]True[/color]
            
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
[/font]
 
Upvote 0
Hi Domenic,
Thanks for your help - much appreciated.
However, I can't seem to make it work.

When I run the code you suggest I get "Run Time Error 52 - Bad file name or number"

The cell J1 contains the full path including the .pdf extension.

Any idea what's going wrong?

Cheers
 
Upvote 0
In that case, try the following instead...

Code:
[font=Verdana][color=darkblue]Option[/color] [color=darkblue]Explicit[/color]

[color=darkblue]Sub[/color] SaveAsPDF()

    [color=darkblue]Dim[/color] PathAndName [color=darkblue]As[/color] [color=darkblue]String[/color]
    [color=darkblue]Dim[/color] Ans [color=darkblue]As[/color] [color=darkblue]Integer[/color]

    PathAndName = Range("J1").Value
    
    [color=darkblue]If[/color] Dir(PathAndName) <> "" [color=darkblue]Then[/color]
        Ans = MsgBox("File already exists.  Overwrite?", vbQuestion + vbYesNo, "Overwrite?")
        [color=darkblue]If[/color] Ans = vbNo [color=darkblue]Then[/color] [color=darkblue]Exit[/color] [color=darkblue]Sub[/color]
    [color=darkblue]End[/color] [color=darkblue]If[/color]
    
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PathAndName, _
        Quality:=xlQualityStandard, IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, OpenAfterPublish:=[color=darkblue]True[/color]
            
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
[/font]
 
Upvote 0
Thanks Domenic, you're a star.
This seems to work perfectly now.

I just wish I understood it all!

I'll get there slowly but surely.
 
Upvote 0
I realise I might be pushing my luck, but I have a tiny little niggle that it would be great to iron out. It's not a big thing, but.....

The code falls down if there is a pdf file with the same name as 'PathAndName' open. Is there a way to make the code close the open file before continuing to save the new one?
 
Upvote 0

Forum statistics

Threads
1,216,753
Messages
6,132,514
Members
449,731
Latest member
dasda34

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