1

Using an MS Word document, I'm utilising a UserForm for staff to complete an assessment. This document will be completed by numerous users (200+) at various times.

I have the form and document working well however I'm trying to manipulate hiding the document but keeping the UserForm shown to reduce clutter and avoid the document masking other applications when the UserForm is in focus.

When the staff member opens the Word Document, the UserForm auto opens:

Private Sub Document_Open()

Dim myForm As frmAssessment1
Set myForm = frmAssessment1

    myForm.Show (0)

End Sub

When the UserForm opens:

Private Sub UserForm_Initialize()

'This defines tab 0 will display.
Me.MultiPage1.Value = 0

'This hides MS Word but remains open in the background.
Application.Visible = False

Dim question1 As String

'Populates the combobox for the Team Number selection_
' with an array (currently 1-30).
    cmbTeamNum.List = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30)

'Assigns the text range of the defined bookmark ("x") to a variable
question1 = ActiveDocument.Bookmarks("question1").Range.Text

'Assigns each variable to the label caption field.
    With Selection
    lblQ1.Caption = question1
    End With

End Sub

Application.Visible = False is working for me apart from showing no presence of Word (or the UserForm) on the Task Bar which I am anticipating to be an issue as each user will have several other Applications open and it's likely some will 'lose' the UserForm.

I've tried using ActiveDocument.WindowState = wdWindowStateMinimize though as word isn't hidden this causes the document to come into focus when the UserForm is active which may mask other Applications being used and cause a poor user experience.

Other than finding the WindowState method, I haven't discovered any other methods to achieve what I'm after nor have I found a way to create a Task Bar button/icon for the UserForm (though there is plenty of info for Excel out there).

Am I correct in saying: There is no way to have only the UserForm show and still have a button/icon on the Task Bar -OR- there is no way to create an icon on the Task Bar for the UserForm?

If I'm not correct, how can I achieve this?

Samuel Everson
  • 2,097
  • 2
  • 9
  • 24
  • 1
    Could you provide a link to one of those Excel solutions, please? Is the UserForm modal, or non-modal? My gut feeling is, it can't be done as you imagine using a VBA UserForm, but could well be done using a Visual Studio .NET Windows Form. That's why the Excel approach interests me - there may be a clue there that can be used with Word... – Cindy Meister Apr 30 '18 at 14:47
  • @CindyMeister here is the first one I stumbled across https://stackoverflow.com/questions/24733377/excel-useform-how-to-hide-application-but-have-icon-in-the-taskbar. A quick google of "vba userform taskbar button" shows quite a few further results. This is well out of my scope of knowledge so I hope it helps you! Unfortunately Visual Studio is not an option. Lastly I did just find this with fresh eyes this morning which I'm looking into now: https://social.msdn.microsoft.com/Forums/en-US/440b4e57-0210-4638-87a0-1ddd954c3baa/userforms-and-the-taskbar?forum=isvvba – Samuel Everson Apr 30 '18 at 22:29
  • @CindyMeister Also, the UserForm is currently modal. FYI: I did change it to non-modal but still had the issue of the Document maximising or moving forward on the display when the UserForm is in focus, so I ended up changing it back to modal for the time being. . – Samuel Everson Apr 30 '18 at 22:36

2 Answers2

0

As a work around I just had an epiphany!

Instead of Applicaiton.Visible = False I'm using Application.Resize.

 Private Sub UserForm_Initialize()

'Without this it would encounter errors here and there.
Application.WindowState = wdWindowStateNormal 

'When the userform is not in focus this allows you to still navigate back to the userform from the MS Word button on the TaskBar but only shows a very small title bar in the top right corner of the screen. 
Application.Resize 10, 10 

'This defines tab 0 will display.
Me.MultiPage1.Value = 0

'Some code does some things...
'
'
End Sub

This is how the resize looks: Resized MS Word Title Bar

In the 'Submit button' sub, Unload Me is used to terminate the UserForm.

And to restore the window size when the UserForm is closed (I chose to maximize):

Private Sub UserForm_Terminate()

Application.WindowState = wdWindowStateMaximize

End Sub

As this doesn't specifically answer my question asked, I'm not marking this as the accepted answer as I'm assuming sooner or later someone will be able to directly answer what I've asked.

Samuel Everson
  • 2,097
  • 2
  • 9
  • 24
  • I wasn't having any issues with the above in MS Word 2007 at home however at work using MS Word 2010 I kept getting Run-time error: 5868 "Window is maximised." when opening the document after it had been opened and closed at least once before. To work around this I used `Application.Resize 600, 600` in `Private Sub_Terminate` instead of `Application.WindowState = wdWindowStateMaximize`. – Samuel Everson May 01 '18 at 23:27
0

After some self education on Windows API functions I still know basically nothing, but I understand enough to be confident to test the excel solutions and it turns out that (at least the one I tested) it works!

I'm not the biggest fan of blindly accepting someone elses code without at least understanding the syntax/logic.

I specifically tested the code provided in Gareth's Answer which worked in both Word 2007 and Excel 2007 with minimal changes. I'm assuming this will work all the same in Office 2010 Applications which is what I'm using at work.

I adjusted the following section of the code:

Private Sub UserForm_Activate()

Application.Visible = False
'Application.VBE.MainWindow.Visible = False
AppTasklist Me

End Sub

Specifically I commented out Application.VBE.MainWindow.Visible = False as with this line included in the code, it would not compile with the following rune-time error present:

Excel

Run-time error '1004':

Programmatic access to Visual Basic Project is not trusted

Word

Run-time error '6068':

Programmatic access to Visual Basic Project is not trusted


Chip Pearson posted a fantastic peice on Extending The Capabilities Of VBA UserForms With Windows API Functions which covers quite a few additional adjustments including showing the minimize and maximize buttons which included with the above referenced answer would make quite a useful adjustment to the UserForm.

Samuel Everson
  • 2,097
  • 2
  • 9
  • 24