哎呀,这个就比较难了。。。
用api吧:
Dim strRegKey, strCode
Set oExcel = CreateObject("Excel.Application")
set WshShell = CreateObject("wscript.Shell")
strRegKey = "HKEY_CURRENT_USER\Software\Microsoft\Office\$\Excel\Security\AccessVBOM"
strRegKey = Replace(strRegKey, "$", oExcel.Version)
WshShell.RegWrite strRegKey, 1, "REG_DWORD"
Set oBook = oExcel.Workbooks.Add
Set oModule = obook.VBProject.VBComponents.Add(1)
strCode = _
"Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long" & vbCrlf & _
"Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long"
oModule.CodeModule.AddFromString strCode Const WM_CLOSE = &H10
Dim hProcess
hProcess = FindWindow(vbNullString, "你想要结束的窗口标题名称")
SendMessage hProcess, WM_CLOSE, 0, 0 Function SendMessage(hwnd, wMsg, wParam, lParam)
SendMessage = oExcel.Run("SendMessage", hwnd, wMsg, wParam, lParam)
End Function
Function FindWindow(lpClassName, lpWindowName)
FindWindow = oExcel.Run("FindWindow", lpClassName, lpWindowName)
End Function
oExcel.DisplayAlerts = False
oBook.Close
oExcel.Quit