最终代码如下:- ' VBS 调用API设置IE显示模式(只允许最小化、最大化) by Yu2n
- On Error Resume Next
- Dim wso
- set wso = CreateObject("WScript.Shell")
- wso.RegWrite "HKCU\Software\Microsoft\Office\11.0\Excel\Security\AccessVBOM",1,"REG_DWORD"
- wso.RegWrite "HKCU\Software\Microsoft\Office\12.0\Excel\Security\AccessVBOM",1,"REG_DWORD"
- wso.RegWrite "HKCU\Software\Microsoft\Office\14.0\Excel\Security\AccessVBOM",1,"REG_DWORD"
- Dim oExcel, oBook, oModule
- Set oExcel = CreateObject("Excel.Application")
- Set oBook = oExcel.Workbooks.Add
- Set oModule = oBook.VBProject.VBComponents.Add(1)
- strCode = _
- "Private Declare Function FindWindow Lib ""user32"" Alias ""FindWindowA"" ( _" & vbCr &_
- " ByVal lpClassName As String, ByVal lpWindowName As String) As Long" & vbCr &_
- "Private Declare Function GetDesktopWindow Lib ""user32"" () As Long" & vbCr &_
- "Private Declare Function GetWindow Lib ""user32"" ( _" & vbCr &_
- " ByVal hwnd As Long, ByVal wCmd As Long) As Long" & vbCr &_
- "Private Declare Function GetClassName Lib ""user32"" Alias ""GetClassNameA"" ( _" & vbCr &_
- " ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long" & vbCr &_
- "Private Declare Function SendMessage Lib ""user32"" Alias ""SendMessageA"" ( _" & vbCr &_
- " ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long" & vbCr &_
- "Private Declare Function IsZoomed Lib ""user32"" (ByVal hwnd As Long) As Long" & vbCr &_
- "Private Declare Function IsIconic Lib ""user32"" (ByVal hwnd As Long) As Long" & vbCr &_
- "Private Const GW_HWNDFIRST = 0" & vbCr &_
- "Private Const GW_HWNDNEXT = 2" & vbCr &_
- "Private Const GW_CHILD = 5" & vbCr &_
- "Private Const WM_SYSCOMMAND = &H112" & vbCr &_
- "Private Const SC_MAXIMIZE = &HF030&" & vbCr &_
- "Public Sub IE_Maximize()" & vbCr &_
- " Dim nHWnd As Long" & vbCr &_
- " Dim s As String * 260" & vbCr &_
- " nHWnd = GetDesktopWindow()" & vbCr &_
- " nHWnd = GetWindow(nHWnd, GW_CHILD Or GW_HWNDFIRST)" & vbCr &_
- " While nHWnd <> 0" & vbCr &_
- " nHWnd = GetWindow(nHWnd, GW_HWNDNEXT)" & vbCr &_
- " s = String(256, Chr(0)): GetClassName nHWnd, s, 255: s = Replace(s, Chr(0), """")" & vbCr &_
- " If InStr(1, s, ""IEFRAME"", vbTextCompare) > 0 Then If nHWnd <> 0 Then WindowMaximize nHWnd" & vbCr &_
- " Wend" & vbCr &_
- "End Sub" & vbCr &_
- "Private Sub WindowMaximize(ByVal nHWnd As Long)" & vbCr &_
- " If IsZoomed(nHWnd) = 0 And IsIconic(nHWnd) = 0 Then SendMessage nHWnd, WM_SYSCOMMAND, SC_MAXIMIZE, ByVal 0" & vbCr &_
- "End Sub"
- oModule.CodeModule.AddFromString strCode
- oExcel.DisplayAlerts = False
- Do
- oExcel.Run "IE_Maximize"
- WScript.Sleep 1000
- Loop
- oBook.Close False
- oExcel.Quit
复制代码
|