我有一个代码,可以执行从Excel工作表到以谷歌浏览器打开的网站的数据输入。该代码工作正常。我只希望Excel和Chrome窗口在宏运行时并排放置在顶部。 excel窗口被完美地定位在左侧,但是对chrome窗口没有影响。谷歌搜索了很多之后,我不知道解决方案。 Chrome和数据输入受Selenium控制。
'This part has code to import window resizing functions.
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function GetWindowRect Lib "user32" ( _
ByVal hWnd As Long, lpRect As RECT) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
ByVal lpClassName As Any, ByVal lpWindowName As Any) As Long
Private Declare Function MoveWindow Lib "user32" ( _
ByVal hWnd As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, _
ByVal nHeight As Long, ByVal bRepaint As Long) As Long
Private Declare Function BringWindowToTop Lib "user32" (ByVal hWnd As Long) As Long
Sub Selenuim_Upload()
Dim test1 As Long, test2 As Long
test1 = Timer
Dim obj As New WebDriver
obj.Start "chrome", ""
obj.Get "https://csa.xyz.com/"
obj.FindElementById("ContentPlaceHolder1_ddlUserProfile").SendKeys ("Collector")
obj.FindElementById("ContentPlaceHolder1_btn_login").Click
obj.Get "https://csa.xyz.com/Collector_view.aspx/Default.aspx/"
'------------------------------------------------------------------------
' This part calculates window sizes and moves Excel window to left and IE window to right.
Dim hWnd As Long
Dim R As RECT, LW As RECT, RW As RECT
'Get the size of the deskop
If GetWindowRect(GetDesktopWindow, R) = 0 Then Exit Sub
'Calculate the left and right side
LW = R
LW.Right = R.Left + (R.Right - R.Left) / 2
RW = R
RW.Left = R.Right - (R.Right - R.Left) / 2
'Move Excel to the left
hWnd = FindWindow("XLMAIN", vbEmpty)
With LW
MoveWindow hWnd, .Left, .Top, .Right - .Left, .Bottom - .Top, True
End With
BringWindowToTop hWnd
'Move Chrome to the right
hWnd = FindWindow("Chrome_WidgetWin_1", vbEmpty)
With RW
MoveWindow hWnd, .Left, .Top, .Right - .Left, .Bottom - .Top, True
End With
BringWindowToTop hWnd
'------------------------------------------------------------------------
'Select Invoice Number in Serch By box
obj.FindElementById("ContentPlaceHolder1_ddlSearch").SendKeys ("Inv Number")
Range("A1").Select
'REST OF THE CODE CONTINUES FROM HERE
答案 0 :(得分:0)
我已经找到解决方案。感谢这个线程link。我使用winlister(由nirsoft开发)找到了该宏正在打开的网站的窗口标题。然后在hWnd = FindWindow("Chrome_WidgetWin_1", vbEmpty)
中用vbEmpty替换掉它,因此最后一行代码就像hWnd = FindWindow("Chrome_WidgetWin_1", "https://csa.xyz.com/Collector_view.aspx/Default.aspx/ - Google Chrome")
一样。
现在代码可以同时排列excel和chrome窗口了。