Save the document created with VBA

S Oberlander

Board Regular
Joined
Nov 25, 2020
Messages
147
Office Version
  1. 365
Platform
  1. Windows
My excel file is blocked from savings with a beforeclose even handler, however, to offer an alternative way to save I created the below sub.
It works great when I step thru it, but when I just click on the button linked to it, it tries to save the blocked workbook so nothing gets saved...
VBA Code:
Sub savecopy()

    Sheets("Pivot").Copy
    ActiveSheet.Shapes.Range(Array("Button 1")).Delete
    With Application.FileDialog(msoFileDialogSaveAs)
        .InitialFileName = ThisWorkbook.Path & "\"
        If .Show Then .Execute
    End With
    
End Sub
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
You may need to temporarily disable events so that your save attempt does not trigger your BeforeSave event to fire.
See if this works:
Rich (BB code):
Sub savecopy()

    Sheets("Pivot").Copy
    ActiveSheet.Shapes.Range(Array("Button 1")).Delete
    Application.EnableEvents = False
    With Application.FileDialog(msoFileDialogSaveAs)
        .InitialFileName = ThisWorkbook.Path & "\"
        If .Show Then .Execute
    End With
    Application.EnableEvents = True
    
End Sub
 
Upvote 0
The things is, that when I step thru the save-as is applying to the newly created workbook, but when I click on the button the save-as is applying to the initial workbook.
So when I used Application.EnableEvents, it simply did a save-as on the blocked workbook.
 
Upvote 0

Forum statistics

Threads
1,214,952
Messages
6,122,454
Members
449,083
Latest member
Ava19

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