Declare Function SendMessage Lib "user32" Alias "SendMessageA" _ (ByVal hwnd as Long, ByVal wmsg as Long, ByVal WParam As Integer, _ ByVal LParam as long) as long
Declare Function FindWindow Lib "User32" Alias _
"Findwindowa" (ByVal lpclassname As String, ByVal _ Lpwindowname As String) as Long
Declare Function lockwindowupdate Lib "user32" (ByVal Hwndlock as long) as long
Private Const Wm_setredraw = &HB
Private Sub Trythis ()
Dim h as Long
h = FindWindow ("Xlmain", vbNullString)
LockWindowUpdate Application.VBE.MainWindow.hwnd
Call SendMessage (H, wm_setredraw, 0, 0)
Application.sendkeys "Range", True
Application.sendkeys "~", True
Application.Goto reference:= "Trythis"
On Error Resume Next ' in case it's not there
With ThisWorkbook.VBProject.VBComponents ("Module2"). CodeModule
. DeleteLines 1,. CountOfLines
End with
Call SendMessage (H, Wm_setredraw, 1, 0)
Application.VBE.MainWindow.Visible = False
LockWindowUpdate 0
Thisworkbook.close True
End Sub
Winapi:findwindow, FindWindowEx-Find window
{window's class name} {title of window} {Returns a handle to the window; return 0} //findwindowex Two more handle parameters than FindWindow: FindWindowEx (Parent:hwnd; {To find the parent window handle of a child window}{child window handle}{}{}}: HWND; {If the parent is 0, the function looks in the desktop window as the parent window, looking for all the child windows of the Desktop window; if hwnd_message, the function only looks for all message windows; The child window must be a direct child window of the parent window; The first subwindow of the rent begins; if both Parent and child are 0, the function finds all the top-level windows and the message window.}
//Test 1: Try to find a handle to the new program's main windowvarH:hwnd;begin{Now we know that the title of the window is: Form1, the window's class name is: TForm1}H: = FindWindow (' TForm1 ',' Form1 '); ShowMessage (IntToStr (h));{656180; this is random, each startup window must be different}{If you do not know the class name}H: = FindWindow (Nil,' Form1 '); ShowMessage (IntToStr (h));{656180} {If you don't know the title name}H: = FindWindow (' TForm1 ',Nil); ShowMessage (IntToStr (h));{656180}{Actually the handle of this window is not self.handle}ShowMessage (IntToStr (Handle));{656180}End;
//Test 2: Find the handle to the Calculator window (first turn on the calculator) var h:hwnd; begin {If it is not a simplified Chinese system, this may not work} h: = FindWindow (nil' calculator '{1508334}}{preferably, But you have to know in advance. The class name of the Calculator window is: Scicalc} H: = FindWindow (' scicalc 'nil{1508334}End ;
FindWindow Find Window