I'd like to save my spreadsheet every time I lock my windows pc. Is there a built-in trigger in excel (similar to on-close)? Or might I be able to get a macro to run every minute and check if my machine is in use or locked?
If you really need this you can try the following workaround:
Enable auditing for the lock event in the event viewer, so that event 4800 for locking is logged, as described here.
Create a scheduled task triggered on the lock event. Choose Trigger on Event with Protocol: "Security", Source: "Microsoft-Windows-Security-Auditing" and Event-ID: "4800"
Run a vbscript that uses GetObject(, "Excel.Application") to retrieve the currently active excel session and run a macro that saves for you with Application.Run (like in this example)
Not very straight forward, but on the other hand, the easy way would just be to press ctrl+s everytime before you lock your computer.
You could try this and see if this works instead of using a macro:
Click the Microsoft Office Button , and then click Excel Options.
Click Save.
Select the Save AutoRecover information every x minutes check box.
In the minutes list, specify how often you want the program to save your data and the program state.
But if you specifically want a macro I could try to form one for you.
Okay well add this into a standard module:
Option Explicit
Public RunTime
Sub StartTimer()
RunTime = Now + #12:10:00 AM#
Application.OnTime RunTime, "SaveBook", schedule:=True
End Sub
Sub SaveBook()
ActiveWorkbook.Save
StartTimer
End Sub
Sub StopTimer()
On Error Resume Next
Application.OnTime RunTime, "SaveBook", schedule:=False
End Sub
Place this in your workbook class module:
Private Sub Workbook_Open()
StartTimer
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Then add this into your workbook class module:
Private Sub Workbook_Open()
StartTimer
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
StopTimer
End Sub
Have fun with it and fiddle around with it
Related
I am using Excel 2016 and have this code written in the ThisWorkbook object in VBA:
Private Sub Workbook_Open()
ThisWorkbook.Protect (password = "password")
End Sub
It is not working. The point here is that this should prevent users from touching the Power Query functions in this workbook. I have saved it as a Macro-enabled workbook, I have all macros and events enabled and this is the only workbook open.
I also have this additional code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
ThisWorkbook.Protect (password = "password")
ThisWorkbook.Save
End Sub
And that is not working either. It works fine if I insert that "ThisWorkbook.Protect" code into a general module or worksheet object and run it manually, but when I want this particular excel file to run this code automatically on open or close, it does not do it.
Any ideas what could be causing this?
For some reason running ThisWorkbook.Protect on a protected workbook seems to unprotect it (even though I couldn't find any documentation that says that it does this) so try this:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If Not ThisWorkbook.ProtectStructure Then ThisWorkbook.Protect Password:="password"
ThisWorkbook.Save
End Sub
In order for automatically-running subroutines to work correctly in Microsoft Excel, they must be contained within a Visual Basic module.
If an Auto_Open, Auto_Close, or other automatically-running subroutine is stored "behind" a worksheet or ThisWorkbook, it may not function correctly when you open or close your workbook, or when you perform an action that should cause the subroutine to run.
MS Topic Discussion - Code "Behind" a Workbook
Strange issue when closing a workbook twice from VBA (Workbook_BeforeClose)
Hi. This problem appears to me in an extremely simple workbook: Workbook_BeforeClose only.
Option Explicit
Private Sub Workbook_BeforeClose(Cancel As Boolean)
ThisWorkbook.Close SaveChanges:=False
End Sub
If I open and close the workbook twice, the main Excel screen looks like this, and it's impossible to do something, I can only close it from the status bar:
If all you are trying to do is to not prompt the user to save changes, just play with the appropriate flags to 'trick' Excel that changes have already been saved.
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Me.Saved = True
End Sub
This will allow the workbook to close, without prompting any changes to be saved, but this does not actually save them.
Notice the subtle difference between the words: Me.Saved and Me.Save.
Saved is a property that gets flipped to False when Excel detects changes were made as of the last save.
Save is a method - not a property as above - that actually will save the workbook.
Your workbook is already closing, which is what fired this event to begin with. No need to try to close it again within this event. Just tell Excel that no changes have been made since the last save, and it should close all on it's own - without the prompts.
It's possible you're re-triggering the event. Try something like this:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Static InProgress As Boolean
If InProgress Then Exit Sub
InProgress = True
ThisWorkbook.Close SaveChanges:=False
End Sub
I am attempting to use a self-activating OnTime sub to edit some text in a UF control. However, I find that an Application.OnTime event won't start working until the UserForm is closed.
As per this SO thread, I have placed the self-activating sub in a public sub in a regular module. However, this is to no avail.
UF sub
Private Sub UserForm_Initialize
TEST.loadingdots
End Sub
Regular sub
Public Sub loadingdots
Debug.Print 4
Application.OnTime Now + TimeValue("00:00:02"), "loadingdots"
End Sub
The first "4" gets printed, then nothing. When I close the UF, the procedure gets executed as expected.
How can I work around this?
Answering my own question for future reference
When a UF holds a RefEdit control, an OnTime event won't be executed.
I created a new, blank UF, which caused no issues whatsoever.
Seems like RefEdits need to be approached with caution, similarly to how using a RefEdit control in a modeless userform forces the user to close the Excel application through task manager.
Hy all.
I need some help. I will be very grateful for any help.
I don't know about using VBscript in excel.
I am using excel 2016. I have a workbook which has connections to MySQL for query. I want that the data connections in work book may be refreshed at specific time intervals.
I try this code:
Private Sub Workbook_Open()
Application.OnTime TimeValue("13:55:00"), "MyMacro"
End Sub
Sub MyMacro()
ActiveWorkbook.RefreshAll
End Sub
The workbook is opened but don't refresh data.
Can some one please guide me for adding or editing code for this purpose.
See http://www.snb-vba.eu/VBA_Application.OnTime_en.html
Try
Private Sub Workbook_Open()
Application.OnTime TimeValue("13:55:00"), "Module1.MyMacro"
End Sub
I am going to show a MessageBox before save workbook. I have tried with event handler Sub App_WorkbookBeforeSave() and Sub Workbook_BeforeSave but both doesn't work! Why?
There are my Sub in addin:
Private Sub App_WorkbookBeforeSave(ByVal Wb As Workbook, _
ByVal SaveAsUI As Boolean, Cancel As Boolean)
MsgBox "Good bye! Data is save."
End Sub
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
MsgBox "Good bye! Data is save."
End Sub
UPDATE
I was put them in ThisWorkbook modules belong to Microsoft Excel Objects
This kind of functionality requires...
The code to be in ThisWorkbook module of the Add-In, OR
Using a class to hold the event handling code, while still kicking it off in the ThisWorkbook module
Either way, you need to create an instance of a WithEvents Application object which, while you don't touch the object directly after you create it, enables the events to be captured.
I like the second option (cleaner, makes you look like a boss, etc.). Create a class and call it something. I like to call my class ImAGoodListener. In the class, include the appropriate Subs for any application events you want to use
Public WithEvents App As Application
Private Sub App_WorkbookBeforeSave(ByVal Wb As Workbook, ByVal SaveAsUI As Boolean, Cancel As Boolean)
On Error Resume Next
MsgBox "Good bye!"
End Sub
With the arguments, you can do cool and mischievous things like prevent the workbook from being saved...
(I like On Error Resume Next for this so we don't risk not allowing a user to save his/her workbook should our code go haywire)
In the ThisWorkbook module put something like this...
Dim objAppLis As New ImAGoodListener
Private Sub Workbook_Open()
Set objAppLis.App = Application
End Sub
This will start the event listening when your Add-In is initially opened.
Alternatively, if you're using CustomUI for the ribbon, you use a Ribbon Onload event to trigger the start of listening (I do this when my events are used primarily for ribbon behavior so I can easy disable listening in the CustomUI xml).
Some other application events of use are:
App_SheetActivate
App_WorkbookActivate
App_WorkbookOpen
App_WorkbookBeforeClose
App_WorkbookBeforeSave
Here's a list of all events, but note that some of them are workbook (event handlers start with Workbook_ events which won't work for this sort of thing.
Chip goes into great detail about these sort of events on his site here.