How to make Application.key Shortcuts WorkBook Specific? - excel

I added code to a series of similar files for different projects.
I defined a (Control + R) "^ + R" shortcut to let users see the current record in a userform.
I added application.key code on workbook activating and deactivating event, so the shortcut can be used when several files of this type are open.
My problem is even if the form opens and reads the data from the respective workbook, the userform is not called from the respective file!
Should I localize the procedure as well?
Here are my codes:
The Code in ThisWorkbook
Private Sub Workbook_Activate()
With Application
.OnKey "^R", "ReadCurrentRecord"
End With
End Sub
Private Sub Workbook_Deactivate()
Application.OnKey "^R"
End Sub
The Code in a general Module
Sub ReadCurrentRecord()
If ActiveCell.Worksheet.Name = "OSW" Then
If OSWRng Is Nothing Then
Set OSW = ThisWorkbook.Sheets("OSW")
Set OSWRng = OSW.Range("a5:bz2000")
End If
FrmWODetails.Tag = OSW.Cells(ActiveCell.Row, 14)
FrmWODetails.UserForm_Activate
FrmWODetails.Show vbModeless
End If
End Sub
All the Names are same in the files.

thisworkbook and activecell may not be what you expect them to be.
if this is an addin, this workbook will refer to the addin while active cell wont. i don't even know if activate a cell can be used in an add-in' sheet... I'm thinking not, I'll test that then i get to s computer for my own knowledge.
it's almost always best to fully qualify the object, sometimes from the application level but normally the workbook will suffice
personally, I prefer codenames over names and babes over indexes but they have a few drawbacks that keep them from being my#1 choice

Related

How to have excel wait for a workbook to open

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.

Normal.dot and Personal.xlsb

For using Word I have many public subs and functions stored in Normal.dot. I can use all of them in any module I write in any other Word project. For instance this elementary function stored in Normal.dot,
Public Function BooleanString(b As Boolean) As String
If b Then BooleanString = "Yes!" Else BooleanString = "No!"
End Function
can be used from other projects as
Sub TestNormal()
Debug.Print BooleanString(True)
End Sub
Using Excel I cannot duplicate this behavior: Public subs and functions I have stored in Personal.xlsb (among then the one above) seem not to be visible when called from a module in the VBAProject belonging to any other spreadsheet than Personal.xlsb, resulting in “Sub or Function not defined”.
I have made sure that the Excel option box “Have trust in the VBA project model” (translated from the Danish version) is checked. Also, using “View”, I have made sure that Personal.xlsb is not hidden; it shows up every time I open Excel.
What am I doing wrong or missing here?
Best regards
Holger Nielsen
In Excel, using Functions/Subs of Personal.xlsb is a little different...
In order to use a UDF function, let us say, testMultiplication:
Function testMultiplication(x As Long, y As Long) As Long
testMultiplication = x * y
End Function
it can be called from the (other) workbook sheet cell in this way:
=Personal.xlsb!testMultiplication(3, 4)
In order to call Personal.xlsb Subs and Functions, a reference to the hidden workbook VBAProject is necessary...
To do that, follow the next steps, please:
Firstly give to the Personal.xlsb VBAProject an elocvent name (also to avoid name conflict error, keeping it like VBAProject):
a) In VBE - Project Explorer, right click on its VBAProject, choose VBAProject Properties... and change the standard name in, let us say, 'PersVBAProject`.
b) Then click on a module of the workbook you want to add the reference and use Tools -> References. Find 'PersVBAProject, check it and press OK`.
Starting from that moment the subs/functions of Prsonal.xlsb can be directly called from the specific workbook
In order to programmatically add the reference, use the next code, please:
Sub AddPersonalXLSBRef()
'It needs a reference to 'Microsoft Visual Basic for Applications Extensibility ...'
Dim VBe As VBIDE.VBe, vbPr As VBIDE.VBProject
Dim objRef As VBIDE.Reference, boolFound As Boolean
Set VBe = Application.VBe
Set vbPr = ActiveWorkbook.VBProject
'check if reference to Personal.xlsb exists:
'Please, take care to have its VBAProject name modified in `PersVBProject`
'or something else and, in such a case, adapt the following lines accordingly
For Each objRef In vbPr.References
If objRef.Name = "PersVBProject" Then
MsgBox "Reference to Personal.xlsb already exists"
Exit Sub
End If
Next
'add the reference, if not exist:
vbPr.References.AddFromFile Application.StartupPath & "\Personal.xlsb"
MsgBox "Reference to Personal.xlsb added Successfully"
End Sub
It adds the reference to the active workbook project. The code can be adapted to add it to all open workbooks (not having it added, yet)...

Reopen workbook at last active sheet

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!

App_WorkbookOpen not responding when opening XLS workbooks

I'm writing a simple macro to solve a common problem for people who use XLS with multiple monitors. Often, a workbook is saved in the second window and will not appear in the main window when re-opened.
To get around this, I have attempted to use the App_WorkbookOpen function and have encountered several problems.
This code only runs when opening a fresh instance of XLS.
This code opens the workbook, but does not display it in the windows menu.
This code interacts only with the personal.xls template.
I've included several lines of code to test what the macro is interacting with.
The code resides in ThisWorkbook section of my personal template.
The core code works if placed in an individual worbook, but I'd like it to run in all Wb_open situations.
Option Explicit
Public WithEvents App As Application
Private Sub Workbook_Open()
Set App = Application
End Sub
Private Sub App_WorkbookOpen(ByVal Wb As Workbook)
On Error Resume Next 'error handling, remove when final
Application.ScreenUpdating = False
If ActiveWindow.Left > Application.UsableWidth Then
ActiveWindow.Left = 15
ActiveWindow.Top = 10
End If
MsgBox "New Workbook:" & Wb.Name 'what wkbook?, remove when final
If Error Then End
End Sub
Any thoughts?

Put a user form directly on the excel sheet

Is there any way to insert a user form directly on the excel sheet?
We can add the build-in controls as well as active x controls. I don't see why we cannot add user forms within the same workbook.
Thanks
No, I don't think it's possible.
Userforms are merely containers to hold your ActiveX controls. Spreadsheets are also ActiveX control containers, so I'm not sure what the benefit of having a container in a container would be.
You could easily color a group of cells to look like a userform and place ActiveX controls within that range. That would simulate a userform embedded on a spreadsheet. You'd be missing userform level events and probably a few other things. But if you wanted those things, you'd probably just use a userform.
If there's something you want to do that I'm missing, let me know.
Place this in the WorkSheet module
VB:
Private Sub Worksheet_Activate()
UserForm1.Show
End Sub
I realize it's an old post and this solution might not have been available in the past but you can insert an ActiveX control element and have "Microsoft Forms 2.0 Frame" to control the information.
You will also need to create a custom class to handle the button presses since you can't attach macros directly onto them like a normal button.
Here is an example of mine:
buttonEventHandler
Option Explicit
Public Sub Click(Sender As Integer)
End Sub
xButton
Private WithEvents btn As MSForms.commandButton
Private bEventHandler As buttonEventHandler
Private b As Integer
Public Sub createObject(EventHandlerOf As MSForms.commandButton, EventHandler As buttonEventHandler, xB As Integer)
Set btn = EventHandlerOf
Set bEventHandler = EventHandler
b = xB
End Sub
Private Sub btn_Click()
If Not bEventHandler Is Nothing Then bEventHandler.Click (b)
End Sub
in Microsoft Excel Objects: thisWorkbook
Public Sub buttonEventHandler_Click(Sender As Integer)
Select Case Sender
Case 1
'Do Stuff
End Select
End Sub
Private Sub workbook_open()
Sheet1.Frame1.Activate
Set xbtn = New Collection
Dim o As Object
Dim xB As New XButton
xB.createObject Sheet1.Frame1.Controls("excelCommandButton"), ThisWorkbook, 1
xbtn.Add xB
Set xB = Nothing
End Sub
You could place MyForm.Show code on the Workbook.Open event to launch the form whenever you open the file... that I've done. You can even choose where to place the form.
Your wish, if possible (I'm pretty positive it's not), could trigger concurrent VB code: e.g. auto-calculate of cells, and the form code. That's an issue because XL is single VBA thread, hence it's impossible to have two things happening. Either the form is showing and it's code is running, or XL application is running and doing cells/graphs/etc stuff.

Resources