Solved

VBA+IE9 FireEvent ("onchange") doesn't work

Posted on 2013-01-09
11
5,130 Views
Last Modified: 2013-01-10
Hi,

In earlier versions of Internet Explorer, I have been able to load a webpage, modify the value of a listbox and then fire an onchange-event, causing the webpage to update. Now, with IE9, I can change the listbox value but the webpage won't update.

The webpage: https://www.avanza.se/aktier/lista

The listbox "Lista", which display "Large Cap" as default, is the one I change.

The old code (should change Large Cap to Mid Cap):

Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
Private Declare PtrSafe Function apiShowWindow Lib "user32" Alias "ShowWindow" _
            (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long

Const SW_MAXIMIZE = 3
Const SW_SHOWNORMAL = 1
Const SW_SHOWMINIMIZED = 2

Sub LoadPage()
    Dim IE As Object 

    Set IE = CreateObject("InternetExplorer.Application")
    IE.Visible = True
    apiShowWindow IE.hwnd, SW_SHOWNORMAL 
    Sleep lng_Sleep
    IE.Navigate "https://www.avanza.se/aktier/lista"
    
    Sleep 5000

    Do
        DoEvents
        Sleep 500
    Loop While IE.readyState <> READYSTATE_COMPLETE

    IE.Document.getElementById("list").Focus
    IE.Document.getElementById("list").selectedIndex = 1
    IE.Document.getElementById("list").FireEvent ("onchange")
End Sub

Open in new window


The listbox I change:

	        			<select name="listKey" id="list" class="list">
	        				
	        					<option value="large_cap_se" >Large Cap</option>
	        				
	        					<option value="mid_cap_se" >Mid Cap</option>
	        				
	        					<option value="small_cap_se" >Small Cap</option>
	        				
	        					<option value="firstnorth_se" >First North</option>
	        				
	        					<option value="aktietorget" >AktieTorget</option>
	        				
	        					<option value="ngm_equity" >NGM, Equity</option>
	        				
	        					<option value="ngm_mtf" >NGM, MTF</option>
	        				
	        					<option value="inofficiella" >Inofficiella</option>
	        				
	        					<option value="xterna_listan" >Xterna listan</option>
	        				
	        			</select>

Open in new window

0
Comment
Question by:Sandstedt
  • 6
  • 5
11 Comments
 
LVL 33

Expert Comment

by:Norie
ID: 38760653
The listbox doesn't appear to have an onchange event.
0
 

Author Comment

by:Sandstedt
ID: 38760682
You are right, but for some reason, calling the onchange event after the focus event and the value change, worked.

I will double check this just to be sure.

EDIT: confirmed. Focus, value change and onchange result in the webpage updating.
0
 
LVL 33

Expert Comment

by:Norie
ID: 38761420
So the code works?
0
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 

Author Comment

by:Sandstedt
ID: 38762049
Yes, but only on Internet Explorer 8, which means that I have to use another computer (not possible to downgrade on my work computer). Is it possible that they removed this functionality in IE9?
0
 
LVL 33

Expert Comment

by:Norie
ID: 38763673
I woudn't think they would remove the functionality, but they may have changed it, or the way it works.

Perhaps it's been replaced with something else.

By the way, you've missed a declaration for READYSTATE_COMPLETE - when I first ran the code it  got into an endless loop.:)
0
 

Author Comment

by:Sandstedt
ID: 38763740
Oops, sorry about that, did some cleaning of the code before I posted it here.

Maybe I should just accept defeat. There seems to be little, if any, information about this on the net (from what I can find). Problem is, IE8 doesn't work on Windows Vista, and IE7 is too old for the webpage, so I right now I have to use Windows 7 + IE9 in order for it to work.

If anyone knows how to get the data I need (large-, mid- and smallcap stock information) in a different way, I'm all ears.
0
 
LVL 33

Accepted Solution

by:
Norie earned 500 total points
ID: 38763755
Here's how you can do it - apparently post-IE8 you need to use dispatchEvent.
Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
Private Declare PtrSafe Function apiShowWindow Lib "user32" Alias "ShowWindow" _
            (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long

Const SW_MAXIMIZE = 3
Const SW_SHOWNORMAL = 1
Const SW_SHOWMINIMIZED = 2
Const READYSTATE_COMPLETE = 4

Sub LoadPage()
    Dim IE As Object
    Dim lst As Object
    Dim evt As Object
    
    Set IE = CreateObject("InternetExplorer.Application")
    IE.Visible = True
    apiShowWindow IE.hwnd, SW_SHOWNORMAL
    Sleep lng_Sleep
    IE.Navigate "https://www.avanza.se/aktier/lista"
    
    Sleep 5000

    Do
        DoEvents
        Sleep 500
    Loop While IE.readyState <> READYSTATE_COMPLETE
    
    Set evt = IE.document.createEvent("HTMLEvents")

    evt.initEvent "change", True, False

    Set lst = IE.document.getElementById("list")
    
    lst.selectedIndex = 1

    lst.dispatchEvent evt
    
End Sub

Open in new window

0
 

Author Comment

by:Sandstedt
ID: 38763858
On this:
Set evt = IE.document.createEvent("HTMLEvents")

Open in new window


I get the error "Run-time error 438: Object doesn't support this property or method"
0
 
LVL 33

Expert Comment

by:Norie
ID: 38763865
That code works just fine for me in IE9.

Which version of IE are you running the code in when you get the error?
0
 

Author Comment

by:Sandstedt
ID: 38763945
You got me, I was using IE8. Tried it in IE9 now and it works. Thank you so much, really appreciate it! You saved me a whole lot of work.
0
 
LVL 33

Expert Comment

by:Norie
ID: 38763996
No problem.

Actually learned something myself.
0

Featured Post

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question