+ Reply to Thread
Results 1 to 5 of 5

Catch non-existent dll compile errors

Hybrid View

  1. #1
    Registered User
    Join Date
    01-07-2009
    Location
    Pittsburgh, USA
    MS-Off Ver
    Excel 2003
    Posts
    2

    Catch non-existent dll compile errors

    I have created a dll in .NET that i use it in excel. End users who use the excel workbook need to register the dll when they first use the workbook. Sometimes, they fail to register the dll and launch the workbook. The workbook has a reference in it to the .dll. As soon as they try to do anything they get a compile error "User-defined type not defined". This is because:
    Dim data
    Set data = New unit.Units
    and the "unit.dll" has not been registered to work with excel.

    What i want to do is to catch and interrupt the compile error gracefully and tell the user to go back and re-register the dll before using the workbook. As it stands now they just see compile error - and well you can imagine when they are looking at the vb screen quite confused.

    So i thought maybe i would create a type
    Type unit
    End Type
    but you can't have a type without members. And i'm not sure how this would work once someone registers the user-defined type by the same name.

    So - any ideas how to solve this problem?

    Thanks,

    -lta
    Last edited by likestheaction; 01-09-2009 at 05:08 PM.

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    Can't you just put On Error Resume Next before the New, and test for an error afterward?

    Edit: Never mind that, it's a compile error, sorry.

    Switch to late binding, and then test for an error:
    Dim oData as Object
    On Error Resume Next
    Set oData = CreateObject("Unit.Units") ' or whatever the object is
    If Err <> 0 Then
        '...
    Edit2: Please take a few minutes to read the Forum Rules, and then edit your post to wrap your code with Code Tags.
    Last edited by shg; 01-07-2009 at 08:51 PM.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    01-07-2009
    Location
    Pittsburgh, USA
    MS-Off Ver
    Excel 2003
    Posts
    2
    Thanks. I will try the late binding approach. I also tried below, and it works as well. I found it on some other website. Where UNITS_LIBRARY_NAME is a constant for the name of the dll. In the following example no references or object creation is necessary. So a different approach to the solution than my question indicated.

    -lta

    On Error GoTo UnitsError
    
    Dim ref As Object
    Dim fRef As Boolean
      
    fRef = False
    For Each ref In ThisWorkbook.VBProject.References
        If ref.Name = UNITS_LIBRARY_NAME Then
            fRef = True
        End If
    Next
      
    If Not fRef Then
        CheckUnits = False 'where CheckUnits is the name of the function
        MsgBox ("The tool could not verify that the units.dll is installed properly. " + vbCrLf + _
                 vbCrLf + _
                 "Verify that the units.dll installation was completed.  Please refer to the installation documentation.")
        Exit Function
    End If
    
    If fRef Then
        CheckUnits = True
        Exit Function
    End If

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    Don't you still get a compile error if you use early binding in the main code?

    You can also test for an add-in using GUID, I think, though I've not used it.

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello likestheaction,

    Here is a routine that will check if the DLL files exists or not. If it does exist, VBA will attempt to register it. If it doesn't exist, the user is alerted. You can change the file name and path to what you are using.
    Sub SelfRegister()
    
      Dim FileName As String
      Dim SysPath As String
      
        FileName = "user32.dll"
        SysPath = Split(Environ("Path"), ";")(1) & "\"
        
          If Dir(SysPath & FileName) <> "" Then
             Shell "regsvr32.exe " & SysPath & FileName
          Else
            MsgBox FileName & " is missing. Please install this file.", vbInformation
            Exit Sub
          End If
        
    End Sub
    Sincerely,
    Leith Ross

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1