自动响应消息框中的提示
下面是在vbaexpress论坛上找到的一个例子:
使用VBA打开另一个工作簿并运行其中的宏,并且自动响应消息框中的提示。
这个例子很有意思,也有参考性,因此辑录于此。
在主调程序所在的工作簿中放置下面的代码:
Option Explicit
Public Declare Function SetTimer& Lib “user32″ (ByVal hwnd&, _
ByVal nIDEvent&, ByVal uElapse&, ByVal lpTimerFunc&)
Private Declare Function KillTimer& Lib “user32″ (ByVal hwnd&, _
ByVal nIDEvent&)
Public Const NV_INPUTBOX As Long = &H5000
Public Sub TimerProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal idEvent As Long, ByVal dwTime As Long)
SendKeys “%Y”
KillTimer hwnd, idEvent
End Sub
Sub test()
With Application
.Calculation = xlCalculationManual
.ScreenUpdating = False
End With
Dim targetworkbook As Workbook
Dim usersave As VbMsgBoxResult
Set targetworkbook = Workbooks.Open(”C:\test3.xls”, UpdateLinks:=0)
Calculate
targetworkbook.Activate
SetTimer 0, NV_INPUTBOX, 1000, AddressOf TimerProc
Application.Run targetworkbook.Name & “!tester”
targetworkbook.Activate
With Application
.Calculation = xlCalculationAutomatic
.ScreenUpdating = True
End With
End Sub
在被调用的工作簿中放置下面的代码:
Sub TimedMsgBox()
Dim cTime As Long
Dim WSH As Object
Set WSH = CreateObject(”WScript.Shell”)
cTime = 5 ‘5 secs
Select Case WSH.Popup(”Open an Excel file?!”, cTime, “Question”, vbOKCancel)
Case vbOK
MsgBox “You clicked OK”
Case vbCancel
MsgBox “You clicked Cancel”
‘ Case -1
‘ MsgBox “Timed out”
‘ Case Else
End Select
End Sub
并在主调过程中调用的该工作簿的tester过程中调用该代码过程。

YuFun:
呵呵
您太客气了,也感谢您到我的博客上留言
我一般是用Window Live Writer写的,代码部分是用一个叫Code Snippet的插件贴上去的:)
5 05月 2008, 2:15 pmhttp://lvildosola.blogspot.com/2007/02/code-snippet-plugin-for-windows-live.html