excel - 使用 VBA 在弹出窗口上自动按 Enter
问题描述
我需要Enter在 VBA 中出现弹出窗口时自动按下。问题是生成弹出窗口的代码是我无法访问的内置插件功能。
Enter在这种情况下,我怎样才能设法自动按下?
我试过Activeworkbook.LockServerFile
了,但它失败了。
解决方案
您可以通过使用Win32API向其发送WM_CLOSE
消息来关闭弹出窗口。SendMessage
您可以SendMessage
使用以下代码检索需要使用的弹出窗口的窗口句柄:
Option Explicit
Private Declare Function FindWindow Lib "User32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindowText Lib "User32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetWindowTextLength Lib "User32" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As Long
Private Declare Function GetWindow Lib "User32" (ByVal hWnd As Long, ByVal wCmd As Long) As Long
Private Declare Function IsWindowVisible Lib "User32" (ByVal hWnd As Long) As Boolean
Private Const GW_HWNDNEXT = 2
Private Sub Test()
Dim lhWndP As Long
If GetHandleFromPartialCaption(lhWndP, "Excel") = True Then
If IsWindowVisible(lhWndP) = True Then
MsgBox "Found VISIBLE Window Handle: " & lhWndP, vbOKOnly + vbInformation
Else
MsgBox "Found INVISIBLE Window Handle: " & lhWndP, vbOKOnly + vbInformation
End If
Else
MsgBox "Window 'Excel' not found!", vbOKOnly + vbExclamation
End If
End Sub
Private Function GetHandleFromPartialCaption(ByRef lWnd As Long, ByVal sCaption As String) As Boolean
Dim lhWndP As Long
Dim sStr As String
GetHandleFromPartialCaption = False
lhWndP = FindWindow(vbNullString, vbNullString) 'PARENT WINDOW
Do While lhWndP <> 0
sStr = String(GetWindowTextLength(lhWndP) + 1, Chr$(0))
GetWindowText lhWndP, sStr, Len(sStr)
sStr = Left$(sStr, Len(sStr) - 1)
If InStr(1, sStr, sCaption) > 0 Then
GetHandleFromPartialCaption = True
lWnd = lhWndP
Exit Do
End If
lhWndP = GetWindow(lhWndP, GW_HWNDNEXT)
Loop
End Function
来自:如何使用 FindWindow 在 VBA 中查找具有部分名称的可见或不可见窗口
此外,有关使用with的示例,请参阅带有sendmessage 且没有提示消息的 vba close pdf 文件。SendMessage
WM_CLOSE
推荐阅读
- python - seaborn残差/联合图中可能存在错误?
- reactjs - React Chart JS 2:为条形图的剩余间隙填充自定义样式
- angularjs - 在 AngularJS 项目中使用 Vue3 组件/路由
- flutter - 如何在自定义小部件构造函数中添加和使用键
- java - setOnClickListener onTouchEvent
- vue.js - 如何使用 jest 捕获和测试 Vue.js 组件中方法的错误
- google-apps-script - 以编程方式向多个表单添加表单触发器 AppsScript
- primefaces - 尝试重现自动完成标记时,JMeter 生成服务器错误
- amazon-web-services - AWS Glue 作业运行不遵守超时且不停止
- google-api - google-cloud-php 文档 AI:INVALID_ARGUMENT