VBA and Internet Explorer


J

Jamie

Hi guys

I have a simple bit of code which opens up Internet Explorer, navigates to a
web page, selects a value from a drop down menu (options are either "Yes" or
"No") and submits the form.

The code works fine if it is selecting "Yes" however if I change it to "No"
it dosn't work. Here is my code:

Sub photo_select()

strURL =
"http://corporate1.internal.standardlife.com/phoneline/Phnline.nsf/frmStaffUpdate"
Set objIE = CreateObject("InternetExplorer.Application")
objIE.Navigate strURL
objIE.Visible = False

While objIE.ReadyState <> 4
Application.Wait Now + TimeValue("00:00:01")
Wend

objIE.Document.all("kwdshowphoto").Value = "Yes"

Call objIE.Document.Forms(0).Submit

objIE.Quit

End Sub

Here is part of the source code of the internal website, which relates to
the drop down:

<table border="0" width="100%" cellspacing="0" cellpadding="0"><a
width="100%" border="0" cellspacing="0" cellpadding="0">
<tr valign="top"><td class="sectionHead" width="350"><a width="1%"><img
width="359" height="1" src="/icons/ecblank.gif" border="0" alt=""><br>
My location data / PhoneLine photo</td><td class="sectionHead" ><a
width="100%"><img width="1" height="1" src="/icons/ecblank.gif" border="0"
alt=""><br>
<div align="right"><a href="javascript:phonelineHelp();" class="globalNav"
title="launch phoneline help">Help</a> </div></td></tr>

<tr valign="top"><td width="350"><a width="1%"><img width="359" height="1"
src="/icons/ecblank.gif" border="0" alt=""><br>
</td><td ><a width="100%"><img width="1" height="1" src="/icons/ecblank.gif"
border="0" alt=""><br>
<br></td></tr>

<tr valign="top"><td class="labelText" width="350"><a width="1%"><img
width="359" height="1" src="/icons/ecblank.gif" border="0" alt=""><br>
Show my photo on Phoneline :</td><td class="dataText" ><a width="100%"><img
width="1" height="1" src="/icons/ecblank.gif" border="0" alt=""><br>
<font size="2">
<input name="%%Surrogate_kwdShowPhoto" type="hidden" value="1">
<select name="kwdShowPhoto">
<option>Yes
<option selected>No</select>
</font> </td></tr>

<tr valign="top"><td width="1%"><img width="359" height="1"
src="/icons/ecblank.gif" border="0" alt=""><br>
</td><td width="100%"><img width="1" height="1" src="/icons/ecblank.gif"
border="0" alt=""><br>
<br></td></tr>

Thanks in advance
Jamie
 
Ad

Advertisements

J

Joel

Can you see on the webpage if No is getting enter in the form?

Why arre you using CALL. Does it work without call?

from
Call objIE.Document.Forms(0).Submit
to
objIE.Document.Forms(0).Submit
 
J

Jamie

Hi Joel

I've changed the .visible to true so I can see it - and it dosn't change to
"No" therefore the problem is not with the submit statement, but the value
statement. I thought it was maybe because I had to use something like
..listindex rather than value?

Thanks for your time
Jamie
 
J

Joel

I don't think you have the correct object selected. The form is just using
the default value. Another method is if the URL is different between Yes
and No then just go to the predefined URL.
 
J

Jamie

Hi Joes

Thanks for the suggestion. I dont think the URL is different.
IS there a bit code I can run to get the names of all the objects on the page?

This is the code specific to the object:

Is it maybe %%Surrogate_kwdshowphoto?

Thanks
Jamie
 
J

Joel

Does the box show the ouble quotes around YES ? If you manually go to the
page and select No does it work? I thinking that Yes may be 1 and No may be
0.
 
Ad

Advertisements

J

Jamie

Hi mate

No the box dosn't show the quotes. If I click No it does work. I did try 0
and 1 but no luck.
I did get a workaround by doing objIE.document.all("kwdshowphoto").focus
then sendkeys "N"
but its still annoying :-(

Thanks for your time, appreciated
 
J

Joel

Send ky with "N" works. Then "N" should work with Value. Not "No"?

set obj = objIE.document.all("kwdshowphoto")
obj.focus
obj.value = "N"
 
T

Tim Williams

'selectedIndex is zero-based, so to select the first option use 0
objIE.document.getElementsByName("kwdshowphoto").selectedIndex = 0

Tim
 
Ad

Advertisements

J

Jie Wang [MSFT]

Hi Jamie,

I think Tim is right, to make sure a specific item is selected, you can set
the selectedIndex property.

If you don't want to use the index number which makes the code a little bit
harder to understand and maintain, you can also set the selected property
on the specific option object to make its value as "selected".

Here I have a complete HTML + Javascript sample showing how it's done:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Page</title>

<script language="javascript" type="text/javascript">
<!--

function setSelectedItem(val) {
cboOptions.options.namedItem(val).selected = true;
}

//-->
</script>

</head>
<body>
<p>
<select name="cboOptions">
<option id="optYes">Yes</option>
<option id="optNo">No</option>
<option id="optNeutral">Neutral</option>
</select>
</p>
<p>
<input name="btnYes" type="button"
onclick="setSelectedItem('optYes')" value="Yes" />
<input name="btnYes" type="button"
onclick="setSelectedItem('optNo')" value="No" />
<input name="btnYes" type="button"
onclick="setSelectedItem('optNeutral')" value="Neutral" />
</p>
</body>
</html>

The basic idea is that you can get all available options from the "select"
object via its "options" collection, and use the "namedItem" method to
retrieve the option by its id (need to be predefined of course), at last
you set the "selected" property of that option to true.

Hope this helps.

If you have any further questions, please let me know.

Best regards,
Jie Wang

Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business days is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top