Application.OnTime stops firing after changing the active document - vba

I am trying to implement an autosave function for all documents created from a certain template. In this template I have created the following for tests:
Dim doc As Document
Dim count As Integer
Private Sub Document_Open()
count = 1
Set doc = ActiveDocument
SaveTime
End Sub
Sub SaveTime()
Application.OnTime When:=Now + TimeValue("00:00:15"), _
name:="DoSave"
End Sub
Sub DoSave()
doc.SaveAs2 "c:\test\testsave" & count & ".docx"
count = count + 1
SaveTime
End Sub
Now if I open a document thats created by this template the autosaving works every 15 seconds as intended (15sec + the counter with different name is just for testing).
BUT as soon as I create a new document in Word or open another file the autosaving in the first document stops working and also doesnt come back if I continue to work in the document.
How can I make the autosave work no matter of which document is active? Like this the feature would only work if only one Document is open at a time, which I can not garantuee of course.

Shot in the dark here, but you are dimming doc within the scope of the module (from what I can tell). It is possible that opening the new document causes this to go out of scope. It would be best to just pass the document between routines.
Something like this:
' This will declare count within the scope of the module
Private count As Integer
Private Sub Document_Open()
count = 1
SaveTime ActiveDocument
End Sub
Sub SaveTime(doc as Document)
Application.OnTime When:=Now + TimeValue("00:00:15"), "'DoSave doc'"
End Sub
Sub DoSave(doc as Document)
doc.SaveAs2 "c:\test\testsave" & count & ".docx"
count = count + 1
SaveTime
End Sub
I haven't used OnTime much, so if the syntax above doesnt allow arguments to be passed, here is a version using a Private doc variable.
' This will declare count and doc within the scope of the module
Private count As Integer
Private doc as Document
Private Sub Document_Open()
count = 1
SaveTime
End Sub
Sub SaveTime()
Application.OnTime When:=Now + TimeValue("00:00:15"), "DoSave"
End Sub
Sub DoSave()
doc.SaveAs2 "c:\test\testsave" & count & ".docx"
count = count + 1
SaveTime
End Sub
I hope this helps. I mostly work within Excel for VBA so I apologize if I am way off the mark here.
EDIT:
I figured it out. What is happening is every time your save event fires it is saving the active document as a new file, but it is not creating a new instance. For example, if test is your first document then:
Test Opens > Event Fires > Test becomes Test2.docx ( Event Fires > Test2 becomes Test3 etc.
Since the code is still stored in memory somehow (this is the part I dont fully understand, but it is, from what I can tell, is what's happening) the event still fires. The problem is, opening a new document somehow refreshes this and the event cancels.
There is a simple workaround, see below:
Option Explicit
Private doc As Document
Private count As Integer
Private Sub Document_Open()
count = 1
Set doc = ActiveDocument
SaveTime
End Sub
Sub SaveTime()
Application.OnTime Now + TimeValue("00:00:15"), "DoSave"
End Sub
Sub DoSave()
doc.Save
Application.Documents.Add doc.FullName
ActiveDocument.SaveAs2 "c:\test\testsave" & count & ".docx"
ActiveDocument.Close
count = count + 1
SaveTime
End Sub
This creates a new instance of the document, saves this new instance, and then closes it. This leaves the old instance in-tact and running.
I hope that all makes sense. Admittedly, I am not clear on why the code remains in memory even when the document holding the code no longer exists.

Related

Word VBA delete hidden bookmarks before closing

I want to set up a VBA so that for any document based on a template hidden bookmarks are deleted prior to the document closing. We publish documents on our website. They are written as Word and an API converts them to html. If there are hidden bookmarks they appear as links on the website (the hidden bookmarks convert to html anchors). Currently we remove the bookmarks manual prior to the API, but this is time consuming (we publish 1000s of documents a year) and ineffective (people forget).
I found VBA to remove hidden bookmarks which works and tried to add DocumentBeforeClose as the trigger. But it doesn't work:
Private Sub DocumentBeforeClose(cancel As Boolean)
Dim nBK As Long
With ActiveDocument
For nBK = .Bookmarks.Count To 1 Step -1
If LCase(Left(.Bookmarks(nBK).Name, 3)) = "_hl" Then
.Bookmarks(nBK).Delete
End If
Next nBK
End With
ActiveDocument.Save
End Sub
I went through Visual Basic Window, Normal, Microsoft Word Objects, ThisDocument.
Nothing happens, the hidden bookmarks remain if I close and re-open the document.
I think you need to add this line :
.Bookmarks.ShowHidden = True
Like this it should work :
Private Sub DocumentBeforeClose(cancel As Boolean)
Dim nBK As Long
With ActiveDocument
.Bookmarks.ShowHidden = True
For nBK = .Bookmarks.Count To 1 Step -1
If LCase(Left(.Bookmarks(nBK).Name, 3)) = "_hl" Then
.Bookmarks(nBK).Delete
End If
Next nBK
End With
ActiveDocument.Save
End Sub
This has solved it:
Sub AutoClose()
On Error Resume Next
Dim nBK As Long
With ActiveDocument
.bookmarks.ShowHidden = True
For nBK = .bookmarks.Count To 1 Step -1
If LCase(Left(.bookmarks(nBK).Name, 3)) = "_hl" Then
.bookmarks(nBK).Delete
End If
Next nBK
End With
ActiveDocument.Save
End Sub
Only issue is that it tries to run when you open a document and puts up an error message that there is no active document. 'On error resume next' is to stop that error message

Wait until user has stopped typing in ComboBox to run macro (VBA)

I'm programming a sort of cross-reference database. An ID is generated based on the document name chosen or created.
The ComboBox I've referred to in the title acts on change (after 3 letters), checks the database for similar entries to what was typed, and displays the drop downof options that match. Once an entry is picked from the match list or a new name created - the appropriate number is generated.
Since the DropDown list is generated after every letter is typed, it takes a while to type what you want. I want to wait a few seconds after the last change to run the macro.
Any Ideas on how I can accomplish this?
An alternative using Application.OnTime again:
In Userform:
Private Sub ComboBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
StartTimer
End Sub
In Module:
Public RunTime As Double
Public Sub StartTimer()
On Error Resume Next
Application.OnTime EarliestTime:=RunTime, Procedure:="YourCode", Schedule:=False
RunTime = Now() + TimeValue("00:00:03")
Application.OnTime RunTime, "YourCode"
End Sub
Public Sub YourCode()
MsgBox "It's working!"
End Sub
This is a bit tricky as VBA doesn't support multi-threading. But we can use the Application.OnTime event to trigger a test in the future to test if the last key event is at least 3 seconds ago.
In a Module insert:
Option Explicit
Public LastKeyEvent As Date
Public Const WaitTimeValue As String = "00:00:03" 'test for key event in 3 seconds
Public Sub TestKeyEvent()
'test if last key event is at least 3 seconds ago.
'If so: run your search or message box
'If not: do nothing
If LastKeyEvent <> 0 And LastKeyEvent + TimeValue(WaitTimeValue) <= Now Then
LastKeyEvent = 0 'Ensure this is only triggered once:
'If we don't do this and multiple keys are pressed within 1 second
'then it would run multiple times.
MsgBox "3 seconds without keypress, we can start search"
'start your search here (instead of message box) …
End If
End Sub
Now you can use for your textbox change event eg TextBox1:
Private Sub TextBox1_Change()
Dim alertTime As Date
LastKeyEvent = Now 'remember when the last key event was
alertTime = LastKeyEvent + TimeValue(WaitTimeValue)
Application.OnTime alertTime, "TestKeyEvent" 'run TestKeyEvent in 3 seconds
End Sub
Note:
This is a workaround that works for 2 or more seconds. But does not for less then 2 seconds.

Print keeps doing double sided (I only want one per page)

I have built the following VBA code:
Option Explicit
Private Sub PrintBtn_Click()
Worksheets.PrintOut from:=FromBox.Value, to:=ToBox.Value, Copies:=Copies.Value
End Sub
Private Sub CloseBtn_Click()
Unload Me
End Sub
Private Sub UserForm_Initialize()
Copies.Value = 1
FromBox.Value = 1
Dim i As Long
For i = 1 To ActiveWorkbook.Sheets.Count
SheetBox.Value = SheetBox.Value & i & " - " & ActiveWorkbook.Sheets(i).Name & vbCrLf
Next i
ToBox.Value = i - 1
End Sub
Basically populates a userform with the names of each worksheet in excel. There's a from box and a to box, and I'm trying to give the user an ability to print out each page individually. It works, the print function works, but rather than printing out each one on a separate page, after the first page it makes everything double sided!
Basically I want to print out sheets X through Y without having it be double sided. The usual print dialog box does not work and also makes it double sided. Perhaps it is a problem with the worksheets themselves, but I do not know what setting I can focus on or change to do so.

Excel: Recalculating every x seconds

One of my spreadsheets deals with various calculations involving, among other things, the current date and time and it would be nice to have it automatically refresh itself once in a while instead of manually having to either press F9 or altering one of the cells.
Is there some way in Excel to set a spreadsheet to automatically recalculate itself every x seconds?
I haven't been able to find a setting in Excel itself, perhaps indicating that no such feature exists. If not, can this be achieved with VBA? (The latter may or may not sound like a silly question, but I have no prior experience with writing Excel macros and as such have no idea what its capabilities are in terms of manipulating spreadsheets.)
This code will create a clock, updated every 10 seconds.
Note that it only refreshes specific cells, and not the entire workbook - this means that you can leave the calculation options at whatever you are happy with:
Dim SchedRecalc As Date
Sub Recalc()
'Change specific cells
Range("A1").Value = Format(Now, "dd-mmm-yy")
Range("A2").Value = Format(Time, "hh:mm:ss AM/PM")
'or use the following line if you have a cell you wish to update
Range("A3").Calculate
Call StartTime ' need to keep calling the timer, as the ontime only runs once
End Sub
Sub StartTime()
SchedRecalc = Now + TimeValue("00:00:10")
Application.OnTime SchedRecalc, "Recalc"
End Sub
Sub EndTime()
On Error Resume Next
Application.OnTime EarliestTime:=SchedRecalc, _
Procedure:="Recalc", Schedule:=False
End Sub
and, to make sure it stops, in the This Workbook module:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
EndTime
End Sub
Goto Developer Visual basic Editor - Right Click workbook - insert module (make sure you have manual calculation
in the module
Sub run_over
Timetorun = Now + timevalue("00:00:10")
application.ontime timetorun,"Refresh_all"
End Sub
Sub Refresh_all
Activeworkbook.Refreshall
End Sub
Sub auto_close()
Application.OnTime timetorun, Refresh_all, , False
End Sub
Change the timing in "00:00:00" format as required

Powerpoint macro - basic problem with counter

I am making my first Powerpoint 2007 macro and I am having a bit of trouble with it hanging, and not letting me move on to the next slide. I can press ESCAPE to quit the slideshow, but pressing space bar or anything else won't progress to the next slide. After a while, it just crashes. I come from a C++/Java background so I think its just something basic that I'm missing.
Basically I am trying to do a counter slide that counts the days/minutes/seconds from a particular date. When the slide loads I want it to show, in real time, how long its been since that date. I've put it through an infinite loop, which works fine to update the time, but then doesnt let me move on to the next slide.
Here's my code:
Sub OnSlideShowPageChange(ByVal SSW As SlideShowWindow)
'If SSW.View.CurrentShowPosition = 3 Then
Do While SSW.View.CurrentShowPosition = 3 ' infinite loop
Dim currentSlide As Integer
currentSlide = SSW.View.CurrentShowPosition
Dim startDate As Date
Dim currentDate As Date
Dim sngDiff As Single
Dim lngDays As Long
Dim lngHours As Long
Dim lngMinutes As Long
Dim lngSeconds As Long
startDate = #7/22/2011 2:00:00 PM#
currentDate = Now
sngDiff = currentDate - startDate
lngDays = CLng(sngDiff)
sngDiff = sngDiff - lngDays
lngHours = Hour(sngDiff)
lngMinutes = Minute(sngDiff)
lngSeconds = Second(sngDiff)
With ActivePresentation.Slides(currentSlide)
With .Shapes(2)
.TextFrame.TextRange.Text = "It has been:" & lngDays & " Days " & lngHours & " hours " & lngMinutes & " minutes " & lngSeconds & " Seconds"
End With
End With
DoEvents
Loop
End Sub
Do I need to listen for some sort of button click to stop this infinite loop, or how do I do this?
Thanks.
A user form is something you add in the VBA editor; it's what you'd normally think of as a dialog box, though forms can be used for other things and needn't even become visible; that's what we're going to do here:
Option Explicit
Public bFormCodeRunning As Boolean
Sub FormDemo()
' Set a flag to let us know the code in the form
' is running
bFormCodeRunning = True
' "show" the form
UserForm1.Show vbModeless
End Sub
Sub KillForm()
' call this at some other point in the presentation
' when you're sure you're done running the form code
If Not bFormCodeRunning Then
Unload UserForm1
End If
' You could actually call this from your slide change event handler
End Sub
Then Insert, User Form from the menu to add a new form; doubleclick it to view its code and add this:
Private Sub UserForm_Activate()
' Don't show my face
Me.hide
DoEvents
' prove that the form's loaded
MsgBox "I'm well-formed"
DoEvents
' and put your other code here
' and when the code's done, flag it
bFormCodeRunning = False
End Sub
For doing a time delay in a VBA context it is usually better to use a form_timer object so in your code have:
If SSW.View.CurrentShowPosition = 3 Then
Me.TimerInterval = 1000
Else
Me.TimerInterval = 0
End If
Or something similar. Then in the form timer code have your clock update code
Private Sub Form_Timer()
// Your clock update code here
End Sub
It's been years since I've done any VBA so I'm a bit rusty but I hope this helps. In general use timers instead of loops for threading tasks, VBA doesn't cope well with them.
The problem is that your routine "owns" the app; until it exits, you won't be able to do anything manually (ie, advance to the next slide).
Whether or not you use a timer on a form (and fwiw, the Timer control isn't shipped with VBA as it is with VB), I think a form may be your solution.
Have your event handler load a form modelessly then exit.
The code in the form can then do any mods to slides or whatever else you want.
Include DoEvents often enough that you don't slow down the main app, but the code in the form will run independently of what the main app is doing.
You don't need to make the form visible (and probably don't want to).