Close form on "lose focus" in VBA? - vba

I have a VBA application that shows a form with controls on it.
I'm trying to find a way to close the form if the user clicks anywhere outside of it.
My VBA application is not in Excel, so I can't use Worksheet.SelectionChange Event

I found a brilliant solution to this dilemma here:
https://sites.google.com/site/msaccesscode/forms-1/howtoclosepopupnonmodalformwhenitlosesfocus
This method uses the pop-up form's Timer event to keep checking whether the pop-up form is the active form. If so, the form stays open. If not, it closes.

Related

Opened form won't reactivate after closing modal

Within VB.NET Visual Studio 2013
I have an mdi application with several forms. Only one form is open at a given time, then while the user enters amounts by typing in the control, the form is recalculating itself constantly via code.
I also have buttons above (on the mdiform) to open modal dialogs to enter certain information; the information entered is somewhat long, but at the end what I need from the dialog is the result of certain calculation.
That number (the result) should be shown on the opened form after the user close the modal dialog; but it is not happening unless I press the enter key so the form is again recalculated.
I am looking for a way that the form recalculate when the user close the dialog.
I tried everything at my disposal, "Activate", "a public sub", "raise an event", and many others, but the form is not recalculating, what happens is that the cursor is there on the last control the user works in.
The only way to see those changes is reloading the form, but it is not elegant.
Any help will be highly appreciated.
PS. I have looked everywhere about how to do it, but seems I am looking on a haystack
Please need help badly.
ariel

Is There a Way To Simulate the Form Activate from Modal/Pop Up Form

I have a form that opens a modal pop up helper form to collect some data. When this modal pop up form closes, focus returns to the main form. I would like to trigger some activity when focus comes back to my main form.
The event model for the form does not trigger the Form_Activate or Form_GotFocus events when returning from a model popup as per Microsoft documentation.
These forms and all controls are completely unbound.
Is there a trick to knowing in code when focus returns to my form?
If both forms are placed in normal mode, Form_Activate does fire in the primary form when the helper form closes, but this does not meet my needs. I have not been able to find a similar event to trap this. I'm hoping someone has a workaround.
For clarity, the issue I ran into was I had the design time properties in the helper form set to act as a dialog - (pop up and modal as true). When I opened the form responding to the button click event on the main form, I did NOT include the acDialog option.
If you omit the WindowMode=acDialog setting the code operates asynchronously completing the code in the click event handler in full, NOT halting and waiting.
The acDialog option apparently forces the DoCmd.OpenForm command to run synchronously, halting code execution, then returning once the modal form has been closed.
Private Sub btnHelper_Click()
DoCmd.OpenForm "frmHelper", WindowMode:=acDialog
'Do stuff after frmHelper closes
End Sub

Excel AddIn - Keeping windows form always visible while w/in Excel

First of all, thank you for your time and assistance in reviewing this!...
I'm trying to upgrade an Excel VBA workbook to a VSTO Excel Add-in in VB.NET using VS 2010. In the original (i.e.- VBA) version I have a modeless UserForm (called frmMain) that floats on top and is visible at all times while the user is still within the Excel application, but is not visible if the user moves to another window outside of Excel.
For example, within Excel the user can click on any worksheet tab, select any cell, etc. and the UserForm is still visible. This is exactly how I'd like it.
The problem is, that in the new VSTO add-in, I can not get the Windows form to mimic this same behavior.
I use frmMain.Show() to show the form as a modeless form, but the moment the user clicks an Excel worksheet (i.e.- activates a worksheet) the form becomes hidden behind the worksheet.
I can manually Alt-Tab to bring the form back into view, but I need it to always remain in view - floating on top of the Excel worksheets so long as the user hasn't left the Excel application.
I tried various things, including setting the form to TopMost, however, that causes the form to be TopMost everywhere - including outside of Excel. Worse than that, if the user does anything that would normally result in Excel's launching a dialog box (e.g.- closing an open workbook, raising the alert "Do you want to save the changes...") the alert dialog box itself is hidden and inaccessible behind the frmMain form (since the frmMain is TopMost).
How can I get my form to behave in the desired way (i.e.- the same way it did in VBA)?
Thanks!!!
Rob
You should take a look at Custom Task Panes which can be docked or floated within the Excel application. You could also look into the COM interfaces for a lower level connection (see related SO Post) - although Task Panes are really what this type of behavior was intended for.
This method might work (worked for me):
Create a worksheet_selectionChange event handler inside your form class
Put these lines in this event handler:
Dim FormHandle As IntPtr = Me.Handle
Me.Visible = False
Me.Show(NativeWindow.FromHandle(FormHandle))

In Excel, is there a way to determine the method being called by a password protected 3rd-party ribbon button?

I'm looking to automate a simple process in VBA and need to "click" a ribbon button. It makes the most sense to just call the button's underlying method. Is there a way to figure out what it is?
We are using Excel 2007 and 2010.
If it's Excel 2003 or earlier, you can use the CommandbarControl.Execute method. For instance:
Application.Commandbars("3rd Party Toolbar").Controls("Button to Push").Execute
There are two ways you can do this.
Right click on the toolbar and select Customise. If you right click on the button there may be details somewhere of what Macro the button is associated with.
If you record a macro and click the button. Then you can look at the VBA workspace and view the code that is generated to run your macro (which is just clicking the button you want). This will show you what method the macro clicks.

Total focus on a Form: ignore all others

in my VB.NET application, my main Form can show other Forms. That works fine, but I need to set "focus" on the newly opened form, and don't allow the user use the main form until the opened Form is closed. How do I do that?
The Form.ShowDialog() method shows a form as a dialog box; this should keep the focus on that form until it is closed (by the user or your code).
Use the Form.ShowDialog() method.
It will show you the form as a dialog box, so focusing is at total, and it will ignore all other forms or objects.