vba - 将用户窗体引入 Forefront Excel VBA
问题描述
在一段时间不活动后,我在 Excel 中弹出了一个用户表单。唯一的问题是用户窗体在单击 excel 之前不可见,因此当用户看到它时,另一个子运行将在单击工作表后关闭工作表。我不确定它是如何相关的,但我相信我有 3 个屏幕的事实有点干扰。
底线:如何使用户窗体出现在所有其他活动窗口的前面
一些不必要的背景:
工作表位于工作共享文件夹中。弹出不活动用户窗体和另一个关闭 excel 的例程的原因是因为一次只有一个人可以进行和保存更改。如果用户已不活动 5 分钟,此例程将启动用户,并且当前创建具有标题自动保存和日期/时间的另存为版本。
解决方案
我有(再次)这个问题。在较早的时间(相同的 excel 版本),我只需要将焦点设置为表单的控件,然后再次不可见和可见,但是(为什么 O 为什么)这不适用于我的新表单。
所以我搜索了一下,发现了这个。
在 Userform_Initialize 中:
With ThisWorkbook
'.Windows(1).WindowState = xlMinimized 'workbook minimize, not needed
'.VBProject.VBE.MainWindow.WindowState = vbext_ws_Minimize 'VBE minimize , not needed
'SetFormParent Me, FORM_PARENT_NONE 'makes the userform independantfrom workbooks
TopMostForm Me, True 'forces userform to show on top at all times
'DoEvents
TopMostForm Me, False 'Userform uses normal Zorder again
End With
大多数您不需要的代码,但我想向您展示它独立工作,即存在 Excel 窗口或 VBE 窗口。
您可以使用 Google 找到 TopMostForm 和 SetFormParent 的过程,但这里是(64 位)。
Sub TopMostForm(F As MSForms.UserForm, Top As Boolean)
' Makes a form the top window if top = True. When top = False it removes this property.
Dim hwnd As LongPtr
hwnd = HWndOfUserForm(F)
If Top Then
SetWindowPos hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE + SWP_NOSIZE
Else
SetWindowPos hwnd, 0, 0, 0, 0, 0, SWP_NOMOVE + SWP_NOSIZE
End If
End Sub
Function SetFormParent(Uf As MSForms.UserForm, _
Parent As FORM_PARENT_WINDOW_TYPE, Optional w As Window) As Boolean ' mettre ,2
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' SetFormParent
' Set the UserForm UF as a child of (1) the Application, (2) the
' Excel ActiveWindow, or (3) no parent. Returns TRUE if successful
' or FALSE if unsuccessful.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim UFHWnd As LongPtr
Dim WindHWnd As LongPtr
Dim R As LongPtr
If w Is Nothing Then Set w = Application.ActiveWindow
UFHWnd = HWndOfUserForm(Uf)
If UFHWnd = 0 Then
SetFormParent = False
Exit Function
End If
Select Case Parent
Case FORM_PARENT_APPLICATION
R = SetParent(UFHWnd, Application.hwnd)
Case FORM_PARENT_NONE
R = SetParent(UFHWnd, 0&)
Case FORM_PARENT_WINDOW
If w Is Nothing Then
SetFormParent = False
Exit Function
End If
WindHWnd = WindowHWnd(w)
If WindHWnd = 0 Then
SetFormParent = False
Exit Function
End If
R = SetParent(UFHWnd, WindHWnd)
Case Else
SetFormParent = False
Exit Function
End Select
SetFormParent = (R <> 0)
End Function
推荐阅读
- php - 多个使用openlog php
- excel - 如何通过excel vba选择网页中的特定下拉元素
- node.js - Mongodb自定义对象id
- ajax - 在for循环mysql中运行多查询
- android - 使用带有身份验证标头的 web 视图使用 REST API
- vuejs2 - 为什么在 Vue-Router 参数之后我无法访问数据?
- jmeter - 当我尝试使用 JMeter 加载 500 个用户时,由于 IP 阻塞而显示连接超时
- wpf - 按钮图像路径未在 wpf 中聚焦
- html - 使用 CSS 对图像进行 div 分割
- string - 使用 Delphi 在文本文件中查找特定字符串的行号