Results 1 to 8 of 8
  1. #1
    5 Star Lounger
    Join Date
    Jul 2001
    Location
    Terneuzen, Netherlands
    Posts
    895
    Thanks
    0
    Thanked 0 Times in 0 Posts
    When I reference to a DLL (let's call it ABC.DLL), the following call in VBA works for me:

    Sub t
    TheCall Arg1, Arg2
    End Sub

    Now I would like to generate an error when the code runs if the DLL is not found. I think, late binding will help me. So I tried this:

    Sub t
    Dim DML As Object
    Set DML = CreateObject("c:\Mypath\ABC.DLL")
    TheCall Arg1, Arg2
    End Sub

    That was too easy and gives an error on the "Set" command. I think I understand a bit why this might fail but I'm also in the dark on how to fix.

    Any little suggestion would be appreciated.



  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    You could use an error handler, but I don't think creating an object will allow you to use TheCall without qualifying it. Would DL.TheCall work?

  3. #3
    5 Star Lounger
    Join Date
    Jul 2001
    Location
    Terneuzen, Netherlands
    Posts
    895
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by HansV View Post
    You could use an error handler, but I don't think creating an object will allow you to use TheCall without qualifying it. Would DL.TheCall work?
    Yes, sorry, that's what I meant:

    Sub t
    Dim DML As Object
    On error goto DLL_Not_Found
    Set DML = CreateObject("c:\Mypath\ABC.DLL")
    TheCall Arg1, Arg2
    exit sub
    '
    DLL_Not_Found:
    MsgBox "Error message"
    End Sub

    Point was (and still is) that the call doesn't work like this (if the DLL is found).

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    To call a function from a DLL without setting a reference, you'd need to include a Declare statement at the top of a module, but that would bomb if the DLL is not found...

    (See Accessing Functions in a DLL and Anatomy of a Declare Statement)

  5. #5
    5 Star Lounger
    Join Date
    Jul 2001
    Location
    Terneuzen, Netherlands
    Posts
    895
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by HansV View Post
    To call a function from a DLL without setting a reference, you'd need to include a Declare statement at the top of a module, but that would bomb if the DLL is not found...

    (See Accessing Functions in a DLL and Anatomy of a Declare Statement)

    OK... now I've lost it ... I thought I wanted late binding so I could test if the DLL would be present in the first place... I believe that's why we do late binding in the first place. Now I've lost track... again: all I want is to call a function in an external DLL. Using early binding, I can set a reference to the DLL and all works. Now I have two cases:

    (1) The DLL is found... my problem is how to make that call work (but I'll check your links)

    (2) The DLL is not found... I should be able to trap that

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    A DLL is not necessarily an automation object, so you won't always be able to use CreateObject with it. In other words, chances are that late binding makes no sense in your situation. Please see the links from my previous reply.

  7. #7
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts
    Why not use Dir to look for the dll?
    Regards,
    Rory

    Microsoft MVP - Excel

  8. #8
    5 Star Lounger AndrewKKWalker's Avatar
    Join Date
    Apr 2001
    Location
    Cambridge, UK
    Posts
    1,020
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Good Call...

    Dir Returns "" if file/folder not found


    If you want to test for the folder existence as well you can use

    'Test for Missing Folder
    If Dir("c:\Mypath\",vbDirectory)="" Then ....


    'Test If DLL File is Missing
    If Dir("c:\Mypath\ABC.DLL")="" then ...


    Obviously you need to decide how to handle the missing file,
    and then only use the call if it Passes the tests
    Andrew

Posting Permissions

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