vba - 声明函数 vba ms 访问时出现编译错误
问题描述
我正在尝试实现一个脚本,该脚本将禁用 ms 访问窗口的“关闭”按钮。
但是,尝试声明函数时出现编译错误:
Option Compare Database
Option Explicit
Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVal wRevert As Long) As Long
Private Declare Function EnableMenuItem Lib "user32" (ByVal hMenu As Long, ByVal wIDEnableItem As Long, ByVal wEnable As Long) As Long
Public Sub AccessCloseButtonEnabled(pfEnabled As Boolean)
' Comments: Control the Access close button.
' Disabling it forces the user to exit within the application
' Params : pfEnabled TRUE enables the close button, FALSE disabled it
' Owner : Copyright (c) FMS, Inc.
' Source : Total Visual SourceBook
' Usage : Permission granted to subscribers of the FMS Newsletter
On Error Resume Next
Const clngMF_ByCommand As Long = &H0&
Const clngMF_Grayed As Long = &H1&
Const clngSC_Close As Long = &HF060&
Dim lngWindow As Long
Dim lngMenu As Long
Dim lngFlags As Long
lngWindow = Application.hWndAccessApp
lngMenu = GetSystemMenu(lngWindow, 0)
If pfEnabled Then
lngFlags = clngMF_ByCommand And Not clngMF_Grayed
Else
lngFlags = clngMF_ByCommand Or clngMF_Grayed
End If
Call EnableMenuItem(lngMenu, clngSC_Close, lngFlags)
End Sub
翻译:函数编译出错。Visual Basic 模块中的语法错误。检查程序,然后重新编译。
你认为可能是什么导致了这个问题?
解决方案
声明不是 64 位兼容的,并且您使用 longs 而不是布尔值来表示 wRevert 和 EnableMenuItem 的返回。您可以尝试以下方法(需要 VBA7 (Office 2010+) 以获得 LongPtr 支持):
Private Declare PtrSafe Function GetSystemMenu Lib "user32" (ByVal hwnd As LongPtr, ByVal wRevert As Boolean) As LongPtr
Private Declare PtrSafe Function EnableMenuItem Lib "user32" (ByVal hMenu As LongPtr, ByVal wIDEnableItem As Long, ByVal wEnable As Long) As Boolean
推荐阅读
- macos - LazyVGrid 中的 NavigationLink 在 swiftUI macOS 中无法正确显示
- f# - 如何使用 F# 的 rx 扩展从简单值创建 Observable?
- windows - iFuse 在 Windows 上访问 iPhone 文件
- mysql - mysql group by 返回所有行
- yocto - meta-qt5 yocto 层无法在 qt 版本 5.15.2 上编译
- python-3.x - 为什么列表变量有时不受函数更改的影响,因为我认为 python3 通过引用传递与列表变量一起工作?
- python - ord () 期望一个字符
- assembly - 在汇编中打印一个三角形的字符
- wordpress - cURL vs wp_remote_get:对于插件中的外部 API 调用,哪一个最可靠?
- websphere - 如何在 Open Liberty 服务器开发模式下调试我的测试(使用 liberty-maven-plugin)并打开/关闭调试器?