I'm using a small piece of code to export the VBProject components before saving the workbook.
Private Sub App_WorkbookBeforeSave(ByVal Wb As Workbook, ByVal SaveAsUI As Boolean, Cancel As Boolean)
If Wb.HasVBProject Then
If Wb.VBProject.Protection = vbext_pp_none Then
If vbYes = MsgBox("Do you want to export the VBProject components?", vbYesNo) Then
ExportWB Wb ' call a function to export the data
End If
End If
End If
End Sub
This code is working fine, but if the Autosave is on the user (me) is asked every few second if it wants to export the project.
I can check if Autosave is On or Off with Wb.AutoSave, but is there a way to check if the event was triggered manually or by Autosave, in order to run the code only when someone require a save, not for the Autosave events?
Vincent
Using ThisWorkbook.AutoSaveOn = False, the effect should be gone. You can place this line e.g. into ThisWorkbook's event Private Sub Workbook_Open.
You can get the info about if AutoSave is currently enabled for this file, from ThisWorkbook.AutoSaveOn (returns True for enabled, or False for disabled).
Related
I have an Excel file that exports a large amount of data to Word (about 100 pages) and I'm trying to turn off autosave during the export to speed it up. I've tried two methods so far but neither seems to do what I expected: (note that the code is in the excel file from which the data is exported)
First method:
oWordfile.AutoSaveOn = False 'oWordfile is type Word.Document
MsgBox "Autosave state: " & oWordfile.AutoSaveOn
I used the msgbox periodically within the code to confirm the autosave state hadn't been changed. The title bar of the Word file I am exporting too indicates it is saving about every 5 minutes while the msgbox popups always indicate that the autosave state is False.
Second method:
oWordApp.Options.SaveInterval = 30 'oWordApp is type Word.Application
After code execution, I confirmed that the AutoRecover save interval had been changed to 30 minutes, but during export, the file still saves about every 5 minutes.
Since neither of these two methods changed the behavior of Word, I figure I must be changing the wrong setting but I'm not sure what other settings control the autosave feature. If any one knows what I'm missing, I'd be grateful for the help.
updates after Byrd's answer:
Third method: cancel save:
MS-Word
Private WithEvents App As Word.Application
Private Sub Document_Open()
Set App = Word.Application
End Sub
Private Sub App_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
If MsgBox("Save Document?", vbYesNo, "Word: BeforeSave") = vbNo Then
Cancel = True
End If
End Sub
Excel
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If MsgBox("Save File?", vbYesNo, "Excel: BeforeSave") = vbNo Then
Cancel = True
End If
End Sub
I used message boxes as a temporary solution as to when a save is wanted to test this method. The message boxes appear any time the code or user initiates a save event in Excel and Word. If the word file is left open the message box also appears when autorecover save event triggers (currently set to 30 minutes). During code execution exporting from Excel to Word, Word still saves at 5-minute intervals without triggering the BeforeSave event.
I have now tested a little further and discovered that even though the title bar indicates saving is happening during the export, the file hasn't actually been saved. Using task manager to force close Word directly after export, I discovered that the file was unchanged from before code execution. Autorecover didn't have a version of the file to recover either. So now I have no idea of what is actually happening - all I know is that the export slows down greatly while the title bar indicates a save is in progress.
Excel
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Cancel = True
End Sub
Word
Private Sub appWord_DocumentBeforeSave _
(ByVal Doc As Document, _
SaveAsUI As Boolean, _
Cancel As Boolean)
Cancel = True
End Sub
I don't know if the user will need to save the document at all during this export but, just canceling the save all together might do it.
I have an excel Workbook that uses Power Query to populate a table. The query pulls information from multiple external Workbooks as well as a table in a Worksheet within the same Workbook.
The Worksheet in the same Workbook also stores changes to columns that are meant to be manipulated. The Worksheet in the same Workbook is part of a loop. It stores information that is pulled into the main table but also stores the changes made.
To make this work correctly, the Workbook needs to be Saved before the storage query runs. If a Save does not occur before running the query, the query will not contain the changes.
This is accomplished easily when clicking the save button. The following code works well:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Application.EnableEvents = False
ThisWorkbook.Save
ThisWorkbook.Connections("Query - Stored").Refresh
Application.EnableEvents = True
End Sub
This does not work when the workbook is closed and save is clicked after clicking the document close.
The BeforeSave event does not fire because it does not get a chance to. The document closes and when it is reopened it shows it as a crashed file and is listed in the recovery list.
Can anyone help me understand why and how to overcome it.
Since this is caused by an Excel crash while closing, it's possible that the Refresh gets messed up when Close is underway.
You could try executing the Refresh before Closing, like this
Private Closing As Boolean
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Closing = True
Application.EnableEvents = False
ThisWorkbook.Save
ThisWorkbook.Connections("Query - Stored").Refresh
Application.EnableEvents = True
End Sub
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If Closing Then Exit Sub
Application.EnableEvents = False
ThisWorkbook.Save
ThisWorkbook.Connections("Query - Stored").Refresh
Application.EnableEvents = True
End Sub
I have an Excel file that runs some ActiveX components. The downside is that it asks to save each time I close the file, even when I didn't make any changes.
I've tried adding the following code to the ThisWorkbook object:
Private Sub Workbook_Open()
ActiveWorkbook.Saved = True
End Sub
However, this doesn't seem to work. It still asks me to save the file when I try to close it without making changes.
Any help?
Ah, yes I have had to deal with this issue myself. Since these components run AFTER the workbook is opened (and after your Workbook_Open() event), you need to run your code when you're closing your workbook.
Private Sub Workbook_BeforeClose(Cancel As Boolean)
ThisWorkbook.Saved = True
End Sub
Should be placed in the ThisWorkbook code module.
To avoid accidentally not saving the workbook when you actually update it, you could try setting your own global variable flag - which should be located in a standard module and have the Public scope. Afterwards, you can use the Workbook_SheetChange event to set this flag when you actually change any content within the workbook:
Standard Module: Add Pub Flag
Public bPromptSave as Boolean
Workbook Module: Event Handlers
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
bPromptSave = True
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
if not bPromptSave then ThisWorkbook.Saved = True
End Sub
I have spreadsheet which uses events upon opening, therefore anytime I try to close the file, the save changes dialog box appears.
Is there a way to prevent this?
You can add a function in VBA to ThisWorkbook to let Excel think that the file has been saved. This will prevent any save prompts.
If you don't want to save your workbook:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Me.Saved = True
End Sub
If you want to save your workbook:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Me.Save
End Sub
Here documentation for deleting work sheet.
So, you need to use as follow:
'Stopping Application Alerts
Application.DisplayAlerts=FALSE
'~~~~~~deleting sheet~~~~~
'Enabling Application alerts once we are done with our task
Application.DisplayAlerts=TRUE
Private Sub Workbook_BeforeClose(Cancel As Boolean)
ThisWorkbook.Close SaveChanges:=False
End Sub
If IsWorkbookOpen("CONTRACT\CONTRACTLIST_Cement.xlsx") Then
x = 0
Else
Application.DisplayAlerts = False
ActiveWorkbook.Close savechanges:=True
Application.DisplayAlerts = True
End If
Hi, despite using the above code, the save as prompt still occasionally appears and affect the program. Does anyone know how to stop it completely? The problem is that after I click save as, it will alert me that it was still open.
Try below code
Its always good to explcilty refer the workbook rather than ActiveWorkbook
Sub test()
If IsWorkbookOpen("CONTRACT\CONTRACTLIST_Cement.xlsx") Then
x = 0
Else
Application.DisplayAlerts = False
ThisWorkbook.Save
ThisWorkbook.Close False
Application.DisplayAlerts = True
End If
End Sub
You can use Workbook_BeforeSave Event in the ThisWorkbook object to capture the user selecting SaveAs (or using the keyboard shortcut), which would result in the Save As prompt being displayed and the SaveAsUI being set to true. If SaveAsUI is true, this means the user is trying to save the file as something else, so you can then cancel the save As operation.
Open up the Visual Basic Window (Alt + F11) and put the following code in ThisWorkbook.
Disable Save As
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If (SaveAsUI = True) Then
MsgBox "Sorry. I can't have you saving this file as something else."
Cancel = True
End If
End Sub
You can delete the MsgBox line if you want; i put it there as an example if you wanted to notify the user that the function was disabled
To disable both the Save and Save As functionalities, you would remove the if statement and cancel the Save operation, regardless of if the Save as prompt is displayed.
Disable Save and Save As
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
MsgBox "Sorry. I can't have you saving this file at all."
Cancel = True
End Sub
If you just want to disable the Save operation, you would only need to look for the occurrence where the user is saving, but the SaveAsUI is not being displayed (i.e. user is simply saving the file).
Disable Save
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If (SaveAsUI = False) Then
MsgBox "Sorry. I can't have you saving any updates to this file."
Cancel = True
End If
End Sub
Finally, note that the user will still get a prompt to save if the user simply closes the file. The user won't be able to save and the file will close, but if you want the experience to be a bit cleaner, you'll need to make an additional change. When a user closes a file, Excel checks the ThisWorkbook.Saved variable to see if the file has been saved. If it is false, it will prompt the user to Save the file. To prevent this as well, we can set this boolean to true without saving, thus "tricking" Excel into thinking the file has been saved
Disable Save and Save As, including after User attempts to close file
Add the following code after your Workbook_BeforeSave code.
Private Sub Workbook_BeforeClose(Cancel As Boolean)
ActiveWorkbook.Saved = True
MsgBox "Click OK to continue closing this file. Changes will not be saved."
End Sub