Popup window with FindWindow API

The following code creates a popup window with FindWindow API, which functions independent of the Office application. It can contain state texts, messages and running totals. The FindWindow call creates a handle to the top-level window.

Option Base 1
Option Explicit

Type RECT32
cl As Long
ct As Long
cr As Long
cb As Long
End Type

Declare Function FindWindow32 Lib "USER32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Declare Function FindWindow32L Lib "USER32" Alias "FindWindowA" (ByVal lpClassName As Long, ByVal lpWindowName As String) As Long
Declare Function GetModuleHandle32 Lib "KERNEL32" Alias "GetModuleHandleA" (ByVal lpModuleName As Any) As Long
Declare Function CreateWindowEx32 Lib "USER32" Alias "CreateWindowExA" (ByVal dwExStyle As Long, ByVal lpClassName As String, ByVal lpWindowName As String, ByVal dwStyle As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hWndParent As Long, ByVal hMenu As Any, ByVal hInstance As Long, lpParam As Any) As Long
Declare Function ShowWindow32 Lib "USER32" Alias "ShowWindow" (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long
Declare Sub SetWindowText32 Lib "USER32" Alias "SetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String)
Declare Function DestroyWindow32 Lib "USER32" Alias "DestroyWindow" (ByVal hWnd As Long) As Long

Const GWW_HINSTANCE = (-6)
Const WS_BORDER = &H800000
Const WS_CHILD = &H40000000
Const WS_SYSMENU = &H80000
Const SW_HIDE = 0
Const SS_CENTER = &H1&
Const GWL_HINSTANCE = (-6)

Dim hWnd16 As Integer
Dim hWndTxt16 As Integer
Dim hWnd32 As Long
Dim hWndTxt32 As Long

Dim g_blnWindowVisible As Boolean
Dim g_lngWindowHandle As Long

' Constants
Const c_strWindowCaption As String = "WindowCaption"
Const c_strWindowText As String = "WindowText"
Const c_strErrorMessage As String = "#VALUE#"

Sub WindowShow()
Dim hWndP As Long
Dim hInstP As Long
Dim a As Long

If g_blnWindowVisible = False Then

'Uncomment line for Word, Excel or Access

        ' Excel in Workbook_Open()
        'hWndP = FindWindow32("XLMAIN", Application.Caption)
        ' Access in AutoExec()
        'hWndP = FindWindow32("OMain", Application.Name)
        ' Word in Document_Open
        'hWndP = FindWindow32("OpusApp", Application.Name)

hInstP = GetModuleHandle32(0&)
hWnd32 = CreateWindowEx32(0, "#32770", c_strWindowCaption, WS_EX_TOOLWINDOW + WS_BORDER, 550, 140, 175, 50, hWndP, 0&, hInstP, 0&)
hWndTxt32 = CreateWindowEx32(0, "Static", c_strWindowText, WS_CHILD + SS_CENTER, 0, 0, 150, 30, hWnd32, 0&, hInstP, 0&)
g_lngWindowHandle = hWndTxt32
a = ShowWindow32(hWndTxt32, SW_SHOWNOACTIVATE)
a = ShowWindow32(hWnd32, SW_SHOWNOACTIVATE)
g_blnWindowVisible = True
End If
End Sub

Sub WindowSetText(Optional Text As String = "")

If Text = "" Then Text = c_strWindowText
If hWndTxt32 = 0 Then
hWndTxt32 = Text
End If
SetWindowText32 hWndTxt32, Text

End Sub

Sub WindowKill()
Dim a As Long

a = ShowWindow32(hWnd32, SW_HIDE)
a = DestroyWindow32(hWnd32)

End Sub

Sub WindowKillAll()
Dim hWndP As Long
Dim a As Long

hWndP = FindWindow32L(0, c_strWindowCaption)
a = ShowWindow32(hWndP, SW_HIDE)
a = DestroyWindow32(hWndP)
g_lngWindowHandle = 0

End Sub

Sub WindowClose()

g_blnWindowVisible = False

End Sub