The macro I am building takes names from an Excel spreadsheet, opens Internet Explorer, and searches the online directory. After searching the directory, it pulls up a Java form with the manager’s name in it. I am able to manually tab to the manager name, right click, copy shortcut, and then post it back on the spread sheet. However, I am having problems with consistent tabbing and copying the shortcut.
- Is there a simple way of bringing focus back onto the IE window?
- How do you copy a shortcut without manually clicking it?
Code:
Sub Macro1()
'
Dim ie As Object
Set ie = CreateObject("internetexplorer.application")
ie.Visible = True
ie.navigate "****url****"
While ie.busy
DoEvents
Wend
ie.document.getElementById("SSOID").Value = "Z19516732"
ie.document.getElementById("Advanced").Checked = False
ie.document.all("Search").Click
'this loop is to slow the macro as the java form is filled from the search
For i = 1 To 400000000
i = i + 1
Next i
'ie.Object.Activate
ie.document.getElementById("Advanced").Checked = False
ie.document.getElementById("SSOID").Focus
Application.SendKeys "{TAB 6}" ', True
'bring up the control menu/right click
Application.SendKeys "+{F10}"
'copy shortcut is 8 items down on the list
Application.SendKeys "{DOWN}"
Application.SendKeys "{DOWN}"
Application.SendKeys "{DOWN}"
Application.SendKeys "{DOWN}"
Application.SendKeys "{DOWN}"
Application.SendKeys "{DOWN}"
Application.SendKeys "{DOWN}"
Application.SendKeys "{DOWN}"
'enter was not working so the shortcut for the menu is 't'
'SendKeys "{ENTER}"
Application.SendKeys "{t}"
Windows("Book21").Activate
Range("A1").Select
ActiveSheet.Paste
End Sub
At the beginning of your module, put this line of code:
This is called a
Declare Statementand will allow you to access theSetForegroundWindowfunction that is built into Windows. This function lives in theuser32DLL of the Windows system. There are actually a number of other functions among multiple DLLs that are accessible to VBA in this way (see link for more examples).In your code, while interacting with your IE object, record the
HWND(handle to that window) like so:Then, after you’ve interacted with Java, use this to continue:
This tells the Windows system to set the window identified by the
HWNDas the foreground window (as the name implies).However, this may not be necessary, depending on how you are interacting with IE. In other words, if you don’t need to see/touch the window, you can still interact using the object as you have in your code already.
There are ways to get the shortcut you are looking for using code like
GetElementById()andGetElementsByTagName()(see here for more info), but it will depend on how the source was created. e.g. an<a href="...>link should be relatively easy to pull, if you know the HTML source.After reviewing your code a second time, I noticed you use a loop to ‘slow down’ the macro. I have a function I use all the time for similar methods of my own. Hopefully this will help in you getting done what you need. I’ve modified my code below from my own original, since I had additional specifics that don’t apply to your case. If there are any errors with it, I can adjust as needed.