-
Aug 13th, 2020, 03:09 PM
#1
Thread Starter
New Member
Automate some steps in IE, with VBA
hello everybody,
i`m here to search for some help, if is possible.
i was trying by myself to scrap some vba code to automate some steps in a IE windows. i saw some tutorials on YT, try using some code, but at the end, when i saw thinking that i made`t, the machine where i was trying to use the script, was not recognize this part SetForegroundWindow HWNDSrc (that suppose to bring forward/activate the IE window).
i mentioned from the beginning, that i don`t use to search for ID name, cause is an closed program that work in IE.
what i what this code to do:
1. search for IE windows with the string IBAN in here name.
2. activate/bring forward the IE
3. use a command like Ctrl&S
4. use [tab] button like 20 times until is reaching the tab that i need
5. there will be a combo box where i wanna select something (let`s say something that i write in cell A1
6. after selection is done, go again 5 [tabs] to go to another combo box.
7. select again something from cell A2
8. use a command for save Ctrl&S
end of the script.
thx in advance!
here is the code:
Code:
Sub test()
Dim i As Long
Dim URL As String
Dim IE As Object
Dim objElement As Object
Dim objCollection As Object
Dim HWNDSrc As Long
marker = 0
Set objShell = CreateObject("Shell.Application")
IE_count = objShell.Windows.Count
For x = 0 To (IE_count - 1)
On Error Resume Next ' sometimes more web pages are counted than are open
my_url = objShell.Windows(x).Document.Location
my_title = objShell.Windows(x).Document.Title
If my_title Like "*" & "IBAN" & "*" Then 'compare to find if the desired web page is already open
Set IE = objShell.Windows(x)
marker = 1
Exit For
Else
End If
Next
If marker = 0 Then
MsgBox ("A matching webpage was NOT found")
Else
'MsgBox ("A matching webpage was found")
'Get Window ID for IE so we can set it as activate window
HWNDSrc = IE.HWND
'Set IE as Active Window
SetForegroundWindow HWNDSrc
For Each itm In IE.Document.all
If itm = "[object HTMLInputElement]" Then
n = n + 1
If n = 3 Then
itm.Value = "orksheet"
itm.Focus
'Activates the Input box (makes the cursor appear)
Application.SendKeys "^{N}", True
Application.SendKeys "{TAB}", True
Application.SendKeys "{TAB}", True
Application.SendKeys "{R}", True
'until keystroke has finished before proceeding, allowing
'javascript on page to run and filter the table
GoTo endmacro
End If
End If
Next
Unload IE
endmacro:
Set IE = Nothing
Set objElement = Nothing
Set objCollection = Nothing
End If
End Sub
Last edited by si_the_geek; Aug 17th, 2020 at 09:40 AM.
Reason: added Code tags
-
Aug 13th, 2020, 03:13 PM
#2
Re: Automate some steps in IE, with VBA
please format your code with the # icon, it is not readable.
The best friend of any programmer is a search engine
"Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
“They did not know it was impossible so they did it” (Mark Twain)
-
Aug 17th, 2020, 09:24 AM
#3
Re: Automate some steps in IE, with VBA
VBA is Office Development. This is the VBGeneral Forum...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 17th, 2020, 09:42 AM
#4
Re: Automate some steps in IE, with VBA
Welcome to VBForums
Thread moved from the 'VB.Net' forum to the 'Office Development/VBA' forum.
-
Aug 18th, 2020, 04:52 AM
#5
Re: Automate some steps in IE, with VBA
was not recognize this part SetForegroundWindow HWNDSrc (that suppose to bring forward/activate the IE window).
that is a call to an API, you would need to declare the API function, which would then go at the top of the code module in the general section, or as public in a module, you can search on the SetForegroundWindow API
an easy alternative might be to use appactivate windowname, but you would have to test if it works with the correct tab in internet explorer
you use on error resume next in case more shell windows are counted, but when you are past that you should go back to having errors halting the code so you can see what is happening
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|