We are creating an Add-In to Microsoft word. In this Add-In there is a modeless form and we set it as the Top Most form too as per the requirement. When we minimize the Microsoft word application, the modeless form is not minimized. If we able to set the MS Word as the owner of this form, this problem will solve. Please tell me a way to do this.
This is the code that I am using currently to load the form.
Dim Test As Long 
With frmSelectStyle
 .aaInitialize SelectStyleDlg:=Me
 .Show vbModeless
End With
Test = SetTopMostWindow(frmSelectStyle.hwnd, True)
I know that we can set an Owner to a form as follows.
frmSelectStyle.Show vbModeless , frmMain 
But in my case, I need to set the MS Word as the owner. Please help me.
The key is to use the SetWindowLong Windows API function. You can wrap that function in your own "SetOwner" function to make it a bit easier to use. SetOwner accepts two Long Windows handles: the first is for your modeless window and the second is for the Word Application main window (the code below is a variation of code originally published at DevX.com). 
I've tested the code below and the modeless window does minimize along with Word if Word is minimized.
Option Explicit
Private hwndOriginal As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SetWindowLong& Lib "user32" Alias "SetWindowLongA" _
    (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long)
Const GWL_HWNDPARENT = (-8)
Function SetOwner(ByVal HwndtoUse, ByVal HwndofOwner) As Long
    SetOwner = SetWindowLong(HwndtoUse, GWL_HWNDPARENT, HwndofOwner)
End Function
Private Sub Form_Load()
   Dim hWndWord As Long
   ' start an instance of Microsoft Word
   Dim WordApp As Word.Application
   Set WordApp = CreateObject("Word.Application")
   ' make sure it's visible
   WordApp.Application.Visible = True
   ' use the FindWindow API to find a window class of "OpusApp" with the specified Word-application caption
   hWndWord = FindWindow("OpusApp", WordApp.Caption)
   hwndOriginal = SetOwner(Me.hWnd, hWndWord)
End Sub
Private Sub Form_Unload(Cancel As Integer)
   SetOwner Me.hWnd, hwndOriginal
End Sub
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With