ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 587|回复: 1

[分享] vba 用 API 异步关闭 ,打开的对话框

[复制链接]

TA的精华主题

TA的得分主题

发表于 2023-3-31 14:56 | 显示全部楼层 |阅读模式
本帖最后由 perfect131 于 2023-3-31 14:59 编辑

        虽然有点偏们,但我相信在某个时刻派上用场

        当 vba 执行这段代码 Application.Dialogs(xlDialogPrintPreview).Show,当你不手动关闭 或者 ESC 时,后面的代码不能执行,这个时候怎么办??
       sendkeys "{ESC}" 这个会让数字键盘失效,手动按 NUMLOCK才开启,不能每次手动按NUMLOCK
       这个时候用到 vbs 关闭 或者 API 异步关闭,API 异步关闭如下:
      1. API 异步关闭 打印预览对话框
   
  1. #If VBA7 Then
  2.     Private Declare PtrSafe Function SetTimer Lib "user32" (ByVal hwnd As LongPtr, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As LongPtr) As Long
  3.     Private Declare PtrSafe Function KillTimer Lib "user32" (ByVal hwnd As LongPtr, ByVal uIDEvent As Long) As Long
  4. #Else
  5.     Private Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
  6.     Private Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal uIDEvent As Long) As Long
  7. #End If
  8. Sub kong()
  9.     '''异步关闭窗口
  10.     Call ClosePrintPreview(Close_In_HowMany_Seconds_FromNow:=0) '<== Close PPreview in 0 sconds - Change argument value to suit
  11.     '''打印预览对话框
  12.     Application.Dialogs(xlDialogPrintPreview).Show
  13. End Sub
  14. Private Sub ClosePrintPreview(ByVal Close_In_HowMany_Seconds_FromNow As Long)
  15.     Call SetTimer(Application.hwnd, 0, Abs(Close_In_HowMany_Seconds_FromNow) * 1000, AddressOf CloseNow)
  16. End Sub
  17. Private Sub CloseNow()
  18.     With Application
  19.         Call KillTimer(.hwnd, 0)
  20.         If .CommandBars.GetEnabledMso("PrintPreviewClose") Then
  21.             .CommandBars.ExecuteMso ("PrintPreviewClose")
  22.         End If
  23.     End With
  24. End Sub
复制代码
        2.API 异步关闭 Application.Dialogs(xlDialogFormulaFind).Show , 2, 2
  1. #If VBA7 Then
  2.     Private Declare PtrSafe Function SetTimer Lib "user32" (ByVal hwnd As LongPtr, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As LongPtr) As Long
  3.     Private Declare PtrSafe Function KillTimer Lib "user32" (ByVal hwnd As LongPtr, ByVal uIDEvent As Long) As Long
  4.     Private Declare PtrSafe Sub keybd_event Lib "user32" (ByVal bVk As Byte, _
  5.                                                           ByVal bScan As Byte, _
  6.                                                           ByVal dwFlags As Long, _
  7.                                                           ByVal dwExtraInfo As LongPtr)
  8. #Else
  9.     Private Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
  10.     Private Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal uIDEvent As Long) As Long
  11.     Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
  12. #End If
  13. Const KEYEVENTF_KEYUP = &H2
  14. Const VK_ESCAPE = &H1B
  15. Sub kong()
  16.     '''异步关闭窗口
  17.     Call ClosePrintPreview(Close_In_HowMany_Seconds_FromNow:=0) '<== Close PPreview in 0 sconds - Change argument value to suit
  18.     '''不用遍历工作表,整个工作簿替换
  19.     Application.Dialogs(xlDialogFormulaFind).Show , 2, 2
  20.     ThisWorkbook.Activate
  21.     '''被替换 What  ,,新内容 Replacement
  22.     Cells.Replace What:="bbb", Replacement:="aaa", LookAt:=xlPart, SearchOrder _
  23.         :=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
  24. End Sub
  25. Private Sub ClosePrintPreview(ByVal Close_In_HowMany_Seconds_FromNow As Long)
  26.     Call SetTimer(Application.hwnd, 0, Abs(Close_In_HowMany_Seconds_FromNow) * 1000, AddressOf CloseNow)
  27. End Sub
  28. Private Sub CloseNow()
  29.     Call KillTimer(Application.hwnd, 0)
  30.     '按下 ESC
  31.     keybd_event VK_ESCAPE, 0, 0, 0
  32.     '释放 ESC
  33.     keybd_event VK_ESCAPE, 0, KEYEVENTF_KEYUP, 0
  34. End Sub
复制代码
一般 ESC 就能 关闭 关闭对话框了

评分

3

查看全部评分

TA的精华主题

TA的得分主题

 楼主| 发表于 2023-3-31 15:10 | 显示全部楼层
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-11-17 16:49 , Processed in 0.039705 second(s), 14 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表