I'm ripping my hair out here ...
I have an Excel worksheet, that I use for registering time used on different tasks. In the worksheet, I start a "clock" that runs as long as I'm working on a task, in increments of 10 seconds.
This works just fine, as long as there is not another workbook open, that is in safe-mode (I.e. opened from a weblocation, and "enable editing" not activated).
If this is the case, the timer fails with...
"Run-time error '1004':
Method 'OnTime' of object '_Application' failed.
AND...
If the workbook in safemode is the "last workbook to have focus", before going to another window on the computer, the macro will fail.
If the workbook that contains the macro is the last Excel window to have focus, before going to another window on the computer, even if there is also another workbook open in "safemode", the macro does NOT fail.
If another workbook is the "last excel to have focus", but is open in "edit enabled" mode, the macro will NOT fail.
My code:
Dim TimerActive As Boolean
Sub Start_Timer()
TimerActive = True
Timer
End Sub
Sub Stop_Timer()
TimerActive = False
End Sub
Sub Timer()
If TimerActive Then
ThisWorkbook.Sheets("Register_time").Range("InputTimeTo").Value = Time
ThisWorkbook.Application.OnTime Now() + TimeValue("00:00:10"), "Timer"
End If
End Sub
When the macro fails, the debugger highlights the line:
ThisWorkbook.Application.OnTime Now() + TimeValue("00:00:10"), "Timer"
I would like to somehow work around this, so that the macro works "in the background", even if another workbook has focus (safemode or not), or if a completely different program has focus.
Note: I can't have the workbook with the macro gain focus, when the clock needs to update, as this would mean that I would leave the program that I'm working in.
It would, however, be ok, if the clock was not updated, until I choose to set focus back to the worksheet where the clock is displayed.
Anyone?
Try to type this code before the "Ontime" line:
On Error Resume Next
ThisWorkbook.Application.OnTime Now() + TimeValue("00:00:10"), "Timer"
This avoid the unexpected behavior.
Best,
Related
My application currently navigates to a vendor website, logs in, opens a report, clicks a download button behind an IFrame(which was cancer to get working), and hits the open button on the open/save dialog box. Now all thats left for this phase of the project is to have excel wait for the new workbook to open, pull the information into a new sheet, and close the other workbook.
The problem is that no matter what I seem to try the new workbook will never open while the macro is running. If i add a break in the code everything works just fine. I've tried application.wait, sleep, DoEvents, and even a msgbox. I'm kinda stumped here.
'This clicks the button that initiates the open/save dialog box in IE
iWin.contentDocument.querySelector("a[title=Excel][onclick*=EXCELOPENXML]").Click
'set the name of the windows the program should look for
iStr = "Reports LeaveXpert Application - Internet Explorer"
eStr = "Case Detail.xlsx [Protected View] - Excel"
'these are declared as longptr
leHwin = 0
cdHwin = 0
'Grabs the windows handle for the IE window
leHwin = FindWindow(vbNullString, iStr)
'Makes sure the IE window is active
i = SetForegroundWindow(leHwin)
'a little bit of delay
Application.Wait DateAdd("s", 5, Now)
'Send the key combination for the open button to the open browser
Application.SendKeys "%{O}"
cdHwin = 0
Debug.Print cdHwin
'this should wait for the new spreadsheet to open but instead the application hangs here and the new spreadsheet never opens.
Do While cdHwin = 0
'various methods of having the program wait that i've tried
'DoEvents
'Application.Wait (Now() + TimeValue("00:00:01"))
'Sleep 1000
'this should set the variable to the windows handle of the new sheet once it opens causing the loop to stop
cdHwin = FindWindow(vbNullString, eStr)
'This just keeps giving me 0 as an output
Debug.Print cdHwin
Loop
'we never get to this line of code unless I add a break somewhere in the loop, then the sheet opens and I get the windows handle as output.
Debug.Print cdHwin
UPDATE:
IT FINALLY WORKS!!!!!!!!!!!!!!!
I'll have to rethink my single button approach to implementation, but that's a pretty small price to pay. This has been an off and on project for the better part of 2 weeks and has taken me far more time than I anticipated, mostly due to IFrames being absolute cancer (I seriously hate them). But I've learned a lot getting this first part working. Now to start on getting the other two relevant data sources integrated into the spreadsheet. The more I learn about computer programming; the more I realize how little I know about it.
Oh and Mathieu-Guindon is an absolute hero!
UPDATE 2 for posterity.
Nothing to see here. I thought I had resolved the problem, but as it turns out I just need more sleep. Relevant quote from comment discussion.
"I really need to stop staying up till 4 AM reading posts about computer code and trying to work on this thing. I really think that I went to sleep, had a dream about fixing it, woke up and posted about the "fix", and thought it was real."
Update 3: Final Update on this question. Relevant explanation is in the comments on the following code.
'I made 2 changes that seem to have resolved the issue.
Private Sub AppEvents_WorkbookOpen(ByVal Wb As Workbook)
If Left(Wb.Name, 11) = CaseDetailFileName Then
'This sub that handles the transfer of data from the newly opened workbook to this workbook.
'It had an internal call to close the newly opened workbook that I forgot about, so I removed it
ImportCase Wb
'a duplicate call also existed out here, there were also a few duplicate ".activate"
'commands inside the import sub and this if statement that I removed
Wb.Close
'This sub handles data sorting and analytics
CaseFAS
End If
'I had tried copying this line from Workbook_Open a few times before and it
'didn't resolve the issue by itself,
'but I'm reasonably sure it is part of the solution
Set AppEvents = Me.Application
End Sub
A peek under the hood of ImportCase for completeness sake.
'This sub handles importing the optis workbook when it opens. It's called by
'the AppEvents_WorkbookOpen sub declared in ThisWorkbook
Public Sub ImportCase(ByVal book As Workbook)
Dim srcSht As Worksheet
Set srcSht = ThisWorkbook.Sheets("OptisSource")
'This line clears out any old data
srcSht.Cells.Clear
'Sets the newly opened workbook to be the active sheet
book.Sheets("Case Detail").Activate
'Copys the information and sends it to a predefined sheet in this workbook
ActiveSheet.UsedRange.Copy Destination:=srcSht.Range("A1")
'sets the newly imported information to the active sheet
srcSht.Activate
'This sub clears out ghost data
ClearNulls
End Sub
What if we just let Excel do its thing, and go with the flow of an event-driven / desynchronized paradigm?
The Excel.Application class raises a number of events you can handle in any class module, but the ThisWorkbook module can do - you declare a WithEvents module-level variable:
Option Explicit
Private WithEvents AppEvents As Excel.Application
And then you initialize it with an Application object reference, perhaps on open - if we're in the ThisWorkbook module, we're inheriting an Application property from the base Excel.Workbook class:
Option Explicit
Private WithEvents AppEvents As Excel.Application
Private Sub Workbook_Open()
Set AppEvents = Me.Application
End Sub
Declaring a WithEvents variable makes it available in the top-left dropdown, and then we can pick a member to implement with the top-right dropdown:
One of the events we can handle at the Application level, is a rather convenient WorkbookOpen event that gives us a Wb As Workbook reference to the workbook that was just opened:
Private Sub AppEvents_WorkbookOpen(ByVal Wb As Workbook)
End Sub
Here you get to intercept every workbook that opens during a macro-enabled session, as long as this AppEvents reference remains in-scope - that is, until ThisWorkbook is closed.
So the ThisWorkbook might look like this:
Option Explicit
Private Const CaseDetailFileName As String = "Case Detail.xlsx"
Private WithEvents AppEvents As Excel.Application
Private Sub Workbook_Open()
Set AppEvents = Me.Application
End Sub
Private Sub AppEvents_WorkbookOpen(ByVal Wb As Workbook)
If Wb.Name = CaseDetailFileName Then
DoThingWithCaseDetailWorkbook Wb
End If
End Sub
And then Public Sub DoThingWithCaseDetailWorkbook(ByVal book As Workbook) could be defined in any standard module and have access to any module state/variable that was set before the WorkbookOpen event fired.
Now the fact that the file is being opened in protected mode (from an untrusted location?) might interfere with this, but then there should be a way to get the script to save the file elsewhere if it's a problem.
I am trying to run foo in Book1.xlsm that runs another sub, bar, in Book2.xlsm. Following is the code for both:
In Book1:
Sub foo()
Dim oldBook As Workbook: Set oldBook = ActiveWorkbook
Dim newBook As Workbook: Set newBook = Workbooks("Book2.xlsm")
Application.Run "'Book2.xlsm'!Module1.bar", oldBook
End Sub
In Book2:
Sub bar(book As Workbook)
book.Close False
Debug.Print "Closed Workbook!"
MsgBox "Closed workbook!"
End Sub
The execution of foo runs perfectly fine and the control is passed onto bar in Book2.xlsm. The line book.Close false runs perfectly fine (closes Book1.xlsm), but code execution stops immediately without any warning (no messages in console or popup). Shouldn't both lines be executed in Book2.xlsm since the control was passed to bar?
I tried this by calling bar workbooks(1) (workbooks(1) = Book1.xlsm) and the entirety of the code runs perfectly fine, as expected. Shouldn't the same happen
How do I preserve code execution in the first scenario, i.e. foo runs bar, change workbooks, close Book1.xlsm and continue execution of bar?
Reconsider who's responsible for what. It's not normal that a workbook is just given any random Workbook object and proceeds to Close it.
Sure you can hack around the fact that you're essentially closing the owner of the call stack, but the root of the problem is, when you have WorkbookA responsible for doing something, and that something involves opening or creating WorkbookB, then it's WorkbookA's responsibility to close WorkbookB when it's done.
Book2 has no business closing anything. Assuming the real macro actually does something more than just closing it, then it should do its thing and then let the caller decide whether to close the workbook or leave it open; Book2!Bar is given a resource that it doesn't own: it's beyond its responsibility to close that resource.
Maybe this can serve as a simple illustration:
Public Sub DoSomething()
Set t = New Thing
UseTheThing t
t.SomeMethod ' error 91: object reference is gone!
End Sub
Private Sub UseTheThing(ByRef t As Thing)
t.Foobar 42
Set t = Nothing ' not your job!
End Sub
Working around the natural order of things (caller -> callee -> back to caller) is neither recommended nor useful. Something is broken in the bigger picture - take a step back and fix the higher level instead of fighting the entire paradigm.
You can use Application.OnTime to accomplish this task.
Book2.xlsm
Public wb As Workbook
Public Sub bar(book As Workbook)
'set the variable for the workbook we want to close - We do this because we cannot pass a workbook object
Set wb = book
'set it to call 1 second from now
Application.OnTime DateTime.DateAdd("s", 1, DateTime.Now), "CloseIt"
End Sub
Sub CloseIt()
wb.Close False
Debug.Print "Closed Workbook!"
MsgBox "Closed workbook!"
End Sub
Even if you use an add-in you may face this problem. I found using the below function very useful in handling such problems. Your call stack root won't be in Book1:
Application.OnTime Now + TimeValue("00:00:01"), "'Book2.xlsm'!Module1.bar", oldBook
How can I reopen my workbook at the last active sheet when I click on a hyperlink that runs ForceReopen? What I have fails because LstSht is not set. (Note that I do not want to save changes when I run ForceReopen.)
' Workbook module
Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
Set LstSht = Sh
End Sub
' Standard module
Public LstSht As Worksheet
Sub ForceReopen()
Application.OnTime Now + TimeValue("00:00:01"), "GoToLast"
ThisWorkbook.Close False
End Sub
Sub GoToLast()
LstSht.Activate
End Sub
You need to store the name of the last active sheet somwhere. Since you don't want to save the file on close, it can't be in the file itself.
One possibility is to create a small text file that contains just the sheet name to activate on file open. The workbook open event can then read the text file and activate the specified sheet.
The workbook activate event should update the text file. Provide error handlers to allow for the text file not existing, or the specified sheet not existing. Depending on how robust you want to make it, you might need to handle the sheet name changing too.
Location of the text file is a design choice: maybe the same folder as the Excel file, or some fixed config folder.
Another possibility would be to use the registry rather than a text file.
Remember something during automatic reopen within Application.Caption
You may store the ActiveSheet.Name within Application.Caption during a forced reopening of a workbook, even if all other global variables are lost.
' Within ThisWorkbook:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.Caption = "Last Active: " & ActiveSheet.Name
' Stop ' temporary to see the caption, but restart will not work!
End Sub
' Both following subs within a normal module:
Public Sub ForceReopen()
Application.OnTime Now + TimeValue("00:00:01"), "GoToLast"
ThisWorkbook.Close False
End Sub
Private Sub GoToLast()
Dim iLastSheet As Integer ' Position of text "Last Active" in Caption
iLastSheet = InStr(Application.Caption, "Last Active:")
' Stop ' temporary to see "Last Active: ..." in the caption
If iLastSheet > 0 Then
On Error Resume Next
ThisWorkbook.Sheets(Mid(Application.Caption, iLastSheet + 13)).Activate
On Error GoTo 0
Application.Caption = ""
End If
End Sub
If you save the workbook staying in Sheet1 and start ForceReopen staying in Sheet3 (by button, by hyperlink, by manual execution, whatever) then it reopens at Sheet3.
To check the functionality, you may add the first Stop to see, if Application.Caption is set correctly (workbook will not open afterwards, so you have to delete this Stop after testing):
You may add the second Stop to check, if Application.Caption is set as intended after it reopened automatically:
There is a logical flaw in your plan. Why should you activate the last viewed sheet and then close the workbook without saving that change?
Set a public variable by the name of LstSht. (I would prefer it to be the sheet name rather than the sheet object but that may be a matter of taste.
Set the variable on the Activate event of each sheet. Note that the Activate event may not occur when you first open the workbook. Therefore the variable must also be set in the Workbook_Open event.
In the GoToLast procedure provide for the eventuality that the variable doesn't hold a value - just in case. Use On Error Resume Next. Also make sure that the sheet isn't activated if it is already active. You may make this procedure a function and let it return True if the sheet was changed (for whatever purpose).
The effect of the whole thing, as you have laid it out, would be that, in the middle of whatever you are doing, the last active sheet is suddenly activated and then the workbook closed without saving your work. Charming idea!
I'm facing an odd situation. I have a button on a sheet which runs many functions, being one of those opening another file:
If Not IsItOpen(ENDERECO2) Then
Workbooks.Open Filename:=ENDERECO1
End If
'ENDERECO2 has the file's name
'ENDERECO1 has the full path of the same file
'IsItOpen is a private function as follows:
'Private Function IsItOpen(Name As Variant) As Boolean
' On Error Resume Next
' IsItOpen = Not (Application.Workbooks(Name) Is Nothing)
'End Function
After opening the other workbook, when it isn't already opened, I bring focus to the first sheet, as I want the second one to be opened on the background. To do that, I use:
'At the very beggining of the code
Dim CEL As Range
Set CEL = Selection
'And at the end of it all
CEL.Select
All the described code works perfectly.
The problem I've been having: as this button runs many things at once, I wanted to add an "Application.Screenupdating = False" at the beggining and "... = True" at the end, so it won't flicker too much when calculating. The thing is that when I added the Screenupdating stuff, it will still open the second workbook as desired, but it won't bring the focus back to the main workbook. Instead, it stops at the recently opened workbook and there it stays.
What could be the interference of the Screenupdating on the CEL.Select command?
Any ideas?
Cheers
Thanks Taelsin. Guess when we don't know exactly why, we improvise lol. This worked fine:
If Not IsItOpen(ENDERECO2) Then
Application.ScreenUpdating = True
Workbooks.Open Filename:=ENDERECO1
Application.ScreenUpdating = False
End If
It is good enough. Cheers!
I have an excel book titled Can Opener that is opened by my task scheduler. It opens another workbook on a shared drive that my task scheduler cannot access, and then closes itself.. Can Opener works fine. The problem I am having is that the other workbook has code that, upon open, if it is 7pm system time executes an update macro to get new data from the servers. Once this is done the file saves and closes. The data workbook opens, but no update is occurring, and it does not automatically save and close. I have tied breaking the code down so it would at least update, and this is not working either. I have checked several forums and other locations and I still cannot figure out what the problem is. Can I get a little help?
Sub DataBook_Open()
Application.EnableCancelKey = xlDisabled
If Hour(Now) = 7 And Weekday(Now, vbSunday) < 7 Then
Run_Update
Me.Save
Application.Quit
Else: Me.Save
Application.Quit
End If
End Sub
Run_Update is the name of the macro in my module that merely executes that other macros in the module that create the server connection and run the SQL's. This works fine if manually ran, or run with a button, so I know the error is not here. Also I am trying to run this code every night at 7pm.
I really appreciate any help I can get guys.
Are you sure DataBook_Open() executes at all?
It might not unless you're calling it from Auto_Open() or Workbook_Open().
Try this minimal test files. Replacing the paths with your actual folders.
--can_opener.xlsm--
Sub OpenOtherWorkbook()
Dim sWbkPath As String
sWbkPath = ThisWorkbook.Path & "\" & "test_data.xlsm"
Dim wbkData As Workbook
Set wbkData = Workbooks.Open(sWbkPath)
End Sub
--test_data.xlsm-- in the ThisWorkbook Object
Private Sub Workbook_Open()
If Hour(Now) = 9 Then 'replace with your condition
UpdateData
ThisWorkbook.Save
DoEvents
Else
DoEvents
End If
ThisWorkbook.Close
'Application.Quit
End Sub
Sub UpdateData()
ThisWorkbook.Sheets(1).Range("A1").Value = Format(Now, "yyyy-mm-dd:hh\hmm")
End Sub