VBA : Detect a change of Activeworkbook - vba

I have a XLA AddIn macro with a main menu Userform. I need to enable/disable boutons on this menu userform according to the Activeworkbook.
My problem is that I don't know how to update the Userform when the user change or close the Activeworkbook.
I do have a UserForm_Activate, which update the Userform. But that event is not triggered when the user close the Activeworkbook then click on the Userform.
I would need to update the Userform either :
as soon as the Activeworkbook is changed or closed
or as soon as the user reach the Userform (ie. before he can click on any control).
How would you proceed ?

In the ThisWorkbook module of your xla:
Private WithEvents xlApp as Excel.Application
You will then see that xlApp has events such as WorkbookActivate and WorkbookDeactivate etc.

Related

Creating a button with macro on a different workbook

I currently have a macro which creates another workbook and adds a button to it. I've assigned a macro to the button, but I believe I still need the master workbook to actually run the macro.
Is there a way to have the macro run on the new workbook without referring to the master workbook? In other words, is it possible to programmatically add a button to a new workbook with a macro and have it be independent of the original workbook?
Thanks in advance!
I'd go like this:
have your "master" workbook with a template worksheet whit both an ActiveX button and its corresponding code in its code pane
say its name is "TemplateSheetWithButtonAndCode"
in your macro use
Worksheets("TemplateSheetWithButtonAndCode").Copy
ActiveWorkbook.SaveAs "c:\MyFolder\MyWorkbooK"
this will generate a new workbook with a copy of your "template" worksheet as its only worksheet, along with necessary code to have the button work
Place the button on a template worksheet. Place the button's associated macro in that worksheet's code area.
If you export the worksheet to another workbook, you will also be exporting the button and the macro.

How to goto excel vba code when opening the macro opens Userform instead of Thisworkbook?

I have written excel VBA code containing Userform and added code to show userform and hide Thisworkbook when the macro is opened. It shows userform during Start up. How to see the vba code, when userform is opened? Clicking Alt+F11 on Userform is not taking to VBA editor.
Press the Shift key and hold it down while you open the file. This will prevent the macro from firing (I'm assuming you have Workbook_open setting both Application.Visible = False and your userform's .Show method?)

ActiveX spreadsheet control disables workbook_open event

I have inserted an ActiveX spreadsheet control into an Excel VBA form.
I have written a procedure in the ThisWorkbook.Workbook_Open() event and it will not execute upon opening the workbook when the ActiveX spreadsheet control exists on the form.
In effort to simplify the problem, I have:
Created a new workbook.
Added a blank form "UserForm1".
Added an ActiveX spreadsheet control to "UserForm1", "Spreadsheet1".
In ThisWorkbook, added the Private Workbook_Open() event:
The Workbook_Open event does not execute when the workbook is opened. When the spreadsheet control is deleted from the form, the workbook_open event executes normally.
Macros are enabled in both cases. Other macros execute successfully when manually called.
How do I get the workbook_open event to execute with an ActiveX control on a VBA form?
Private Sub Workbook_Open()
MsgBox "Workbook_Open event has executed."
End Sub
UPDATE: When security is set to "Disable all macros with notification" and file is opened for the first time in protected view, the Workbook_Open() event is fired. On subsequent opening of the file, the event is not fired.

How to get the worksheet that a regular (non-ActiveX) control is on?

I have a sub that I am calling by clicking a button. I've chosen to use a regular button (aka Form Control), not an ActiveX one, because I've been seeing the text size on ActiveX controls fluctuating and regular buttons don't have this problem. I want the sub to be able to use the worksheet that the button is on. I was thinking of something like this:
Sub showSheetName()
Dim sht As Worksheet
'this would work if the code was in the worksheet's module
Set sht = Me
msgbox sht.Name
End Sub
But this sub is in a shared module, not the worksheet's module, because I want several sheets to be able to use it. So Me doesn't point to the worksheet, and this approach just gives an Invalid use of Me keyword.
How can I get a reference to the worksheet that the button is on?
EDIT: If it wasn't clear, the reason why it's important that it's not an ActiveX control is because that means there's no myButton_Click() event in the worksheet that I can use to get the worksheet object and pass to a version of showSheetName() that takes a worksheet argument.
Set sht = ActiveSheet...
This of course assumes that the procedure is being invoked manually (i.e., the user is actively clicking the button) rather than being invoked through a Call or Application.Run statement.
The button can only be clicked by the user when the worksheet is active view.
There is no such thing as a regular button. Nor anymore are there ActiveX controls.
ActiveX is a marketing terminology by MS that described controls using COM. It, as a terminology, was retired years ago (the technology is still current).
Therefore it is hard to know what you are referring to.
Use names that HELP uses. List code that specifies the objects/etc that you are using.

Make the Userform in Outlook stay on the screen - VBA

So I have created a VBA Macro embedded withing Outlook. The code runs a userform.
When I run my macro:
Currently:
When I minimize Outlook, my userform also minimizes.
What I Want:
When I minimize outlook I want my userform to stay on the screen.
Any ideas on how to achieve this? I am using vbModeless to display my userform, as i want the user to access the outlook content while the userform is running.
Try adding this to your code.
Private Sub UserForm_Initialize()
Dim olapp As Object
Set olapp = GetObject(, "Outlook.Application")
olapp.ActiveWindow.WindowState = 1
End Sub
This will minimize Outlook the moment userform is shown.
Not exactly what you want (what you describe is a bit complicated), but nearly the same effect.