+ Reply to Thread
Results 1 to 20 of 20

Missing references VBA fix (to avoid compile error)?

  1. #1
    Registered User
    Join Date
    11-03-2009
    Location
    Derby, UK
    MS-Off Ver
    Excel 2007
    Posts
    9

    Missing references VBA fix (to avoid compile error)?

    Hi,

    I have an excel database which links into Outlook and Word via macros to automate sending of e-mails and creating documents, etc. Obviously, I have created the correct VBA references and things have been working fine for a while.

    However, this is a shared workbook over a small number of machines and due to a recent upgrade, one of the machines is running Vista and Office 2007, whereas the rest run Office 2000 and NT.

    All works well until the workbook is opened and saved on the Office 2007 machine as this then changes all the references to Word 12, Outlook 12, etc, instead of Word 9 as seen in Office 2000. Then, when an office 2000 machine opens the workbook, it has a compile error as it cannot find the office 12 references!!

    I have created some code to fix this, which uses the AddFromGuid method, which works ok, e.g.:
    Please Login or Register  to view this content.
    What I want to do is as excel opens, check for missing references and fix them automatically, which is pretty simple on the face of it. However, as the macro fails due to a compile error all macros stop running, therefore whatever I put in the Open_Workbook event doesn't run.

    Does anyone know how to either stop the compiler running on start up, or a way of error handling the compile error??
    Last edited by Leith Ross; 11-03-2009 at 05:15 PM. Reason: Added Code Tags

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

    Re: Missing references VBA fix (to avoid compile error)?

    Either

    a) add some code to the Workbook_BeforeClose routine to unload the reference(s) such that they are not missing on open

    b) use Late Binding and avoid need for referencing libraries in first instance
    (can make coding trickier and I would say it's not always possible pending the actions being performed)

    Examples...

    http://www.excelforum.com/excel-prog...ough-code.html
    Last edited by DonkeyOte; 11-03-2009 at 06:58 PM.

  3. #3
    Registered User
    Join Date
    11-03-2009
    Location
    Derby, UK
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: Missing references VBA fix (to avoid compile error)?

    DonkeyOte,

    Thanks for the response.

    If I unload the references before close then you are correct, they won't be classed as missing on start up. However, the compile error will still occur as the coding for operating Outlook and Word won't be recognised (as there are no references at all).

    As for late binding, I did consider this but there are thousands of lines of code in the workbook (it does a lot of clever stuff) and I don't want to have to re-write all of this. All of my experience is in early binding and could really do with a solution that supports this.

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

    Re: Missing references VBA fix (to avoid compile error)?

    If I unload the references before close then you are correct, they won't be classed as missing on start up. However, the compile error will still occur as the coding for operating Outlook and Word won't be recognised (as there are no references at all).
    No, I think you're missing my point.

    Early Bind in the Workbook_Open event, remove the libraries in the Close event.
    Bind appropriately (by checking version).
    The link provided previously gave an illustration of this concept.

    If you applied your logic to the entire process the same would be true upon Close - ie at the point of de-referencing the library all code referencing said objects would generate debug - it does not.
    Last edited by DonkeyOte; 11-04-2009 at 07:44 AM.

  5. #5
    Registered User
    Join Date
    11-03-2009
    Location
    Derby, UK
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: Missing references VBA fix (to avoid compile error)?

    DonkeyOte,

    I guess I am missing something. I removed the references manually and when I saved the workbook I received compile errors, all relating to Outlook and Word code. An example of the code causing the error is below:

    Public myfolder As MAPIFolder

    Obviously this is Outlook code, and as the reference no longer exists, then it causes a compile error.

    Regardless of this I then continued. I put code in the workbook open even to add the correct references, but this did not run as the compile error occurred on start up before any code would run.

    I've had a look at the link you provided but can't see how it helps.

    Apologies, I may just be having a thick moment.........

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

    Re: Missing references VBA fix (to avoid compile error)?

    So my question would be... are you invoking routines via the Open event ?

    If you are I'm not quite sure how you can handle this to be honest using Early Binding. The variable being Public shouldn't matter unless the module in which it resides is invoked from the open event at which point it will compile the Module (ie running the binding first in the open event will not make any difference).

    Attached is a basic demo of the point... if you open the file you should find the word library has been added without compile error, closing the file and opening another file will show it's been removed upon close... however if you re-open the file, activate the call in the open event to test save and re-open the file you will get the compile error you're referring to.
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    11-03-2009
    Location
    Derby, UK
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: Missing references VBA fix (to avoid compile error)?

    DonkeyOte,

    I can't seem to find your attachment but I understand your point I think as I just did a similar test.

    I created a workbook with the following code to operate Outlook in module 1:

    Please Login or Register  to view this content.
    In the ThisWorkbook object I put the following:


    Please Login or Register  to view this content.
    And this works perfectly. However, doing similar on the proper workbook still has a compile error. This then confused me.

    However, as you guessed in the last post I am also invoking other routines in the Open_Event in the proper workbook:

    Please Login or Register  to view this content.
    Is this the reason why it's compiling and subsequently failing???
    Last edited by fantata1; 11-04-2009 at 08:21 AM. Reason: To split out the code

  8. #8
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,434

    Re: Missing references VBA fix (to avoid compile error)?

    Are the error caused by you doing this action?

    I removed the references manually
    Personally I would go with the late binding approach.
    You would need to replace explicit references to objects with generic object.

    Please Login or Register  to view this content.
    Also any constants would need to be declared. If you use Option explicit you should be able to track these down.

    With regards to other code in the Open event you can get around that by moving the code to a separate module and after doing the AddFromGuid then call that routine.
    Cheers
    Andy
    www.andypope.info

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

    Re: Missing references VBA fix (to avoid compile error)?

    Quote Originally Posted by Andy Pope View Post
    With regards to other code in the Open event you can get around that by moving the code to a separate module and after doing the AddFromGuid then call that routine.
    Thanks Andy - the one thing I did not check ... once again I am guilty of making "assumptions" without testing (ie that it would work it's way through and still debug)

    Can you outline (roughly) the process of code compilation (ordering etc) - as is blatantly obvious I'm not overly clear on it myself.

  10. #10
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,434

    Re: Missing references VBA fix (to avoid compile error)?

    I don't pretend to know the in's and out's of how the compiler works. I can not find that level of info in any of my books. Even web searching was fruitless

    I think, and I could well be wrong, that a module is not compiled until it is referenced by a some code. This assumes the code has not be manualled compiled or the VBE setting "Compile On Demand" is not set

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

    Re: Missing references VBA fix (to avoid compile error)?

    Thanks Andy I appreciate your input.

    So in short, as seemingly illustrated here, only Modules explicitly referenced in the Open Event(s) will be compiled upon Open, ie:

    Please Login or Register  to view this content.
    then

    Please Login or Register  to view this content.
    and

    Please Login or Register  to view this content.
    on the workbook being opened only Module1 is initially Compiled even though in turn Module1 explicitly references Module2... Module2 will be compiled when Module1 is active in terms of run-time (by which time libraries have been set etc... hence no error)

    Interesting stuff (to me at least).

  12. #12
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,434

    Re: Missing references VBA fix (to avoid compile error)?

    Looks like the compile does not occur until the actual routine is called.

    I constructed and compiled the code with the reference to Outlook.
    I then removed the reference and stepped through the code from the Open event. No compile error until a routine with an unresolved reference was called
    Attached Files Attached Files

  13. #13
    Registered User
    Join Date
    11-03-2009
    Location
    Derby, UK
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: Missing references VBA fix (to avoid compile error)?

    Guys,

    You are both superstars!!

    I've moved the additional code from the open event into a new module, and also moved all the routines this in turn invokes into that module.

    Hence, all that is now left in the Workbook_Open event is this:

    Please Login or Register  to view this content.
    Now, when the workbook opens the references are re-instated and the compile error does not appear as there are no references to Outlook and Word in the module that Sub OpenReg is in.

    Geniuses!!

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

    Re: Missing references VBA fix (to avoid compile error)?

    fantata1 - glad you have resolved ! I've learnt something too so thanks for asking the question in the first instance !

    Quote Originally Posted by Andy
    Looks like the compile does not occur until the actual routine is called.
    Hmm... but that would contradict the earlier code example, no ?

    ie if you had A_Mod1 called from Workbook_Open but called after the Library was added it would still debug even though by the time A_Mod1 is physically called the references should resolve given the libraries have been added.

    Or am I missing something... is there something specific about how/when the libraries are added mid routine ?

    I'm guessing an automation issue of some sort ?

    (apologies if (as I suspect I am) I am missing the point...)
    Last edited by DonkeyOte; 11-04-2009 at 10:07 AM.

  15. #15
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,434

    Re: Missing references VBA fix (to avoid compile error)?

    If I extend the Open event with this I still do not get an error.

    Please Login or Register  to view this content.
    The reference is in place before any of the "A" routines get called.
    Note: you will not be able to step through the code this time.

  16. #16
    Registered User
    Join Date
    11-03-2009
    Location
    Derby, UK
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: Missing references VBA fix (to avoid compile error)?

    I'm not sure how this contradicts the solution...

    In his example Andy does not add any reference to Outlook though. Therefore it will only fail when it reaches the unresolved outlook reference. It doesn't trigger on start up as I was seeing as it's not directly invoked from the Open Event.

    If in Andy's example you add references to Outlook in module 1 using the AddfroMGuid then this stops the compile error.

    Surely this is reflecting my error and how it was resolved.

  17. #17
    Registered User
    Join Date
    11-03-2009
    Location
    Derby, UK
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: Missing references VBA fix (to avoid compile error)?

    Sorry, posted before seeing Andy's latest response!!

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

    Re: Missing references VBA fix (to avoid compile error)?

    Quote Originally Posted by AndyP
    The reference is in place before any of the "A" routines get called.
    I guess I'm back to square one...

    On that basis why does my earlier attachment debug when the subroutine test is called from the open event given it is only called after the routine which is adding the Word library ?
    (the obvious implication is that the Module in which test is compiled before the library is available and thus debugs)

    However I just tested your earlier file and moved the library addition code to a further subroutine (so as to mimic setup of my earlier sample file) and your file still works without issue

    So by powers of deduction I can only assume the issue relates to the nature of the EarlyBindLibrary routine as used in my earlier file (not GUID based).

    Any ideas ?

  19. #19
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Missing references VBA fix (to avoid compile error)?

    Your Public variable is the problem I think. In order for the ThisWorkbook module to compile, the editor has to get the address of the Test function. It would appear that while doing this, although it doesn't check inside the procedures in that module, it does attempt to check any public or module-level variables.
    Remember what the dormouse said
    Feed your head

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

    Re: Missing references VBA fix (to avoid compile error)?

    Bingo, thanks R ... in all honesty I thought I'd checked that earlier - ie this was why I had added a non-Public variable to my test routine (so as to be able to toggle) but I guess for whatever reason that one slipped past me.

    Mystery resolved. Onwards and downwards.

+ 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