ABN Lookup VBA Script

Mitchellstokes

New Member
Joined
Nov 2, 2014
Messages
4
Hey Guys,

I've been running a VBA script from Microsoft Access that looks up an ABN number from the ABN lookup website.

It appears that an upgrade to ie10 may have ruined the script. It fails at "Set contentmajor=ie.Document.getelementbyid("content-major")" with error: "Run-time error '-2147467259 (80004005)': Method 'Document' of object 'iWebBrowser2' failed.

Code:
Option Compare Database
Private Sub CheckABRButton_Click()
Dim ie As Object
Dim fromStart, toStart, readLength, contentMajorStart As Variant
Dim abnDetails As Object
Dim contentMajor As Object
Dim searchNumber, entityName, entityType, ACN, businessLocation, status As String
If Len(f_abrABNACN) > 0 Then
    searchNumber = Replace(f_abrABNACN, " ", "")
Else
    searchNumber = ""
End If
If Len(searchNumber) = 11 Or Len(searchNumber) = 9 Then
    
    Set ie = CreateObject("internetexplorer.application")
    
    ie.Visible = False
    ie.navigate "[url=http://abr.business.gov.au/SearchByAbn.aspx?SearchText]ABN Lookup -[/url]=" & searchNumber
    
    While ie.busy
        DoEvents
    Wend
    Set contentMajor = ie.Document.getelementbyid("content-major")
    'MsgBox contentMajor.innerHTML
    If contentMajor Is Nothing Then
        MsgBox "Request to ABR failed. There might be too much network traffic at this time. Please try again."
        ie.Quit
        Exit Sub
    Else
        contentMajorStart = InStr(contentMajor.innerhtml, "process-error")
    End If
    
    If contentMajorStart > 0 Then
        f_abrEntityName = ""
        f_abrEntityType = ""
        f_abrACN = ""
        f_abrPostCode = ""
        f_abrStatus = ""
        CopyDetailsButton.Visible = False
        MsgBox "The details you provided couldn't be found in the ABR. Please re-check them and try again."
    Else
        Set abnDetails = ie.Document.getelementbyid("abn-details")
        
        If abnDetails Is Nothing Then
            MsgBox "Request to ABR failed. There might be too much network traffic at this time."
            ie.Quit
            Exit Sub
        End If
        
        ' Get entityName - was 40 shift, depends on version of IE!
        fromStart = InStr(abnDetails.innerhtml, "Entity name:") + 23
        toStart = InStr(fromStart, abnDetails.innerhtml, "</td>")
        readLength = toStart - fromStart
        entityName = Trim(Mid(abnDetails.innerhtml, fromStart, readLength))
        f_abrEntityName = entityName
        
        ' Get entityType
        fromStart = InStr(abnDetails.innerhtml, "href=""/EntityTypeDescription.aspx?Id=")
        If fromStart <= 0 Then
            fromStart = InStr(abnDetails.innerhtml, "Entity Type:") + 43
            toStart = InStr(fromStart, abnDetails.innerhtml, "</td>")
        Else
            fromStart = fromStart + 44
            toStart = InStr(fromStart, abnDetails.innerhtml, "</a>")
        End If
        readLength = toStart - fromStart
        entityType = Trim(Mid(abnDetails.innerhtml, fromStart, readLength))
        f_abrEntityType = entityType
        
        ' Get ACN
        If entityType = "Australian Private Company" Then
            fromStart = InStr(abnDetails.innerhtml, "searchType=OrgAndBusNm") + 38
            ACN = Trim(Mid(abnDetails.innerhtml, fromStart, 9))
            f_abrACN.Visible = True
            f_abrACN = ACN
        Else
            f_abrACN = ""
            f_abrACN.Visible = False
        End If
        
        'Get Business Location - was 40 shift, depends on version of IE!
        fromStart = InStr(abnDetails.innerhtml, "location:") + 23
        toStart = InStr(fromStart, abnDetails.innerhtml, "</td>")
        readLength = toStart - fromStart
        businessLocation = Trim(Mid(abnDetails.innerhtml, fromStart, readLength))
        'f_abrPostCode = Split(businessLocation, " ")(1)
        f_abrPostCode = businessLocation
        
        'Get Status - was 35 shift, depends on version of IE!
        fromStart = InStr(abnDetails.innerhtml, "status:") + 18
        toStart = InStr(fromStart, abnDetails.innerhtml, "</td>")
        readLength = toStart - fromStart
        status = Trim(Mid(abnDetails.innerhtml, fromStart, readLength))
        f_abrStatus = Split(status, " ")(0)
        
        CopyDetailsButton.Visible = True
        
    End If
    
    ie.Quit
Else
    MsgBox "You need to enter a valid ABN or ACN to continue. ABNs are 11 digits, ACNs are 9 digits."
End If
End Sub

I've had a look online but I'm not that familiar with this type of code.
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
What's an example ABN number to search for? I'm not sure what that "ie.navigate..." line is doing- is it doing a google search, or other type of internet search, or somehow going straight to the "ABN lookup website", whatever that is? Not sure how it would be doing that...

I just noticed you said this script was from microsoft acccess- but you posted in the Excel questions category- did you copy the code into a module in an excel workbook, or are you running it in access?
 
Last edited:
Upvote 0
For what it's worth, when I run it in an Excel VBA module using IE 11 (only copied the code up to the line you got the error), it seems to run just fine. Are you able to upgrade to IE 11?
 
Upvote 0
Unfortunately i'm carrying this operation out for my employer and as such, their programs are controlled by IT and they wont update IE until they do it for EVERYONE. Any work around? It used to work?
 
Upvote 0
I'd do it like this and remove the reliance on IE

Code:
Public Function ABNData(ABNid As String) As Collection


    Dim request As Object
    Dim dom As Object
    Dim tr As Object
    Dim searchTerms, searchTerm
    Dim i As Long
    Dim data As New Collection
    
    Const url As String = "http://abr.business.gov.au/SearchByAbn.aspx?abn="
    
    
    searchTerms = Array("Entity name", "ABN status", "Entity type", "Goods & Services Tax (GST)", "Main business location")
    
    Set request = CreateObject("WinHttp.Winhttprequest.5.1")
    Set dom = CreateObject("htmlFile")
    
    On Error GoTo errhandler:
    
        With request
            .Open "GET", url & ABNid, False
            .send
            If request.Status = 200 Then
                If InStr(1, request.responsetext, "Problem searching ABN Lookup") = 0 Then
                    dom.write request.responsetext
                    For Each tr In dom.getelementsbytagname("table")(0).Rows
                        For Each searchTerm In searchTerms
                            If InStr(1, tr.Cells(0).innerText, searchTerm) > 0 Then
                                data.Add tr.Cells(1).innerText, searchTerm
                                Exit For
                            End If
                        Next searchTerm
                    Next tr
                Else
                    Err.Raise 11112, Description:="The ABN Number does not exist in the database"
                End If
            Else
                Err.Raise 11111, Description:="The website could not be reached or has errored"
            End If
        End With
    
    Set ABNData = data
    
    Exit Function
    
errhandler:
        MsgBox "Error # " & Err.Number & vbCrLf & Err.Description


End Function
 
Upvote 0
Kyle thanks so much for that code. I semi-understand how it works, however, I'm not sure how to incorporate it into my current Microsoft Access processes. Do I put that code into its own module, have a button click call the function and insert Forms![AddNewApplication].f_abrEntityName= EntityName ?
 
Upvote 0

Forum statistics

Threads
1,217,376
Messages
6,136,198
Members
449,999
Latest member
sammokhan

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top