Share via

Javascript .Selected = true doesn't work properly with VBA webbrowser control

Anonymous
2017-03-29T22:59:33+00:00

Hi,

I am using VBA(Excel) webbroswer (InternetExplorer) control to open a webpage with javascript code like ".Selected = true " which doesn't work properly.  It works with C# webbrowser control and any browser, like IE, edge, Chrome and Firefox but VBA browser control.

Here is my html page source code (not the real page I am accessing but having the same issue). The problem is caused by "s[i].selected = s[i].value==lang;"

=======================

<html>

<body>

<h1>Test Language</h1>

<SELECT tabIndex=0 id=slang title="Select a Language:" class=selector name=currentLanguage message="FND_SSO_LOGIN_LANG_SELECT" onchange="changeVal()">

<OPTION lang=pt title="Brazilian Portuguese" value=PTB>Português (Brasil)</OPTION>

<OPTION lang=en title="Simple Chinese" value=CN>Chinese</OPTION>

<OPTION lang=en title="American English" value=US>English</OPTION>

</SELECT>

<script>

function changeVal(){

s=document.getElementById('slang');

lang = s.value

for (var i = 0;s && i < s.length;i++) { 

s[i].selected = s[i].value==lang;

}

}

</script>

</body>

</html>

============================

The function walks through the whole list of this listbox and set the 'Selected' property to true if the value is equal to the current selected value.

With the VBA webbrowser control, it will set every item in the list to true and finally the last item in the list will be selected. For example, when I change the value from 'PTB' to 'CN', it will trigger the onChange event and finally set the 'EN' as selected as 'EN' is the last item in the list.

My VBA code is very simple and :

==========================

'Public WithEvents objIE As WebBrowser         'Tried this one as well. Didn't work either

Public WithEvents objIE As InternetExplorer

Set objIE = Me.Controls.Add("Shell.Explorer.2")  'Me is a userform to embed the webbrowser control

objIE.Navigate sLoginURL

==========================

The limitation here is:

  1. The page I need to navigate to is a thirty-party page and I can't change the code of that page.
  2. It works if I open the page in the VBA code using external browser rather than an embedded browser (using "Set objIE = CreateObject("InternetExplorer.Application")" rather than using "dim objIE As InternetExplorer") . However the real page I am accessing is a login page and I need to keep the login session for further operations.

Did anyone have the same issue as mine?

I think this is an issue of webbrowser control and it's hard or maybe impossible to fix from VBA code. I am also looking for the replacement of webbrowser control. Does anyone know any other solution of webbrowser control replacement?

I have been stuck for a long time. I would appreciate if someone can help.

Thanks,

Lily

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

2 answers

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  2. Anonymous
    2017-07-24T23:09:21+00:00

    We demand answers.

    Was this answer helpful?

    0 comments No comments