Search code examples
vbams-wordhideuserform

Hide Document When UserForm Initializes without affecting other unrelated documents but also keep a taskbar icon


I want to initialize a form upon running a document.

In a module I entered the following Code:

Public Sub AutoOpen()
    UserFormLaunchWindow.Show
End Sub

Which sends to command to show the UserForm.

The UserForm has the following Code:

Private Sub UserForm_Initialize()
    ActiveDocument.Windows(1).Visible = False
End Sub

Private Sub UserForm_Terminate()
    ThisDocument.Windows(1).Visible = True
If Not Application.Documents.Count > 1 Then
    ThisDocument.Save
    Application.Quit
Else
    ThisDocument.Save
    ActiveDocument.Close True 'Here if I use ThisDocument, all docs open close.
End If
End Sub

Which is supposed to make the Document Invisible and keep just the UserForm Window displayed.

However, during the initialization of the userform if there is another instance of word open for another unrelated document it closes it too, or hides it too, not sure which.

The ultimate goal is to open the initialized Userform, Hide only the Document that initializes the UserForm without affecting any other documents. Keep a taskbar icon to easily show the UserForm. Also when closing the UserForm, it shouldn't affect any other unrelated documents.


Solution

  • I have no idea why your code resulting that situation, the same code I used to test is OK . When the Form opens, just the .docm Document will be hidden, and when it closes also only the .docm document file closes.

    the code in the UserForm whose name is "UserFormLaunchWindow"

    Option Explicit
    
    Rem cocode with chatGPT
    
    Rem If your Office is 64-bit, you must add PtrSafe before the "Function" and after the word "Declare"。
    Rem Unfortunately, the topmost display in the 64-bit Office run seems to be invalid. So chatGPT taught me to do this:
    
        Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
        Private Declare PtrSafe Function GetForegroundWindow Lib "user32" () As LongPtr
        Private Declare PtrSafe Function SetWindowPos Lib "user32" (ByVal hwnd As LongPtr, ByVal hWndInsertAfter As LongPtr, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As LongPtr
    #Else
        Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
        Private Declare Function GetForegroundWindow Lib "user32" () As Long
        Private Declare 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
    #End If
    
    Private Const SWP_NOMOVE = &H2
    Private Const SWP_NOSIZE = &H1
    
    Private Sub UserForm_Initialize()
        ThisDocument.Windows(1).Visible = False
    End Sub
    
    Private Sub UserForm_Terminate()
        topMostTerminate
        UserFormTerminate
    End Sub
    
    Private Sub UserForm_Activate()
        topMost
    End Sub
    
    Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
        topMostTerminate
    End Sub
    
    Private Sub topMost()
        ' Set UserForm to always be displayed at the top ( topmost)
        'SetWindowPos Me.hwnd, -1, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE
        SetWindowPos ThisDocument.Windows(1).hwnd, -1, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE
    End Sub
    
    Private Sub topMostTerminate()
        ' When closing UserForm, return the focus to the main window of the Word application
        #If VBA7 Then
            Dim hwndWord As LongPtr
        #Else
            Dim hwndWord As Long
        #End If
        hwndWord = FindWindow("OpusApp", Application.Caption)
        SetWindowPos hwndWord, -2, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE
    End Sub
    
    Private Sub UserFormTerminate()
        Rem The document will eventually be closed, there is no need to show it again.
        'ThisDocument.Windows(1).Visible = True
        If Not Application.Documents.Count > 1 Then
            ThisDocument.Save
            Word.Application.Quit
        Else
            If Not ThisDocument.Saved Then ThisDocument.Save
            ThisDocument.Close
        End If
    End Sub
    
    
    

    the code in ThisDocument object in the .docm file where saves both the code and the form

    Private Sub Document_Open()
    
        UserFormLaunchWindow.Show False
    
    End Sub
    
    

    remark:

    If you want to open this userform code document file without executing the program, you can hold down "Shift" key and open the file. For example, when you want to edit or check the code.

    sample file, download and check it out