VBA - Error Handling for missing reference

GaryStone10

Board Regular
Joined
Nov 18, 2008
Messages
100
Hi All

I'm hoping this is possible. I have a report that loads up scanned documents from another application. This is done by using a reference to the other application and works fine. However, there will be people that don't have the other application and therefore not have the reference, so I wanted to add some error handling to give them a specific error. This is my code.

Code:
Sub Open_Ados(reporttarget As Range, Optional region As Boolean)
'This sub is used to open ADOS Viewer (if possible) to load up the document relating to the data clicked on.
'This only works if the user has ADOS access and ADOS installed on the desktop, otherwise it takes you to the
'error message

On Error GoTo adoserr
Dim ados_server As New ADOSServer
Dim ados_document As New ADOSDocument
Dim docnorow As Integer
Dim docno As Long
Dim claimdata As Worksheet

Unprotect_sheets

'set the sheet used
If region = True Then
    Set claimdata = Sheets("Region Claim Data")
Else
    Set claimdata = Sheets("Claim Data")
End If

'set the server and connect to it
Set ados_server = CreateObject("TWADOSObjects.ADOSServer")
ados_server.Connect 1

'find the corresponding row to the cell clicked on and the docno from that row
If reporttarget.Column < 11 Then
    docnorow = (reporttarget.Row - 112) + (10 * ((reporttarget.Column - 2) / 3))
    docno = claimdata.Cells(docnorow, 3)
Else
    docnorow = (reporttarget.Row - 112) + (10 * (reporttarget.Column - 11))
    docno = claimdata.Cells(docnorow, 7)
End If

'load up the document as if it was a hyperlink, this is the easiest way to open it within ADOS Viewer
ThisWorkbook.FollowHyperlink ados_document.Retrieve(docno, "", ados_server)
ThisWorkbook.Activate
protect_sheets
Exit Sub

adoserr:
MsgBox "It was not possible to load this document. This may be due to ADOS not being installed on the computer, or incorrect access rights.", , "Error accessing document"
protect_sheets

End Sub
The issue is that the macro fails on this line

Dim ados_server As New ADOSServer

Which I believed the error handling should have taken care of. Any ideas what else I can do?

Many thanks in advance

Gary Stone
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
You could loop through all References first and check that it exists:

Sub Test()
Dim i As Long

For i = 1 To Application.VBE.ActiveVBProject.References.Count
If Application.VBE.ActiveVBProject.References(i).Name = "ADOSServer" Then 'I'm not sure of the correct name here please check
'Continue with Code
End If
Next i
End Sub
 
Upvote 0
You could loop through all References first and check that it exists:

Sub Test()
Dim i As Long

For i = 1 To Application.VBE.ActiveVBProject.References.Count
If Application.VBE.ActiveVBProject.References(i).Name = "ADOSServer" Then 'I'm not sure of the correct name here please check
'Continue with Code
End If
Next i
End Sub

That works great, thanks very much.
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,717
Members
452,939
Latest member
WCrawford

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