PDA

View Full Version : How To Bring Userform In Front Of Another Application Other Than Parent Application



peccora3
07-06-2013, 02:18 AM
I have created a modeless UserForm in Powerpoint 2013 vba in Windows 8 which displays while the user to perform a certain task in Excel and then press OK.
When the user switches the program to Excel, the form is hidden behind. I want it to be displayed in front of all opened windows regardless of program type.
I tried SetWindowsPos but this does not seem to work on Windows 8.
Is there a way to display the form on top without going through the task of signing up trusted certificate?

Excel Fox
07-06-2013, 12:43 PM
Does the SetWindowsPos give you the desired feature in Win 7 or previous? Don't have a Win 8, so cannot check.

peccora3
07-07-2013, 08:26 AM
Does the SetWindowsPos give you the desired feature in Win 7 or previous? Don't have a Win 8, so cannot check.

Thanks to your remark, I tested further and found the cause. It was not the SetWindowPos, but because I wrongly declared the FindWindow API. Now, it's working fine on Powerpoint 2013-Windows 8.
But curiously, when I bring this VBA to Powerpoint 2010-Windows 7, I obtain an error message saying the program could not read dll. I'll have to find a way so that it works on both PPT 2010 and 2013 on Windows 7 and 8.

Thank you again.

Excel Fox
07-07-2013, 02:26 PM
Can you post a trimmed down version of the powerpoint file with the form and vba which works in Win 8. If as you say it's working fine on Win 8, will try to integrate some code to make it compatible for both Win 8 and Win 7.

peccora3
07-10-2013, 04:35 AM
Thank you for your helpful suggestions. I have since tested with other PCs and found that this problem occurs only on one particular PC. So it must be some error on the side of the PC, not the code itself.

For reference, here is the script I have written. (This is only the essence. Error handling, etc. are omitted)
Assuming a UserForm1 is created:

Public Declare PtrSafe Function SetWindowPos Lib "User32" (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, _
ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Public Const HWND_TOPMOST = -1
Public Const HWND_NOTOPMOST = -2
Public Const SWP_NOMOVE = &H2
Public Const SWP_NOSIZE = &H1

Public Declare PtrSafe Function FindWindow Lib "User32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Sub ShowFormOnTop()
Dim bwind As Long

Load UserForm1
UserForm1.Show vbModeless

If UserForm1.Visible = True Then
bwind = FindWindow(vbNullString, UserForm1.Caption)
Else
bwind = 0
End If
SetWindowPos bwind, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE
End Sub
----------------------------
Thank you again.