I created a macro in VBA to open an Internet Explorer window, go to Google, and search Google based on what is the the active cell when you trigger the macro. The macro also Maximizes the IE browser window.
The problem is that the macro does not run correctly every time. Often it will go to Google but then not fire the search. Other times it will return an error.
I need help (1) making sure the macro runs correctly every time and (2) resizing the IE window to 1/4 of the screen and placing it in the top left corner of the screen.
Here is my code:
Declare Function apiIEsize Lib "user32" Alias "ShowWindow" _
(ByVal hwnd As Long, ByVal CmdShow As Long) As Long
Global Const SW_MAXIMIZE = 3
Global Const SW_SHOWNORMAL = 1
Global Const SW_SHOWMINIMIZED = 2
Sub Product_Search_1()
Dim MyItem As String, MyIe As Object, MyLoop As Object, MyElements As Object
MyItem = ActiveCell.Value
Set MyIe = CreateObject("InternetExplorer.Application")
MyIe.navigate "www.google.com"
While MyIe.busy = True
Wend
Set MyElements = MyIe.document.getElementsbyTagName("Input")
For Each MyLoop In MyElements
If MyLoop.Name = "q" Then: MyLoop.Value = MyItem
If MyLoop.Value = "Google Search" Then
MyLoop.Focus
MyLoop.Click
MyLoop.fireevent ("onsubmit")
End If
Next MyLoop
MyIe.document.getelementbyid("_fZl").Click
MyIe.document.getelementbyid("lst-ib").Value = MyItem
MyIe.Visible = True
apiIEsize MyIe.hwnd, 3
End Sub
The problem is that the macro does not run correctly every time. Often it will go to Google but then not fire the search. Other times it will return an error.
I need help (1) making sure the macro runs correctly every time and (2) resizing the IE window to 1/4 of the screen and placing it in the top left corner of the screen.
Here is my code:
Declare Function apiIEsize Lib "user32" Alias "ShowWindow" _
(ByVal hwnd As Long, ByVal CmdShow As Long) As Long
Global Const SW_MAXIMIZE = 3
Global Const SW_SHOWNORMAL = 1
Global Const SW_SHOWMINIMIZED = 2
Sub Product_Search_1()
Dim MyItem As String, MyIe As Object, MyLoop As Object, MyElements As Object
MyItem = ActiveCell.Value
Set MyIe = CreateObject("InternetExplorer.Application")
MyIe.navigate "www.google.com"
While MyIe.busy = True
Wend
Set MyElements = MyIe.document.getElementsbyTagName("Input")
For Each MyLoop In MyElements
If MyLoop.Name = "q" Then: MyLoop.Value = MyItem
If MyLoop.Value = "Google Search" Then
MyLoop.Focus
MyLoop.Click
MyLoop.fireevent ("onsubmit")
End If
Next MyLoop
MyIe.document.getelementbyid("_fZl").Click
MyIe.document.getelementbyid("lst-ib").Value = MyItem
MyIe.Visible = True
apiIEsize MyIe.hwnd, 3
End Sub