AnsweredAssumed Answered

VISA COM and Excel VBA error handling

Question asked by pdmochow on Aug 2, 2013
Latest reply on Aug 2, 2013 by hognala
I am writing a test script using Excel , Command expert and VBA.
At some point I setup instruments and I have a VBA code that checks whether the instrument is connected.
From this forum I got the VBA code (below). It works fine when the instrument is connected but hangs and returns error: " Automation error. Not able to convert object"
Debugging shows that if there is no instrument at the given IP address the code crashes at the line 
Set instrument.IO = ioMgr.Open..........
I have a spreadsheet with a button connected to this script. It places the instrument ID string in a given cell, but crashes when there is no instrument. Normally I have DSO9000 scope connected.
How to make it foolproof  when there is no instrument at the other end ?

Sub instrIDN()
    Dim ioMgr As VisaComLib.ResourceManager
    Dim instrument As VisaComLib.FormattedIO488
    Dim idn As String
    Dim instrADDR As String
    Set ioMgr = New VisaComLib.ResourceManager
    Set instrument = New VisaComLib.FormattedIO488
    instrADDR = "TCPIP0::"
'    instrument.IO.Timeout = 1000
    Set instrument.IO = ioMgr.Open(instrADDR, AccessMode.NO_LOCK, 1000, "")
    instrument.WriteString "*IDN?"
    idn = instrument.ReadString()
    ActiveSheet.Cells(50, 2) = idn
End Sub