我有一个字符串Application.hWndAccessApp
,它出现在多行代码中以调用自定义MsgBoxT
。与其在模块和类的每个实例上都不写Application.hWndAccessApp
,我希望将其分配给一个公共变量,例如hid as string
。我知道有关声明公共变量,然后在函数或SubRoutine中为其分配值的知识。
在这里,我希望全局分配该值,以便每次我想使用MsgBoxT
函数时,该值在所有模块/类中都可用。
编辑:我遵循以下注释的提示,但它给出了Error # 13: Type Mismatch
。
Global Const hid = "Application.hWndAccessApp"
然后我使用下面的函数调用它:
MsgBoxT hid, "Record Updated!", "Confirmation", VbInformation, 0 , 1000
我的MsgBoxT函数声明为公开的:
Public Declare PtrSafe Function MsgBoxT _
Lib "user32" _
Alias "MessageBoxTimeoutA" ( _
ByVal hwnd As LongPtr, _
ByVal lpText As String, _
ByVal lpCaption As String, _
ByVal wType As VbMsgBoxStyle, _
ByVal wLange As Long, _
ByVal dwTimeout As Long) _
As Long
答案 0 :(得分:2)
基于更新后的问题,变量为LongPtr
。由于它是在Runtime
上初始化的,因此您有一个选择:
声明您即将分配的 Global LongPtr :
Global hid As LongPtr
hid = Application.hWnd
有了下面问题中更新的代码,它现在可以顺利运行:
Global hid As LongPtr
Public Declare PtrSafe Function MsgBoxT _
Lib "user32" _
Alias "MessageBoxTimeoutA" ( _
ByVal hwnd As LongPtr, _
ByVal lpText As String, _
ByVal lpCaption As String, _
ByVal wType As VbMsgBoxStyle, _
ByVal wLange As Long, _
ByVal dwTimeout As Long) _
As Long
通过这些更改,它可以工作(在我的PC上经过测试):
'On This_Workbook
Private Sub Workbook_Open()
hid = Application.hwnd
End Sub
'Anywhere in the code
Sub test()
MsgBoxT hid, "Record Updated!", "Confirmation", vbInformation, 0, 1000
End Sub
答案 1 :(得分:0)
Public hid as string
Sub Main()
hid = Application.hWndAccessApp
End Sub
或
Private Sub Workbook_Open()
hid = Application.hWndAccessApp
End Sub