2013-01-20 86 views
1

我正在使用鍵盤快捷方式運行vbs文件。儘管代碼運行良好,但vbs快捷鍵的問題在於,只要按下鍵盤快捷鍵,前景中的窗口就會失去焦點。 (您可以通過在某處放置一個空的vbs文件來嘗試,例如在開始菜單文件夾中的文件快捷方式,分配給該快捷方式的鍵盤快捷方式以及按下快捷鍵。)將下一個窗口設置爲活動窗口(ALT + TAB)

我發現通過使用ALT + TAB,我可以得到前景窗口重新獲得焦點。但是,我無法在VBA中重複此功能。顯然,ShellObject.SendKeys("%{TAB}")不起作用...

有什麼辦法來實現VBA中ALT + TAB的功能嗎?提前致謝。

編輯

在此期間,我做了一個開關,AutoIt的,看它是否可以讓我進一步。這是我的了:

ControlFocus("[CLASS:CabinetWClass]", "", "[CLASS:DirectUIHWND]") 

我注意到,選擇資源管理器窗口(即CabinetWClass),在某些情況下是不夠的。這就是爲什麼我將重點放在實際包含文件/文件夾的控件上。

它的工作非常好,但我仍然希望有一個VBA解決方案:)

回答

0

您可以使用Windows的API將其帶回頂端?在Excel中運行帶來的記事本上時,這對我的作品,所以它應該工作。您將不得不在FindWindow調用中替換您的窗口的名稱。

注意:您應該是與一些標誌的真小心,因爲如果你做他們的一些錯誤的組合,你會得到一些奇怪的行爲。

Public Declare Function FindWindow Lib "user32" _ 
    Alias "FindWindowA" (_ 
    ByVal lpClassName As String, _ 
    ByVal lpWindowName As String) As Long 


Declare Function SetWindowPos Lib "user32.dll" (ByVal hwnd As _ 
     Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As _ 
     Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long 

'Either the handle of the window to position this window behind, or exactly one of the following flags stating where in the 
'Z-order to put the window: 
Private Const HWND_BOTTOM = 1    'Put the window at the bottom of the Z-order. 
Private Const HWND_NOTOPMOST = -2   'Put the window below all topmost windows and above all non-topmost windows. 
Private Const HWND_TOP = 0    'Put the window at the top of the Z-order. 
Private Const HWND_TOPMOST = -1   'Make the window topmost (above all other windows) permanently. 

'x: The x coordinate of where to put the upper-left corner of the window. 
'y: The y coordinate of where to put the upper-left corner of the window. 
'cx: The x coordinate of where to put the lower-right corner of the window. 
'cy: The y coordinate of where to put the lower-right corner of the window. 

'Flags: Zero or more of the following flags stating how to move the window: 
Private Const SWP_DRAWFRAME = &H20   'Same as SWP_FRAMECHANGED. 
Private Const SWP_FRAMECHANGED = &H20   'Fully redraw the window in its new position. 
Private Const SWP_HIDEWINDOW = &H80   'Hide the window from the screen. 
Private Const SWP_NOACTIVATE = &H10   'Do not make the window active after moving it unless it was already the active window. 
Private Const SWP_NOCOPYBITS = &H100   'Do not redraw anything drawn on the window after it is moved. 
Private Const SWP_NOMOVE = &H2    'Do not move the window. 
Private Const SWP_NOSIZE = &H1    'Do not resize the window. 
Private Const SWP_NOREDRAW = &H8    'Do not remove the image of the window in its former position, effectively leaving a ghost image on the screen. 
Private Const SWP_NOZORDER = &H4    'Do not change the window's position in the Z-order. 
Private Const SWP_SHOWWINDOW = &H40   'Show the window if it is hidden. 


Sub ShowWindow() 
    Dim hwnd As Long 'handle to get the window 
    Dim flags As Long ' the flags specifying how to move the window 
    Dim retval As Long ' return value 

    hwnd = FindWindow(vbNullString, "Untitled - Notepad") 
    flags = SWP_NOSIZE Or SWP_DRAWFRAME 
    retval = SetWindowPos(hwnd, HWND_TOP, 0, 0, 1, 1, flags) ' move the window 
End Sub 
+0

非常感謝!這解決了vba中的問題。可能仍然使用了AutoIt雖然:P – Daan

+0

不用擔心,謝謝你把AutoIt的在我的雷達。 10年來,甚至沒有想過這種自動化! – CuberChase

0

也許嘗試AppActivate功能,如果你知道你要切換過的項目,你可能不知道中的名字這個案例。

否則,試試這個:

SendKeys "%{TAB}", True 
+0

謝謝,SendKeys函數不幸地不起作用。在使用AppActivate功能可能無法正常工作,因爲我只知道,窗口是由Explorer.exe的,它是在堆棧中的下一個窗口(即ALT + TAB)。 – Daan