2013-01-20 62 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