Using VBA to insert a value to a web page (1 Viewer)

Alc

Registered User.
Local time
Today, 19:18
Joined
Mar 23, 2007
Messages
2,407
Via research on this site and using Google, I've put together a Sub procedure to open the web page for tracking Purolator parcels. However, I'm not having much luck in working out how to input the parcel number to be tracked.

By commenting out various lines, I've found that all other lines work (including the line to submit the request after a parcel number is entered) but I can't work out how to get the line in bold red font to work.

I know this is at least partly because of my complete lack of familiarity with HTML I'm hoping someone on this site who isn't totally ignorant in this area might be able to point me in the right direction.

The code as it stands is this:
Code:
Private Sub CARRIER_TRACKING_NO_DblClick(Cancel As Integer)
    Dim ie As Object
    Dim strTrackingNo As String
    
    On Error GoTo Err_Point
    
    If Len(Replace(Nz(CARRIER_TRACKING_NO, ""), " ", "")) = 0 Then
        Exit Sub
    Else
        strTrackingNo = CARRIER_TRACKING_NO
    End If
        
    Set ie = CreateObject("InternetExplorer.Application")
    ie.Navigate "[URL]https://www.purolator.com/en/ship-track/tracking-summary.page[/URL]?"
    ie.Visible = True
    While ie.Busy
        DoEvents
    Wend
    
  [COLOR=red]  [B]ie.Document.all("Search").Value = strTrackingNo[/B][/COLOR]
    
    ie.Document.getElementById("buttonTrackSearch").Click
    
    Exit Sub
    
Err_Point:
    
    strMsg = Err.Number & Chr(13) & Err.Description
    strTitle = "Error during translation"
    strResponse = MsgBox(strMsg, vbCritical, strTitle)
End Sub
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 07:18
Joined
May 7, 2009
Messages
19,169
Did you try to check if "search" is a tagname or id. Right ckick on the web page and view source.
 

Alc

Registered User.
Local time
Today, 19:18
Joined
Mar 23, 2007
Messages
2,407
Thanks for the reply.

I did, yes. When I selected 'Inspect Element' for that field the following code was highlighted
Code:
<textarea name="search" class="$" id="search" maxlength-"3000" placeholder="Enter p to 75 Tracking Numbers / PINs or References" rows="4" cols="40"></textarea>
I used the same method to find the name of the button buttonTrackSearch, so I thought it was correct in this case as well.

The error message I get at that line is
Run-time error '438':
Object doesn't support this property or method
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 07:18
Joined
May 7, 2009
Messages
19,169
Try this

ie.Document.getElementById("Search").SetAttribute("value", strTrackingNo)
 

Alc

Registered User.
Local time
Today, 19:18
Joined
Mar 23, 2007
Messages
2,407
Try this

ie.Document.getElementById("Search").SetAttribute("value", strTrackingNo)
Unfortunately Access highlights the line in red immediately after I finish typing.
 

Alc

Registered User.
Local time
Today, 19:18
Joined
Mar 23, 2007
Messages
2,407
Maybe I'm over simplifying this but what is the website link you end up on after a successful tracking inquiry?

I only say it because we use UPS and you can go straight to the tracking page by using a format like

https://wwwapps.ups.com/WebTracking/track?track=yes&trackNums=1Z51A7306645550534&loc=en_uk

Where the tracking number is (rather obviously) 1Z51A7306645550534
HI Minty. I did think of that, since I've built a similar function for translating French to English using Google Translate. In that case, it was as simple as you describe, so I hoped it would be the same here.

However, if I use parcel number 331287667017 the link in this case goes from
https://www.purolator.com/en/ship-track/tracking-summary.page?
to
https://www.purolator.com/purolator...B%2BLCAAAAAAAAAAzNjY0sjA3MzM3MDQHAGPzxd4MAAAA
and I'm damned if I can work out the formula being used to convert the one to the other.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 07:18
Joined
May 7, 2009
Messages
19,169
Use

Call ie.document...
 

Alc

Registered User.
Local time
Today, 19:18
Joined
Mar 23, 2007
Messages
2,407
Playing with the relevant field, I've found that if I enter a parcel number and then inspect the element before clicking on the <track> button, the properties for the element change from
Code:
<textarea name="search" class="$" id="search" maxlength-"3000" placeholder="Enter p to 75 Tracking Numbers / PINs or References" rows="4" cols="40"></textarea>
to
Code:
<textarea name="search" class="$" id="search" maxlength-"3000" placeholder="Enter p to 75 Tracking Numbers / PINs or References" rows="4" cols="40">331297146465</textarea>
If I can figure out a way to add the number to just before the closing </textarea> I think it will work.

Now, I 'just' need to find how to do that. :banghead:
 

jleach

Registered User.
Local time
Today, 19:18
Joined
Jan 4, 2012
Messages
308
Ugly stuff: I've done more if it than I care to really. If you happen to access to jQuery on the site there's an ExecuteScript (or maybe just Exec?) which would make it pretty easy:

(aircode)
Code:
ie.Document.ExecuteScript("$('#search').text(YourTrackingNumber);")

I don't work with vanilla javascript quite enough to know the syntax for doing the same offhand, but if you could always fall back to string manipulations as a last resort (using instr and recording positions of token values to narrow your way down to honing in on the position of the last > and the first </ of the textarea, then you can grab what's in the middle and replace it).
 

Alc

Registered User.
Local time
Today, 19:18
Joined
Mar 23, 2007
Messages
2,407
Solved! Thanks for the help, guys.

Forgive my knowledge of correct HTML terms, but the text field into which I was trying to input a numbers act as a list and it was necessary to specify into which part of the list the value should be placed.

The following code works. Hopefully, it'll help somebody else.

Just to clarify, CARRIER_TRACKING_NO is a text field on a form and displays a Purolator tracking number (where one exists).
Code:
Private Sub CARRIER_TRACKING_NO_DblClick(Cancel As Integer)
    Dim ie As Object
    Dim strTrackingNo As String
    
    On Error GoTo Err_Point
    
    If Len(Replace(Nz(CARRIER_TRACKING_NO, ""), " ", "")) = 0 Then
        Exit Sub
    Else
        strTrackingNo = CARRIER_TRACKING_NO
    End If
        
    Set ie = CreateObject("InternetExplorer.Application")
    ie.Navigate "[URL]https://www.purolator.com/en/ship-track/tracking-summary.page[/URL]?"
    ie.Visible = True
    While ie.Busy
        DoEvents
    Wend
    
    ie.Document.all("search")(1).Value = strTrackingNo
    
    ie.Document.getElementById("buttonTrackSearch").Click
    
    Exit Sub
    
Err_Point:
    
    strMsg = Err.Number & Chr(13) & Err.Description
    strTitle = "Error during translation"
    strResponse = MsgBox(strMsg, vbCritical, strTitle)
End Sub
 

Users who are viewing this thread

Top Bottom