在任务栏上显示隐藏应用程序的 Word 用户窗体
Show a Word UserForm on the Taskbar with the application hidden
我正在使用 MS Word 文档为员工使用用户表单来完成评估。本文档将由众多用户 (200+) 在不同时间完成。
我的表单和文档运行良好,但是我正在尝试隐藏文档但保持显示用户窗体以减少混乱并避免在用户窗体处于焦点时文档屏蔽其他应用程序。
当工作人员打开 Word 文档时,用户窗体会自动打开:
Private Sub Document_Open()
Dim myForm As frmAssessment1
Set myForm = frmAssessment1
myForm.Show (0)
End Sub
用户窗体打开时:
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
除了在任务栏上显示不存在 Word(或用户窗体)外,我认为这是一个问题,因为每个用户都会打开其他几个应用程序,而且很可能有些会 'lose' 用户窗体。
我尝试过使用 ActiveDocument.WindowState = wdWindowStateMinimize
,但由于未隐藏单词,这会导致文档在用户窗体处于活动状态时成为焦点,这可能会掩盖正在使用的其他应用程序并导致糟糕的用户体验。
除了找到 WindowState
方法外,我还没有发现任何其他方法来实现我所追求的目标,也没有找到为用户窗体创建任务栏的方法 button/icon (尽管那里有很多关于 Excel 的信息)。
我说的对吗:没有办法 只有 用户窗体显示并且在任务栏上仍然有 button/icon - OR- 无法在任务栏上为用户窗体创建图标吗?
如果我不正确,我该如何实现?
作为解决方法,我顿悟了!
我使用 Application.Resize
而不是 Applicaiton.Visible = False
。
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
这是调整后的样子:
在'Submit button'子中,Unload Me
用于终止用户窗体。
并在用户窗体关闭时恢复 window 大小(我选择最大化):
Private Sub UserForm_Terminate()
Application.WindowState = wdWindowStateMaximize
End Sub
因为这并没有具体回答我提出的问题,所以我没有将其标记为已接受的答案,因为我假设迟早会有人能够直接回答我的问题问.
经过一些WindowsAPI函数的自学后,我仍然基本上一无所知,但我已经足够自信去测试excel 解决方案,结果 (至少我测试过的那个) 有效!
我不喜欢盲目接受别人的代码而不至少理解 syntax/logic。
我专门测试了 Gareth's Answer 中提供的代码,该代码在 Word 2007 和 Excel 2007 中都可以正常工作,只需进行很小的改动。我假设这在我工作时使用的 Office 2010 应用程序中同样有效。
我调整了代码的以下部分:
Private Sub UserForm_Activate()
Application.Visible = False
'Application.VBE.MainWindow.Visible = False
AppTasklist Me
End Sub
特别是我注释掉了 Application.VBE.MainWindow.Visible = False
因为代码中包含这一行,它不会编译并出现以下运行时错误:
Excel
Run-time error '1004':
Programmatic access to Visual Basic Project is not trusted
字
Run-time error '6068':
Programmatic access to Visual Basic Project is not trusted
Chip Pearson 在 Extending The Capabilities Of VBA UserForms With Windows API Functions 上发布了一篇很棒的文章,其中涵盖了很多额外的调整,包括显示最小化和最大化按钮,这些按钮包含在上面引用的答案中,这将对用户窗体进行非常有用的调整。
我正在使用 MS Word 文档为员工使用用户表单来完成评估。本文档将由众多用户 (200+) 在不同时间完成。
我的表单和文档运行良好,但是我正在尝试隐藏文档但保持显示用户窗体以减少混乱并避免在用户窗体处于焦点时文档屏蔽其他应用程序。
当工作人员打开 Word 文档时,用户窗体会自动打开:
Private Sub Document_Open()
Dim myForm As frmAssessment1
Set myForm = frmAssessment1
myForm.Show (0)
End Sub
用户窗体打开时:
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
除了在任务栏上显示不存在 Word(或用户窗体)外,我认为这是一个问题,因为每个用户都会打开其他几个应用程序,而且很可能有些会 'lose' 用户窗体。
我尝试过使用 ActiveDocument.WindowState = wdWindowStateMinimize
,但由于未隐藏单词,这会导致文档在用户窗体处于活动状态时成为焦点,这可能会掩盖正在使用的其他应用程序并导致糟糕的用户体验。
除了找到 WindowState
方法外,我还没有发现任何其他方法来实现我所追求的目标,也没有找到为用户窗体创建任务栏的方法 button/icon (尽管那里有很多关于 Excel 的信息)。
我说的对吗:没有办法 只有 用户窗体显示并且在任务栏上仍然有 button/icon - OR- 无法在任务栏上为用户窗体创建图标吗?
如果我不正确,我该如何实现?
作为解决方法,我顿悟了!
我使用 Application.Resize
而不是 Applicaiton.Visible = False
。
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
这是调整后的样子:
在'Submit button'子中,Unload Me
用于终止用户窗体。
并在用户窗体关闭时恢复 window 大小(我选择最大化):
Private Sub UserForm_Terminate()
Application.WindowState = wdWindowStateMaximize
End Sub
因为这并没有具体回答我提出的问题,所以我没有将其标记为已接受的答案,因为我假设迟早会有人能够直接回答我的问题问.
经过一些WindowsAPI函数的自学后,我仍然基本上一无所知,但我已经足够自信去测试excel 解决方案,结果 (至少我测试过的那个) 有效!
我不喜欢盲目接受别人的代码而不至少理解 syntax/logic。
我专门测试了 Gareth's Answer 中提供的代码,该代码在 Word 2007 和 Excel 2007 中都可以正常工作,只需进行很小的改动。我假设这在我工作时使用的 Office 2010 应用程序中同样有效。
我调整了代码的以下部分:
Private Sub UserForm_Activate()
Application.Visible = False
'Application.VBE.MainWindow.Visible = False
AppTasklist Me
End Sub
特别是我注释掉了 Application.VBE.MainWindow.Visible = False
因为代码中包含这一行,它不会编译并出现以下运行时错误:
Excel
Run-time error '1004':
Programmatic access to Visual Basic Project is not trusted
字
Run-time error '6068':
Programmatic access to Visual Basic Project is not trusted
Chip Pearson 在 Extending The Capabilities Of VBA UserForms With Windows API Functions 上发布了一篇很棒的文章,其中涵盖了很多额外的调整,包括显示最小化和最大化按钮,这些按钮包含在上面引用的答案中,这将对用户窗体进行非常有用的调整。