PDA

View Full Version : Solved: Select Checkboxes in Internet Explorer



gemsera
07-30-2008, 03:52 AM
Hi All,

Sorry to have to ask but after trawling the web for two days and mucking around with sendkeys, its still not doing what I want. My code is here: (bear in mind its ugly, but it works)




Sub SPIRtest()

redo:
MyLogin = "603889262"
MyToken = Application.InputBox("Please enter your ActivID Token Number", "Token", Default:="", Type:=1)
MyPass = "mypasshere"

Set ie = New InternetExplorer
ie.Visible = True
ie.Navigate "LINKGOESHERE"

Do Until ie.ReadyState = READYSTATE_COMPLETE
Loop
'Look for password Form by finding test "Password"
For Each ieForm In ie.Document.forms
If InStr(ieForm.innertext, "password") <> 0 Then
ULogin = True
'enter details
ieForm(1).Value = MyLogin
Application.Wait (Now + TimeValue("00:00:02"))
ieForm(2).Value = MyToken
Application.Wait (Now + TimeValue("00:00:02"))
ieForm(3).Value = ""
Application.Wait (Now + TimeValue("00:00:02"))
ieForm(4).Value = MyPass
Application.Wait (Now + TimeValue("00:00:02"))

DoEvents
Call SendKeys("{enter}") 'Hit enter on the "login" button



End If
Application.Wait (Now + TimeValue("00:00:05"))

ie.Navigate "next page-secondary login"
Application.Wait (Now + TimeValue("00:00:05"))

DoEvents
Call SendKeys(MyLogin) 'This is the login name box
DoEvents
Call SendKeys("{tab}") 'tab to password box
DoEvents
Call SendKeys(MyPass) 'Password box
DoEvents
Call SendKeys("{enter}") 'Hit enter on the "login" button



' goto Search Query
Application.Wait (Now + TimeValue("00:00:10"))
ie.Navigate "SEARCHQUERY HTML HERE"
Application.Wait (Now + TimeValue("00:00:05"))



' Goto Search Fields Page

Dim objIE As SHDocVw.InternetExplorer
Dim ieDoc As MSHTML.HTMLDocument
Dim btnSubmit As MSHTML.HTMLInputElement
Set ieDoc = ie.Document
Set btnExport = ieDoc.all.Item("export")
btnExport.Click

Application.Wait (Now + TimeValue("00:00:10"))
ie.Visible = True



Dim Doc As HTMLDocument
Set Doc = ie.Document
With Doc.getElementsByName("checkBoxList(Search)")
ieDoc.Item(0).Checked = True 'Entered
End With



Call SendKeys("{TAB 27}")
DoEvents
Call SendKeys("{ }")
Application.Wait (Now + TimeValue("00:00:01"))


DoEvents
Call SendKeys("{TAB 4}")
DoEvents
Call SendKeys("{ }")
Application.Wait (Now + TimeValue("00:00:01"))


DoEvents
Call SendKeys("{TAB}")
DoEvents
Call SendKeys("{ }")
Application.Wait (Now + TimeValue("00:00:01"))


DoEvents
Call SendKeys("{TAB 3}")
DoEvents
Call SendKeys("{ }")
Application.Wait (Now + TimeValue("00:00:01"))


DoEvents
Call SendKeys("{TAB 4}")
DoEvents
Call SendKeys("{ }")
Application.Wait (Now + TimeValue("00:00:01"))



Set ieDoc = ie.Document

Set btnExport = ieDoc.all.Item("Export")
btnExport.Click
Next




End



End Sub







I have to login to one page (with a random changing number), navigate to the next, login there, navigate to the search, hit export, select some checkboxes and then hit export again. I can get to the page where the checkboxes are, but cannot for the life of me select the right checkboxes to proceed any further.

Unfortunately I dont know the names of the checkboxes, as the page is encrypted and beyond my league in programming. any suggestions? :dunno

Thanking all in advance.

TomSchreiner
07-30-2008, 04:29 AM
ieDoc.Item(0).Checked = True 'Entered
should be
.Item(0).Checked = True 'Entered

Try that first.

gemsera
07-30-2008, 05:34 AM
Ok thanks Tom, I just tried that and I get Object Variable or With block variable not set. What does this mean? It is possible the name of the checkbox list is incorrect, as I cannot see the html behind it. Would this possibly cause this error?

TomSchreiner
07-30-2008, 05:43 AM
"It is possible the name of the checkbox list is incorrect"

I'd say it's likely. :)

Is ("checkBoxList(Search)") just a guess? :)

If you cannot see the source then that is a first for me. Have your tried to view the cource from the toolbar? Not the right click option but - View, Source.

gemsera
07-30-2008, 05:49 AM
Hi Tom,

Yes, unfortunately it is a guess. "Search" is a guess, but most of that code came from various internet forums and search queries. I have tried a few other names but im afraid the checkbox might be scripted on the server side and not the client side. I've tried saving the file and opened html that way, along with right click and the view menu to no avail. I've managed to save the file as a MHT (web archive) but despite my web knowledge it looks like gobbledegook.

Is there an alternate way to sendkeys that works similar, however perhaps more reliable? That may be the only way for it to work. Thanks again for your help Tom :)

TomSchreiner
07-30-2008, 06:04 AM
"...im afraid the checkbox might be scripted on the server side and not the client side."

That doesn't matter. Your browser is rendering the HTML.

Add a reference to MSForms and edit this snippet in your procedure. When you get the msgbox, paste into your next reply wrapped in HTML tags.

Change:Dim Doc As HTMLDocument
Set Doc = ie.Document
With Doc.getElementsByName("checkBoxList(Search)")
ieDoc.Item(0).Checked = True 'Entered
End With

To:
Dim Doc As HTMLDocument
Dim ClipBoard As New DataObject
Set Doc = ie.Document
ClipBoard.SetText Doc.Body.ParentElement.InnerHtml
ClipBoard.PutInClipboard
MsgBox "Paste into your reply now..."
With Doc.getElementsByName("checkBoxList(Search)")
ieDoc.Item(0).Checked = True 'Entered
End With

gemsera
07-30-2008, 07:55 AM
Woah thats pretty cool. All the HTML. I didnt even think to use VBA to pull the HTML out.

you are a genius.

I would happily post the html however it is an extremely security sensitive site. I assume now i take the "fields" part of:

<input type=checkbox value=LastModified name=fields> ???

the form name is exportform.

Thank you thank you thank you :)

TomSchreiner
07-30-2008, 08:18 AM
Dim Doc As HTMLDocument
Set Doc = ie.Document
Doc.All("fields").Checked = True 'Entered

Should do...

gemsera
07-30-2008, 08:42 AM
it didnt, but thats no fault of yours. I will keep working and post back some final code which may help someone in the future.

Thank you a million times over once again, and I only hope I'm good enough at VBA to oneday repay the favour to someone.

gemsera
07-31-2008, 02:53 AM
Solved -
Dim Doc As HTMLDocument
Set Doc = ie.Document
With Doc.getElementsByName("fields[]")
.Item(0).Checked = True 'Entered
.Item(1).Checked = True 'Entered
.Item(2).Checked = True 'Entered
.Item(3).Checked = True 'Entered
.Item(4).Checked = True 'Entered
.Item(5).Checked = False
.Item(6).Checked = True 'Entered
.Item(7).Checked = True 'Entered
.Item(8).Checked = True 'Entered
.Item(9).Checked = True 'Entered
.Item(10).Checked = True 'Entered

Turned out it was "fields[]". If anyone needs any help with doing something similar in the future, I'll be happy to give a hand please PM Me.

Once again, Thanks Tom for all your help.