|
本帖最后由 perfect131 于 2023-3-31 14:59 编辑
虽然有点偏们,但我相信在某个时刻派上用场
当 vba 执行这段代码 Application.Dialogs(xlDialogPrintPreview).Show,当你不手动关闭 或者 ESC 时,后面的代码不能执行,这个时候怎么办??
sendkeys "{ESC}" 这个会让数字键盘失效,手动按 NUMLOCK才开启,不能每次手动按NUMLOCK
这个时候用到 vbs 关闭 或者 API 异步关闭,API 异步关闭如下:
1. API 异步关闭 打印预览对话框
- #If VBA7 Then
- 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
- Private Declare PtrSafe Function KillTimer Lib "user32" (ByVal hwnd As LongPtr, ByVal uIDEvent As Long) As Long
- #Else
- Private Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
- Private Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal uIDEvent As Long) As Long
- #End If
- Sub kong()
- '''异步关闭窗口
- Call ClosePrintPreview(Close_In_HowMany_Seconds_FromNow:=0) '<== Close PPreview in 0 sconds - Change argument value to suit
- '''打印预览对话框
- Application.Dialogs(xlDialogPrintPreview).Show
- End Sub
- Private Sub ClosePrintPreview(ByVal Close_In_HowMany_Seconds_FromNow As Long)
- Call SetTimer(Application.hwnd, 0, Abs(Close_In_HowMany_Seconds_FromNow) * 1000, AddressOf CloseNow)
- End Sub
- Private Sub CloseNow()
- With Application
- Call KillTimer(.hwnd, 0)
- If .CommandBars.GetEnabledMso("PrintPreviewClose") Then
- .CommandBars.ExecuteMso ("PrintPreviewClose")
- End If
- End With
- End Sub
复制代码 2.API 异步关闭 Application.Dialogs(xlDialogFormulaFind).Show , 2, 2
- #If VBA7 Then
- 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
- Private Declare PtrSafe Function KillTimer Lib "user32" (ByVal hwnd As LongPtr, ByVal uIDEvent As Long) As Long
- Private Declare PtrSafe Sub keybd_event Lib "user32" (ByVal bVk As Byte, _
- ByVal bScan As Byte, _
- ByVal dwFlags As Long, _
- ByVal dwExtraInfo As LongPtr)
- #Else
- Private Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
- Private Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal uIDEvent As Long) As Long
- Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
- #End If
- Const KEYEVENTF_KEYUP = &H2
- Const VK_ESCAPE = &H1B
- Sub kong()
- '''异步关闭窗口
- Call ClosePrintPreview(Close_In_HowMany_Seconds_FromNow:=0) '<== Close PPreview in 0 sconds - Change argument value to suit
- '''不用遍历工作表,整个工作簿替换
- Application.Dialogs(xlDialogFormulaFind).Show , 2, 2
- ThisWorkbook.Activate
- '''被替换 What ,,新内容 Replacement
- Cells.Replace What:="bbb", Replacement:="aaa", LookAt:=xlPart, SearchOrder _
- :=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
- End Sub
- Private Sub ClosePrintPreview(ByVal Close_In_HowMany_Seconds_FromNow As Long)
- Call SetTimer(Application.hwnd, 0, Abs(Close_In_HowMany_Seconds_FromNow) * 1000, AddressOf CloseNow)
- End Sub
- Private Sub CloseNow()
- Call KillTimer(Application.hwnd, 0)
- '按下 ESC
- keybd_event VK_ESCAPE, 0, 0, 0
- '释放 ESC
- keybd_event VK_ESCAPE, 0, KEYEVENTF_KEYUP, 0
- End Sub
复制代码 一般 ESC 就能 关闭 关闭对话框了
|
评分
-
3
查看全部评分
-
|