Enter data to web page with VBA

hstaubyn

Board Regular
Joined
Sep 13, 2010
Messages
93
Hi,

I am trying to fill a text box on a web page with discouraging results.

When I try to trun the code below, I get a Run time error '91' - "Object Variable or With Block Variable not set" and when I click 'Debug', the last line of code (with "Hello" in it) is highlighted.

Code:
 Sub Fill_Website_Textbox()
Dim objIE As SHDocVw.InternetExplorer
Dim OrgBox As HTMLInputElement
 
Set objIE = New SHDocVw.InternetExplorer
objIE.Navigate "My Website"
    objIE.Visible = True
    WaitReady 'Sub to wait while internet loads
 
Set OrgBox = objIE.Document.getElementById("ctl00_MainContent_TabContainer_ProjectInformationPanel_ProjectListView_ctrl0_websiteTextBox")
[COLOR=red]OrgBox.Value = "Hello"[/COLOR]
 
End Sub

This is not the complete code, just the important bit. The website loads fine, but when it gets to the entering data part, the error crops up. Can anyone tell me where I'm going wrong?

Thanks,
H
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
What exactly does your WaitReady sub do?

You don't seem to be passing the new instance of Internet Explorer to it so how can it check that the page is fully loaded in that instance and that IE itself is ready.
 
Upvote 0
Ok, the waitready was a distraction, here is the code without it:

Code:
Sub Fill_Website_Textbox()
Dim objIE As SHDocVw.InternetExplorer
Dim OrgBox As HTMLInputElement

Set objIE = New SHDocVw.InternetExplorer
objIE.Navigate "My Website"
    objIE.Visible = True
    Do While objIE.ReadyState < 4: Loop
 
Set OrgBox = objIE.Document.getElementById("ctl00_MainContent_TabContainer_ProjectInformationPanel_ProjectListView_ctrl0_websiteTextBox")
OrgBox.Value = "Hello"

End Sub
 
Upvote 0
You aren't waiting for the page to fully loaded, which is quite important when working with ASP pages.

I'm assuming it is an ASP page.

This is what I've used in the past and it's worked.
Code:
  Do While doc.ReadyState <> "complete": DoEvents: Loop
doc is a variable referencing the document object.

You can also check and wait if IE is in a 'busy' state.
Code:
Do While IE.Busy: DoEvents: Loop
All of these might not be needed some time but they aren't doing any harm.

They might slow the code a little if you use them everywhere but it's a simple task to eliminate the unneeded code later, once the rest of the code is working.

Note, I'm not saying using these will definitely work - I would need more information, specifically the URL, to check further.

Give them a try though.:)
 
Upvote 0
Thanks, those suggestion work fine.

Do you have any idea about the entering data part of my question?

The code on the web page is as follows:

<input name="ctl00$MainContent$TabContainer$ProjectInformationPanel$ProjectListView$ctrl0$OrganizationTextBoxAO" type="text" id="ctl00_MainContent_TabContainer_ProjectInformationPanel_ProjectListView_ctrl0_OrganizationTextBoxAO" class="EntryTextBox" /></div>

Hang on, the previous code I posted should have said:

Code:
Sub Fill_Website_Textbox()
Dim objIE As SHDocVw.InternetExplorer
Dim OrgBox As HTMLInputElement

Set objIE = New SHDocVw.InternetExplorer
objIE.Navigate "My Website"
    objIE.Visible = True
    Do While objIE.ReadyState < 4: Loop

Set OrgBox = objIE.Document.getElementById("[COLOR=#0000ff]ctl00_MainContent_TabContainer_ProjectInformationPanel_ProjectListView_ctrl0_OrganizationTextBoxAO[/COLOR]")
OrgBox.Value = "Hello"

End Sub

Just tried it and it works! I do have a follow up question though.......
 
Upvote 0
Ok,

the website is set up in a way that you start typing the name in the text box and a list of options drops down depending on what you type. You must then click on one of the options for it to be inserted in the text box. It only accepts entries in the list.

I can fill the text box with whatever I like, but it does not accept what I enter through VBA into the textbox in the same way as if I had manually typed it.

The full code for the text box is shown below:

<td><divid="SelectedContainerAO" class="SelectedContainer" *******="document.getElementById('ctl00_MainContent_TabContainer_ProjectInformationPanel_ProjectListView_ctrl0_OrganizationTextBoxAO').focus()"><divclass="selectorheader"><divstyle="float:left;"><spanid="SelectedCountSpanAO">0 selected</< font>span> | <ahref="javascript:removeallAO();">Clear all</< font>a> | <ahref="javascript:SelectSavedEntriesAO('SelectedEntriesContainerAO');">Revert to saved</< font>a></< font>div><divstyle="float:right;font-weight:normal;color:Gray;font-style:italic">Start typing the organization name</< font>div><divstyle="clear:both"></< font>div></< font>div><divid="SelectedEntriesContainerAO"></< font>div><inputname="ctl00$MainContent$TabContainer$ProjectInformationPanel$ProjectListView$ctrl0$OrganizationTextBoxAO" type="text" id="ctl00_MainContent_TabContainer_ProjectInformationPanel_ProjectListView_ctrl0_OrganizationTextBoxAO" class="EntryTextBox" /></< font>div>

If someone could tell me how to have it accept what is input via VBA (it will always be something from the websites own internal list), that would be amazing.

(btw It has to be done through the web page for various reasons)
 
Upvote 0
Do you know the exact value you want to enter?

Is it on the list of valid entries?

How is it not 'accepting' what you enter?

The code you've posted might not be relevant for the behaviour you describe but it will be connected.

This sort of thing is really hard to help without a URL, even that might not help here because it looks like there's a lot going on server side.
 
Upvote 0
Norie,

1. Yes, I know the exact value
2. Yes, it's on the list
3. Entering it manually, a drop down menu appears as you type and you have to click the required text option. It then 'nestles in' with a grey background. Entering it with VBA leaves it sitting there 'un-nestled'

Sorry, not very descriptive and I think this will be too hard to achieve.

What I can achieve though, is getting the text to sit in there without 'nestling'. The nestle bit can be achieved manually.

The problem I am having now is that if the text being entered is above a certain length, I get a run time error '91' (and also sometimes when the string is short). Something funny happens though when I add in a message box in here:
Code:
Sub Fill_Website_Textbox()
Dim objIE As SHDocVw.InternetExplorer
Dim OrgBox As HTMLInputElement
 
Set objIE = New SHDocVw.InternetExplorer
objIE.Navigate "My Website"
    objIE.Visible = True
    Do While objIE.ReadyState < 4: Loop
 
[COLOR=red]Msgbox "Continue"[/COLOR]
    Set OrgBox = objIE.Document.getElementById[COLOR=black]("ctl00_MainContent_TabContainer_ProjectInformationPanel_ProjectListView_ctrl0_OrganizationTextBoxAO[/COLOR]")
    OrgBox.Value = "Hello"
 
End Sub
it works whatever the length of string. Is there a reason for this and if so can the problem be overcome?
 
Upvote 0
So you aren't using any of the code I suggested to check and wait if the page is fully loaded and IE is ready?

As far as I can see that's what's missing, showing the messagebox has sort of caused the code to wait.

While the code is waiting the page/IE are loading etc.
 
Upvote 0
Appologies, that was copied and pasted from my old post.

I am using your code, but it seems to be making little difference...

I'm sure it is something to do with the loading of the web page, but can't seem to get it to work.
 
Upvote 0

Forum statistics

Threads
1,214,975
Messages
6,122,538
Members
449,088
Latest member
RandomExceller01

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top