Sometimes you may need to check at runtime whether a particular component is installed. For example, you might have written a common code library that sends email in certain circumstances. But rather than allow it to break if you attempt to use it from a new server, you might wish to check for the CDONTS NewMail object programmatically first, and then send the email message if and only if the component is present. You can use the following function to make this determination. Simply pass the ProgId (the string you'd normally pass as the argument of the CreateObject method) to the IsComponentInstalled function and the Boolean return value will indicate whether the component is available for your use.
<% Option Explicit
Function IsComponentInstalled(ProgId)
Dim tmpObject
On Error Resume Next
Set tmpObject = Server.CreateObject(ProgId)
If Err.Number = 0 Then
IsComponentInstalled = True
Else
IsComponentInstalled = False
End If
Set tmpObject = Nothing
End Function
If IsComponentInstalled("CDONTS.NewMail") Then
Response.Write "We'll send the email message now
..."
'...
Else
Response.Write "We'll *not* send the email message
..."
'...
End If
%>
Comments