AW: vba Code ausführen, bei Wechsel zu Excel
14.08.2009 21:53:59
Nepumuk
Hallo Karsten,
ein Beispielcode:
In das Modul "DieseArbeitsmappe":
Option Explicit
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Call prcStop
End Sub
Private Sub Workbook_Open()
blnActiv = True
Call prcStart
End Sub
In ein Standardmodul:
Option Explicit
Option Private Module
Private Declare Function SetTimer Lib "user32.dll" ( _
ByVal hwnd As Long, _
ByVal nIDEvent As Long, _
ByVal uElapse As Long, _
ByVal lpTimerFunc As Long) As Long
Private Declare Function KillTimer Lib "user32.dll" ( _
ByVal hwnd As Long, _
ByVal nIDEvent As Long) As Long
Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Private Declare Function GetActiveWindow Lib "user32.dll" () As Long
Private Const GC_CLASSNAMEMSEXCEL = "XLMAIN"
Private lnghWnd As Long
Public blnActiv As Boolean
Public Sub prcStart()
lnghWnd = FindWindow(GC_CLASSNAMEMSEXCEL, Application.Caption)
Call SetTimer(lnghWnd, 0, 100, AddressOf TimerProc)
End Sub
Public Sub prcStop()
Call KillTimer(lnghWnd, 0)
End Sub
Private Sub TimerProc(ByVal hwnd As Long, ByVal nIDEvent As Long, _
ByVal uElapse As Long, ByVal lpTimerFunc As Long)
If GetActiveWindow = lnghWnd Then
If Not blnActiv Then
Call prcYour_Procedur 'Aufruf deiner Prozedur
blnActiv = True
End If
Else
blnActiv = False
End If
End Sub
Private Sub prcYour_Procedur() 'Deine Prozedur
Static intIndex As Integer
intIndex = intIndex + 1
MsgBox "Excel agree focus " & CStr(intIndex) & ". time", 64, "Info"
End Sub
Gruß
Nepumuk