Change .getElementsByClassName with code (1 Viewer)

selvsagt

Registered User.
Local time
Today, 14:58
Joined
Jun 29, 2006
Messages
99
Hi.

I use a function to look up a currency value from a webpage to do some realtime calculation. It needs to be realtime, and this restricts possible sources online.

I have a function that works alright, but it has some flaws I would like to address.

The class changes names depending on if the last change is a positive or negative.

The code I use today is at the bottom.

For "no change" it has the name "ftqbb ftqw2 pid-59-bid" This is the one I use.
for positive change it has the name "ftqbb ftqw2 pid-59-bid greenBg" No result for me.

for negative change it has the name "ftqbb ftqw2 pid-59-bid redBg" no result for me.

Of these three classes, only ONE exists, and I would like my code to choose the one that actually works.. How may I build up an IF or Case statement here?

Code:
Private Sub cmdValuta_Click()
On Error GoTo Err_cmdValuta

Me.txtLys = "X"
DoCmd.Hourglass True

    Dim IE As Object
    'Lag InternetExplorer Object
    Set IE = CreateObject("InternetExplorer.Application")
    
    IE.Visible = False
    IE.Navigate "http://fxrates.forexpros.com/index.php?pairs_ids=59"
    Do While IE.Busy
    Loop

    'Time for code to loop to "find" the class.
    Dim dd As String
    Dim oNow As Date
    oNow = DateAdd("s", 5, Now())

    Do
    DoEvents
    Loop Until Now() > oNow Or Not IE.Document.getElementsByClassName("ftqbb ftqw2 pid-59-bid")(0) Is Nothing

    dd = IE.Document.getElementsByClassName("ftqbb ftqw2 pid-59-bid")(0).innerText

    Me.txtValuta.Value = dd
    Me.txtLys.Value = "V"
    IE.Quit
    Set IE = Nothing
    
    DoCmd.Hourglass False
    
    Exit Sub

Err_cmdValuta:    MsgBox Err & " " & Error$
Exit Sub


End Sub
 

obeylele

Registered User.
Local time
Today, 16:58
Joined
Jul 29, 2005
Messages
28
Try this:

Code:
Do


	if Not IsNull(IE.Document.getElementsByClassName("ftqbb ftqw2 pid-59-bid")(0)) then
   		dd = IE.Document.getElementsByClassName("ftqbb ftqw2 pid-59-bid")(0).innerText
		exit Do
	elseif Not IsNull(IE.Document.getElementsByClassName("ftqbb ftqw2 pid-59-bid greenBg")(0) ) then
   		dd = IE.Document.getElementsByClassName("ftqbb ftqw2 pid-59-bid greenBg")(0).innerText
		exit Do
	elseif Not IsNull(IE.Document.getElementsByClassName("ftqbb ftqw2 pid-59-bid redBg")(0) ) then
   		dd = IE.Document.getElementsByClassName("ftqbb ftqw2 pid-59-bid redBg")(0).innerText
		exit Do
	endif

Loop Until Now() > oNow
 

selvsagt

Registered User.
Local time
Today, 14:58
Joined
Jun 29, 2006
Messages
99
Thank you!
The code works, but in the same way as before. If the currency has no change it works, if its the green or red, it gives me an error.

I get an error on the first statement when this is the case.
Run-time 91 "object variable or block variable not set.

Perhaps it is not an "is null" but something like "Not exists" or something like that?

Code:
dd = IE.Document.getElementsByClassName("ftqbb ftqw2 pid-59-bid")(0).innerText
 

obeylele

Registered User.
Local time
Today, 16:58
Joined
Jul 29, 2005
Messages
28
Try
Code:
Do

	if Not (IE.Document.getElementsByClassName("ftqbb ftqw2 pid-59-bid")(0)) Nothing then
   		dd = IE.Document.getElementsByClassName("ftqbb ftqw2 pid-59-bid")(0).innerText
		exit Do
	elseif Not (IE.Document.getElementsByClassName("ftqbb ftqw2 pid-59-bid greenBg")(0) ) Nothing then
   		dd = IE.Document.getElementsByClassName("ftqbb ftqw2 pid-59-bid greenBg")(0).innerText
		exit Do
	elseif Not IsNull(IE.Document.getElementsByClassName("ftqbb ftqw2 pid-59-bid redBg")(0) ) Nothing then
   		dd = IE.Document.getElementsByClassName("ftqbb ftqw2 pid-59-bid redBg")(0).innerText
		exit Do
	endif

Loop Until Now() > oNow
 

selvsagt

Registered User.
Local time
Today, 14:58
Joined
Jun 29, 2006
Messages
99
Excellent!
I had to add "is nothing then", and it now works like a charm!
Thank your so much, and I wish you a great evening!

Code:
Do

    If Not (IE.Document.getElementsByClassName("ftqbb ftqw2 pid-59-bid")(0)) Is Nothing Then
        dd = IE.Document.getElementsByClassName("ftqbb ftqw2 pid-59-bid")(0).innerText
        Exit Do
    ElseIf Not (IE.Document.getElementsByClassName("ftqbb ftqw2 pid-59-bid greenBg")(0)) Is Nothing Then
        dd = IE.Document.getElementsByClassName("ftqbb ftqw2 pid-59-bid greenBg")(0).innerText
        Exit Do
    ElseIf Not (IE.Document.getElementsByClassName("ftqbb ftqw2 pid-59-bid redBg")(0)) Is Nothing Then
        dd = IE.Document.getElementsByClassName("ftqbb ftqw2 pid-59-bid redBg")(0).innerText
        Exit Do
    End If

Loop Until Now() > oNow
 

Users who are viewing this thread

Top Bottom