CASE:
I have created an Access database.
Using Access-VBA I open an Excel workbook to do stuff.
This workbook (xlWb) does complex calculations and also loads an Excel Userform (UserForm2) on open.
So far everything OK. I open xlWb, UserForm2 loads, I do stuff.
GOAL:
After this, I need to "refresh" Userform2.
By the term "refresh" I mean either call a custom sub of this UserForm, or unload and reload UserForm2.
QUESTION:
How can I can reference UserForm2 from my Access-VBA code?
WHAT I 'VE TRIED:
By searching I 've only found how to reference a UserForm from
another workbook.
The first suggestion was to use VBA.UserForms to get a loaded UserForm.
So I 've tried the following references: VBA.UserForms("UserForm2"), VBA.UserForms.Item("UserForm2"), VBA.UserForms(0), VBA.UserForms(1), all of which threw error: "subscript out of range", which implies that what I 'm writing is not a member of the collection.
Another suggestion was to create a function that loads and unloads the object.
So I wrote inside an xlWb's module named Apps this:
Public Sub Refresh_UserForm()
Unload Userform2
Userform2.Show
End Sub
and in the access sub this:
Application.Run "'" & xlWb.Name & "'!Apps.Refresh_UserForm"
This throws a
Run-time error 2517 cannot find the procedure
'calc_8.4.xls'!Apps.Refresh_UserForm'
The same error is also generated when I tried a 3rd similar suggestion to create a function that returns an instance of the object.
Every suggestion is very welcome thank you.
So this works for me:
Note: for demonstration purposes I created a xlsm file called "Map1.xlsm" with a userform called "UserForm1". Also, you'll need the "Microsoft Excel 16.0 Object Library" reference turned on.
First, as mentioned above I created an Excel file with the below macro in Module1:
1). To open the UserForm for the first time (and refresh):
Sub Refresh_UserForm()
Unload UserForm1
UserForm1.Show False
End Sub
Within Access-VBA I created the following two macros (based on this):
1). To open and use the Excel UserForm
Option Compare Database
Dim xlApp As Excel.Application, xlWB As Excel.Workbook
Sub mySub()
Set xlApp = CreateObject("Excel.Application")
Set xlWB = xlApp.Workbooks.Open("C:\...Path...\Map1.xlsm") 'Specify Path
xlApp.Visible = True
xlApp.Run "Map1.xlsm!Refresh_UserForm"
End Sub
2). To Refresh the Excel UserForm
Sub Refrsh()
Set xlApp = GetObject(, "Excel.Application")
xlApp.Run "Map1.xlsm!Refresh_UserForm"
End Sub
Hopefully this helps
Please try the following:
In userform2, add a "Refresh" button and the following procedure:
Private Sub CommandButton_Refresh_Click()
Me.Repaint
End Sub
If you have a UserForm_Initialize sub and UserForm_Terminate sub , you can include the procedures to simulate the unload-reload action.
Private Sub CommandButton_Refresh_Click()
Call UserForm_Terminate
Call UserForm_Initialize
Me.Repaint
End Sub
If you need to unload the form and call it back later manually, then you will have to call it back from the actual loaded Excel window (e.g. with a form Button on the worksheet), but cannot be from Access or the xlWb hook.
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 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
I want to make a GUI-like interface for an Excel workbook, and I don't want the workbook to be visible until it closes. However, making the workbook not visible messes with the references of my code and I cannot read the ranges without heavily modifying it. I tried minimizing the window, but that minimizes the form as well.
Is there a way to keep the active workbook active but not readily visible, and the form visible?
Include the following when you load the userform
Private Sub UserForm_Initialize()
ThisWorkbook.Application.Visible = False
End Sub
and this for when you end the userform (return to normal state)
Private Sub UserForm_Terminate()
ThisWorkbook.Application.Visible = True
End Sub
put this code in form's UserForm_Initialize method
ActiveWindow.Visible = False
I suggest that my workbook contains a VBA code below. But It does not run when I opened my workbook too.
Sub Appl_StatusBar()
Application.StatusBar = "SupportABC"
End Sub
If you put your code in the Workbook Open Event it will do what you need. To do this click the top dropdown where it says "(General)" and hit "Workbook". In the right dropdown select "Open" and save your code there". See below
Private Sub Workbook_Open()
Application.StatusBar = "SupportABC"
End Sub
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?