I have and Excel-based application that only occasionally needs user access to the underlying workbook. A macro, triggered by an OnTime event (triggers every 15 minutes), shows a 1st userform using the command Userform1.show vbModeless. Within that user form, I use the userform_activate event to set it to be always on top (SetWindowPost(Hwnd1, -1, 0, 0, 0, 0, 3)), which works exactly as intended. Subsequently I can minimize and restore the underlying workbook to my heart's content without affecting the visible 1st userform. However, on that 1st userform, I have a command button that loads a 2nd userform - also modeless (Userform2.show vbModeless). This too has a userform_activate that uses the SetWindowPos(Hwnd2, -1, 0, 0, 0, 0, 3). Initially, this seems to work correctly and both userforms are visible and always on top, with the 2nd being topmost, above the 1st. However what I have discovered is that (assuming the underlying workbook is minimized to start) restoring the underlying workbook initially has no effect on either of the 2 userforms, but re-minimizing the workbook also takes the 2nd userform with it (i.e. minimized). I can only restore the 2nd userform to the desktop by restoring the workbook. I will admit that the userform_activate code for both the 1st and 2nd userforms also repositions each form over its parent application window using the SetWindowPlacement API: The 1st is positioned over the current foreground application window, and the 2nd user form is place over the 1st. This code uses a sequence of APIs to get the location of the "parent" window, its rectangular position and then to calculate how to place the 2nd window over the center of the current (for the 1st window) and the 1st for the 2nd window.
I have tried adding code to the workbook Workbook_WindowResize to bring the 2nd userform back to the foreground by using the command "AppActivate Userform2.Caption"). However, this command errors unless the workbook is restored (xlNormal or xlMaximised),
This behavior seems counterintuitive with respect to the expected behavior of modeless userforms. Certainly Userform1 is behaving exactly as I would expect remaining visible and on top regardless of the state of the underlying workbook window. It's the 2nd userform, launched from a command button of the 1st and also showing as modeless only staying as truly modeless until the underlying workbook window is maximized and subsequently following the state of the workbook window. Modeless userforms show be independent of the workbook containing the macros that launch them. Remember that the 1st userform continues to remain visible and always on top. It's juts the 2nd that has the problem.
I hope someone know why this is happening and can offer a solution.
Many Thanks,
I have tried adding code to the workbook Workbook_WindowResize to bring the 2nd userform back to the foreground by using the command "AppActivate Userform2.Caption"). However, this command errors unless the workbook is restored (xlNormal or xlMaximised),
This behavior seems counterintuitive with respect to the expected behavior of modeless userforms. Certainly Userform1 is behaving exactly as I would expect remaining visible and on top regardless of the state of the underlying workbook window. It's the 2nd userform, launched from a command button of the 1st and also showing as modeless only staying as truly modeless until the underlying workbook window is maximized and subsequently following the state of the workbook window. Modeless userforms show be independent of the workbook containing the macros that launch them. Remember that the 1st userform continues to remain visible and always on top. It's juts the 2nd that has the problem.
I hope someone know why this is happening and can offer a solution.
Many Thanks,