Solved

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

Posted on 2013-01-09
11
4,826 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
 

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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

759 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now