你好,我有以下代码:当我运行它时,一切正常。 UserForm1已打开(word文件正在后台运行但不可见) 当我关闭 UserForm1 时,一切都会关闭。
问题是我的任务栏底部没有图标,当 UserForm1 在后台时我可以单击该图标。我该如何解决这个问题?希望有一些代码
我的 UserForm1 代码:
Private Sub CommandButton1_Click()
UserForm1.Hide
Application.Visible = True
End Sub
Private Sub UserForm_Terminate()
Dim appWord As Object
Set appWord = Application
ThisDocument.Save
ThisDocument.Close
End Sub
我的文档模块代码:
Sub AutoOpen()
Application.Visible = False
UserForm1.Hide
UserForm1.Show vbModeless
End Sub
VBA 没有为此提供任何开箱即用的东西。您需要使用 Windows API 函数来完成工作。例如,以下是可用于更改窗口样式的示例代码(请参阅
AppTasklist
子项):
Option Explicit
'API functions
Private Declare Function GetWindowLong Lib "user32" _
Alias "GetWindowLongA" _
(ByVal hwnd As Long, _
ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" _
Alias "SetWindowLongA" _
(ByVal hwnd As Long, _
ByVal nIndex As Long, _
ByVal dwNewLong As Long) 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
Private Declare Function FindWindow Lib "user32" _
Alias "FindWindowA" _
(ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Private Declare Function GetActiveWindow Lib "user32.dll" _
() As Long
Private Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long
Private Declare Function DrawMenuBar Lib "user32" _
(ByVal hwnd As Long) As Long
'Constants
Private Const SWP_NOMOVE = &H2
Private Const SWP_NOSIZE = &H1
Private Const GWL_EXSTYLE = (-20)
Private Const HWND_TOP = 0
Private Const SWP_NOACTIVATE = &H10
Private Const SWP_HIDEWINDOW = &H80
Private Const SWP_SHOWWINDOW = &H40
Private Const WS_EX_APPWINDOW = &H40000
Private Const GWL_STYLE = (-16)
Private Const WS_MINIMIZEBOX = &H20000
Private Const SWP_FRAMECHANGED = &H20
Private Const WM_SETICON = &H80
Private Const ICON_SMALL = 0&
Private Const ICON_BIG = 1&
Private Sub AppTasklist(myForm)
'Add this userform into the Task bar
Dim WStyle As Long
Dim Result As Long
Dim hwnd As Long
hwnd = FindWindow(vbNullString, myForm.Caption)
WStyle = GetWindowLong(hwnd, GWL_EXSTYLE)
WStyle = WStyle Or WS_EX_APPWINDOW
Result = SetWindowPos(hwnd, HWND_TOP, 0, 0, 0, 0, _
SWP_NOMOVE Or _
SWP_NOSIZE Or _
SWP_NOACTIVATE Or _
SWP_HIDEWINDOW)
Result = SetWindowLong(hwnd, GWL_EXSTYLE, WStyle)
Result = SetWindowPos(hwnd, HWND_TOP, 0, 0, 0, 0, _
SWP_NOMOVE Or _
SWP_NOSIZE Or _
SWP_NOACTIVATE Or _
SWP_SHOWWINDOW)
End Sub
Private Sub UserForm_Activate()
Application.Visible = False
Application.VBE.MainWindow.Visible = False
AppTaskList Me
End Sub
Private Sub UserForm_Terminate()
Application.Visible = True
End Sub
请参阅 Excel 使用表单:如何隐藏应用程序但在任务栏中有图标了解更多信息。
您可能会发现示例代码很有帮助 - 在任务栏中显示用户表单,并带有自定义图标和隐藏 Excel(模仿独立应用程序) .