2013-07-18 35 views
0

我无法理解什么是用下面的代码的问题 -多次调用BringWindowToTop不工作

i = 15 
While (i < 100) 
    If i Mod 2 = 0 Then 
     handle = FindWindow(vbNullString, "My Details - Windows Internet Explorer") 
     Range("A1").Value = handle 
     BringWindowToTop handle 
     Application.Wait DateAdd("s", 1, Now) 
    Else 
     handle1 = FindWindow(vbNullString, "Codeomnitrix - Outlook Web App - Mozilla Firefox") 
     Range("A2").Value = handle1 
     BringWindowToTop handle1 
     Application.Wait DateAdd("s", 1, Now) 
    End If 

    i = i + 15 
Wend 

应该两个窗口之间切换,让他们成为市场关注焦点至凌晨1秒但实际上发生了什么它只是把Firefox放在最上面,然后再切换。

谢谢

+0

'FindWindow(vbNullString,“Codeomnitrix - Outlook Web App - Mozilla Firefox”)是否返回零或值?例如,我使用Excel进行了测试:**“test.xlsm - Microsoft Excel”** Findwindow()必须查找**“test.xlsm”**,而不是使其工作。 – Raybarg

+0

否Raybarg它正在为这两个窗口返回适当的值,并且如果我一次运行的东西它正常工作。我的意思是,如果只有IE窗口需要带到前面或只有Firefox窗口需要带前,它工作正常,但切换不起作用 – codeomnitrix

+0

你应该在'If()'内的Debug.Print'阻止** i **的值和**处理**,看看它是否贯穿整个循环。我用File Explorer和Excel测试了你的代码,它工作正常。可能是firefox捕捉到API钩子,也许是一些反弹的东西呢?我只是猜测... – Raybarg

回答

1

如果你考虑使用Autoit,这可以是非常简单的。

添加引用到自动DLL。

enter image description here

Download Autoit

Sub test() 

    Dim oAutoit As New AutoItX3 
    oAutoit.Opt "WinTitleMatchMode", 2 ' Match any substring in the title 
    oAutoit.WinActivate "My Details - Windows Internet Explorer" ' Activates (gives focus to) a window. 

End Sub 
+0

谢谢Santosh,但你看我想用findWindow和bringWindowtoFront来做。 – codeomnitrix

+2

@codeomnitrix Autoit内部使用Windows API。它是一个基本上隐藏了Windows API复杂性的类,使得它更易于使用。 – Santosh

+0

是Santosh,我明白了,但你看到这个问题只是为了理解api – codeomnitrix

0

我发现启动时最小化的窗口不会弹出。

Private Declare Function GetWindowPlacement Lib _ 
     "user32" (ByVal hwnd As Integer, _ 
     ByRef lpwndpl As WINDOWPLACEMENT) As Integer 
Private Declare Function SetWindowPlacement Lib "user32" _ 
     (ByVal hwnd As Integer, ByRef lpwndpl As WINDOWPLACEMENT) As Integer 

Private Const SW_SHOWMINIMIZED As Short = 2 
Private Const SW_SHOWMAXIMIZED As Short = 3 
Private Const SW_SHOWNORMAL As Short = 1 

Private Structure WINDOWPLACEMENT 
    Dim length As Integer 
    Dim flags As Integer 
    Dim showCmd As Integer 
    Dim ptMinPosition As POINTAPI 
    Dim ptMaxPosition As POINTAPI 
    Dim rcNormalPosition As RECT 
End Structure 

[...] 

Dim handle As Integer 
Dim wp As WINDOWPLACEMENT 

handle = FindWindow(vbNullString, "My Details - Windows Internet Explorer") 
GetWindowPlacement(handle, wp) 
wp.showCmd = SW_SHOWNORMAL 
SetWindowPlacement(handle, wp) 

所有这些从Codeproject article复制来恢复窗口。

+0

谢谢Raybarg :)我会检查并更新你。 – codeomnitrix