Keeping windows form on top within Excel application but not TopMost RRS feed

  • Question

  • I'm trying to convert an Excel VBA workbook to a VSTO Add-in in VB.NET.  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 want it.

    The problem is, that in the new VSTO add-in, I can not get the Windows form to mimick 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 aplication.

    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)?



    Rob Shurman

    Wednesday, February 22, 2012 8:06 PM

All replies

  • Hi Rob,

    Thanks for posting in the MSDN forum.

    Would you please clarify your Excel version for further research?

    Have a good day,


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Friday, February 24, 2012 7:17 AM
  • Sure.  I'm using Excel 2010 (although the VBA version of the workbook also worked in a similar manner when I tested in Excel 2003).



    Rob Shurman

    Friday, February 24, 2012 1:33 PM
  • Hi Rob,

    It sounds to me like you should look at Custom Task Panes. They stay attached to the open workbook.

    Hope this helps

    Kind Regards, Rich ... http://greatcirclelearning.com

    Friday, February 24, 2012 2:04 PM
  • Thanks Rich, but I need more functionality, customization, event handling, etc. than what Custom Task Panes would provide.

    Rob Shurman

    Friday, February 24, 2012 2:20 PM
  • Okay, in the form have you tried the Me.Refresh command?  It might help.

    Kind Regards, Rich ... http://greatcirclelearning.com

    Friday, February 24, 2012 2:28 PM
  • I just tried Me.Refresh as you suggested, but no luck.  I put it into the LostFocus event handler...

    Private Sub frmMain_LostFocus(sender As Object, e As System.EventArgs) Handles Me.LostFocus
    End Sub

    I know that the form did refresh since the Paint event handler fired, however even though the form did refresh it still stayed in the background (i.e.- behind the worksheet).



    Rob Shurman

    Friday, February 24, 2012 7:14 PM
  • I am having the same problem, clicking outside Excel makes my Custom Task Pane user control to be hidden, clicking on Excel then add-in is shown infront of Excel.  Rob, since its been a couple of months did you find a fix, otherwise maybe post this question on Stackoverflow.

    Jeremy - MCP | MCAD.Net | MCSD.Net

    Wednesday, May 16, 2012 5:24 AM
  • you have to call form.Show overload that accepts parent - just pass there Application.Hwnd, wrapped inside NativeWindow.FromHandle
    Wednesday, May 16, 2012 8:37 AM
  • Just beware of passing Excel's HWND into your own form...

    I am currently passing Application.Hwnd (of Excel) into the Show method of my form eg ourForm.Show(_excel.Hwnd);

    But when I shut down Excel, it crashes (Microsoft Excel has stopped working...) - not from our code either, of it's own internals. 

    If we call Show() without Excel.Hwnd as the arg, Excel does not crash on Close.

    • Edited by PandaWood Wednesday, February 25, 2015 4:21 AM
    Wednesday, February 25, 2015 4:16 AM
  • Rob Shurman,

    did u found any issue for the issue? I am also facing the same, if you can guide me in some direction.

    Thanks & Regards,


    Tuesday, September 29, 2020 7:11 AM