[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 6597
  • Last Modified:

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

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
Sandstedt
Asked:
Sandstedt
  • 6
  • 5
1 Solution
 
NorieCommented:
The listbox doesn't appear to have an onchange event.
0
 
SandstedtAuthor Commented:
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
 
NorieCommented:
So the code works?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
SandstedtAuthor Commented:
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
 
NorieCommented:
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
 
SandstedtAuthor Commented:
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
 
NorieCommented:
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
 
SandstedtAuthor Commented:
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
 
NorieCommented:
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
 
SandstedtAuthor Commented:
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
 
NorieCommented:
No problem.

Actually learned something myself.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now