+ Reply to Thread
Results 1 to 9 of 9

Checking references automatically through code?

  1. #1
    Forum Contributor
    Join Date
    01-13-2009
    Location
    Mumbai
    MS-Off Ver
    Excel 2003, 2007
    Posts
    168

    Checking references automatically through code?

    I have an excel add-in that has got few macros which require two references,

    1) Microsoft Word 12.0 object library
    2) Microsoft Internet Controls

    I have checked both these references and the macros are running perfectly on my PC. but when I distribute this add-in, sometimes the references are checked but sometimes it gives an error and the user is required to check the reference again.

    Can i use some code to automatically check these two references. or is there some other easier way to get rid of this error.

    Regards,
    Vaibhav
    Last edited by c.vaibhav; 05-05-2009 at 05:54 AM.

  2. #2
    Forum Contributor
    Join Date
    01-13-2009
    Location
    Mumbai
    MS-Off Ver
    Excel 2003, 2007
    Posts
    168

    Re: Checking references automatically through code?

    I hope you understand my problem..

    Basically I want to use statement like "imports system" like how we use in VB.NET..

    Does anyone have any idea?

    any help would be greatly appreciated..

  3. #3
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Checking references automatically through code?

    Do all users run the same version of Office and do people run a complete suite of Office in one version or have a mix - ie Excel 2007 but Word 2003 etc...
    If you run different versions you will find trying to register 12.0 on an 11.0 machine will generate an error given the library doesn't exist... if everyone's XL version = Word version you can use

    Please Login or Register  to view this content.
    as basis to determine which library to install... alternatively

    Please Login or Register  to view this content.
    Something like the above would give you the version but you can see also the above utilising late-binding does not require the object library at all.

    overview of late vs early: http://www.*****-clicks.com/excel/olBinding.htm
    Last edited by DonkeyOte; 05-05-2009 at 02:17 AM.

  4. #4
    Forum Contributor
    Join Date
    01-13-2009
    Location
    Mumbai
    MS-Off Ver
    Excel 2003, 2007
    Posts
    168

    Re: Checking references automatically through code?

    Hi DonkeyOte,

    Thank-you for your reply..

    All the users run the same version of office and that there is no mix of versions.. complete MS office 2007 is installed on all the PC's

    As the porblem is machine-specific, once I eradicate the error by checking the reference the error doesn't come again. However the same error can occur on somebody else's PC.. and it is not possible for me to check all the PC's.. instead I would like to have solution that will eliminate error irrespective of the machine..

    Please Login or Register  to view this content.
    The above code creates a word application even without checking the reference.. Now I am in a dilemma..

    does this code work because I had checked the word reference once and now I dont need to check it again and again.. or is there something in the code that enables the WORD reference..

    I would also like to know the role played by "Debug.Print" statement..

    Is this a foolprrof way by which I can enable the reference..

    Thank you so much..

    Vaibhav

  5. #5
    Forum Contributor
    Join Date
    01-13-2009
    Location
    Mumbai
    MS-Off Ver
    Excel 2003, 2007
    Posts
    168

    Re: Checking references automatically through code?

    Please Login or Register  to view this content.
    Can we install library at run-time??

    Regards,
    Vaibhav

  6. #6
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Checking references automatically through code?

    If everyone runs consistent suite of apps then things are relatively straightforward for early binding, eg:

    Please Login or Register  to view this content.
    and if desired run a remove before closing the file (could cause issues if library being used in other files in same application instance)

    Please Login or Register  to view this content.
    Both of the above residing in ThisWorkbook object.

    In a separate module, the below code:

    Please Login or Register  to view this content.
    (In 2007 MS Internet Controls is seemingly not listed by default in References)

    You didn't specify what errors you were getting previously... if you try and add the library and it's referenced already you would generate an error, the above has a handler in place... obviously the handler means that if it's not added because it's not available then your other code will still a generate an error given the objects used can't be found (as library not referenced)

    re: the other library-less approach ... this is late binding... you will find lots of articles on line about the method, far superior to anything I could put together... in short late binding can be a little slower but more importantly harder to code as you don't have intellisense when you put it together, the advantage is of course you don't need to reference the library... early binding with libraries gets more complex if you run multiple versions as libraries are not backwards compatible... ie someone using say Word 12.0 library who forwards the file to someone on 2003 (11.0) will generate an error.

    I should add that elements of the above code are based off code posted by Leith Ross here previously...
    Last edited by DonkeyOte; 05-05-2009 at 04:28 AM.

  7. #7
    Forum Contributor
    Join Date
    01-13-2009
    Location
    Mumbai
    MS-Off Ver
    Excel 2003, 2007
    Posts
    168

    Re: Checking references automatically through code?

    for the following line of code,

    Please Login or Register  to view this content.
    I get an error - "User-Defined Type not defined", if the reference of word is not enabled.. however when i enable the reference it works fine..

    In your code I am not getting desired result (i.e. automatic enabling of MSWORD.OLB), the following line is not functioning properly I guess,

    Please Login or Register  to view this content.
    When I remove - "On Error Resume Next".. I get an error -
    "Programmatic access to visual basic project is not trusted" - Less frequent
    "Method 'VBE' of object '_Application' failed" - More Frequent

    I had put the code in ThisWorkbook.. I also tried putting in the main module by giving the sub different names.. Neither the references is enabled automatically neither is it removed if i enable it manually..

  8. #8
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Checking references automatically through code?


  9. #9
    Forum Contributor
    Join Date
    01-13-2009
    Location
    Mumbai
    MS-Off Ver
    Excel 2003, 2007
    Posts
    168

    Re: Checking references automatically through code?

    Hey DonkeyOTE,

    That worked perfectly!!!!

    Thanks a lot.........

    one more small question..

    by following your procedure, can I by any chance encounter any kind of error related to the reference part.. though you have written "On Error Resume Next"

    I cannot see any error in the code that you have written.. but still I just want to be sure.. as you are the expert

    Thanks again..
    Last edited by c.vaibhav; 05-05-2009 at 05:58 AM.

+ 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