1

So I have an MS Access application originally in a 2002-2003 format. Developed on a production network image, and when the image pushed the office upgrade, it became office 2007. So this is used in a very non traditional way...reps take it with them on company laptops enter data, and it automates complex ppt presentations, and excel worksheet for them (they love it....they need it). Data is not held in this like a database repository, just long enough to produce the automation they need and it works....saves them time. So Access is limited, and in the use case, really limited since i am using it in a way it certainly wasn't intended to be used. I get that, but like I said so far, its working pretty well.....

The one bump I have run into is if someone has to use another computer for whatever reason (doesn't happen often but it got me thinking), and say they have a version of 2002-2003 access, the tool will run, but once we get to the code routines/modules for ppt & outlook, the object libraries show MISSING because working on this tool seems to automatically cause the libraries to go up to the next available version, but not down to find the version.....

so originally when I made this....it used MS PowerPoint 11.0, and MS Outlook 11.0, and then when I had to start working in 2007 it became 12.0 for both, some instances I see it bumped up to 14.0, and none of the ups are a problem, but now since I have an image with Office 2007 out, and new files version I try to give them has the libraries defaulted to 12.0 and if they run into a scenario where they would take the file on a disc and use it on a computer that has office 2003, those libraries just come up missing and it doesn't recognize the appropriate 11.0 object libraries anymore.

So finally my question....any suggesions (save the obvious and build a real app...lol...would love to....no $) to handling this? Is there code that can/will evaluate what libraries need to be set on opening the file using vba? Is that possible?

JimmyPena
  • 8,694
  • 6
  • 43
  • 64
Justin
  • 4,461
  • 22
  • 87
  • 152

1 Answers1

4

There is Remove Method which you could use to remove a reference, then AddFromFile Method or AddFromGuid Method to add a reference.

The AddFromFile approach was offered in answers to this recent StackOverflow question: Add references programatically However, make sure to read Tony's answer ... if you're distributing your application as an MDE, you're SOL.

Consider switching to late binding for your PowerPoint and Outlook stuff. That way you won't have to fiddle with references (no reference required for late binding). A drawback from late binding is you lose the benefit of IntelliSense during development. However, you can develop with early binding, then switch to late binding for deployment. Substitute the values where you used any named constants from the referenced library ... debug will highlight any you overlook.

A frequent counter-argument is that late binding imposes a "performance penalty". However, I've yet to encounter a use case where the "slow-down" was large enough to be noticeable by a human observer.

In any case I've been using late binding for years specifically to avoid the type of deployment issue you're struggling with now.

Edit: Here is an example which uses the Excel type library. I have it set to use early binding. To convert it to late binding, comment out the declarations under '* early binding ... , and uncomment those under '* late binding ...

Also notice the 2 lines which include the named constant, xlAutomatic.

Public Sub EarlyVsLateBinding()
    Dim strFullPath As String
    '* early binding requires reference to Excel type library *'
    Dim objExcel As Excel.Application
    Set objExcel = New Excel.Application
    Dim objBook As Excel.Workbook
    Dim objSheet As Excel.Worksheet
    '* late binding; no reference required *'
'    Dim objExcel As Object '
'    Dim objBook As Object '
'    Dim objSheet As Object '
'    Set objExcel = CreateObject("Excel.Application") '

    strFullPath = CurrentProject.Path & Chr(92) & "foo.xls"

    Set objBook = objExcel.Workbooks.Open(strFullPath)
    Set objSheet = objBook.Worksheets("bar")
    objSheet.Range("B1").Select
    With objExcel.Selection.Font
        .Name = "Arial"
        .FontStyle = "Regular"
        .Size = 10
        .ColorIndex = xlAutomatic 'named constant available with reference set '
        '.ColorIndex = -4105 'substitute xlAutomatic value so we drop reference
    End With
    objBook.Close SaveChanges:=True
    objExcel.Quit
    Set objSheet = Nothing
    Set objBook = Nothing
    Set objExcel = Nothing
End Sub
Community
  • 1
  • 1
HansUp
  • 95,961
  • 11
  • 77
  • 135
  • Thanks Hans....is there perhaps a resource for learning about the late binding suggestion? I am not familiar with the distinction. Thanks! – Justin Apr 10 '11 at 20:28
  • Start with the link Tony offered in his response to that other SO question. http://www.granite.ab.ca/access/latebinding.htm – HansUp Apr 10 '11 at 20:31
  • I appreciate it as always Hans. THough it seems like after investigation and trying this out...this doesn't look right for me. Unless I have something wrong, this Access app I have is not anything like a typical access app at all...its main purpose is automating ptt, word, outlook, excel, xml files, etc and if I use late binding, I have to write a version specific set of routines per job?? that is alot of work, and I pretty much won't have time to stand it up...let alone boxes with all the different libraries to figure out the syntax....am I missing something with this? – Justin Apr 17 '11 at 18:54
  • 1
    I dunno, Justin. Fer sure one of us is missing something; maybe it's me. I added a simple example for early vs. late binding with Excel. It doesn't seem like much effort to me to develop with early binding and switch to late binding for deployment. If that's more effort than you consider reasonable, then you get to spend your time resolving the problems which result from version differences for the Office type libraries. I don't want to spend **any** time on that. – HansUp Apr 17 '11 at 20:18
  • thanks for the example! I think I may need to learn "how" to do this a little better...perhaps my conclusion was a little too early. – Justin Apr 18 '11 at 02:00
  • You're welcome. That approach works for me. If I had scads of procedures to switch between late and early binding I would adopt the compiler constants/directives approach Tony T. described in that link I gave you. But it's not been an issue for me. – HansUp Apr 18 '11 at 22:34
  • Also, I've focused on my reasons in favor of late binding. However, I'm biased by my experience. To see some counter-arguments, read the comments from this SO answer: http://stackoverflow.com/questions/4813598/using-the-browse-for-file-dialog-in-access-vba/4813662#comment-5342521 – HansUp Apr 18 '11 at 22:36
  • @HansUp.....wow! that was educational. lol. I get the major argument for late binding, especially in the case of traditional access db dev and deployment. what i am doing is completely atypical from that traditional sense. I am faking a desktop app (real cheap fake to a developer at that) sort of, in that none of these files are linked to a repos...its simply a GUI that creates other office files, and that is it.....about 11 different types of ppt presentations, 18 different xls workbooks, 5 docs, auto emails, 7 different xml files for data transfer via attached emails, etc....cool i think... – Justin Apr 22 '11 at 19:38
  • ...so I am looking for the best solution that will allow that automation to work. Not so much preventing the entire app from breaking, because the automation is the app so to speak. I could give my people mdb(s) and educate them on how to set the references themselves if they get the errors but i am afraid so tell them to do anything where the code lives. Either way its obvious from the good advice that you guys always give, and have given me for over a year, that I need to learn late binding. Not sure if it will fix my situation but I need some reference to start learning the differences in – Justin Apr 22 '11 at 19:42
  • ..the syntax, because I basically learned the VBA for all the office programs (ppt, xls, outlook, etc) by using early binding, or explicit objects relative to the app.... i.e. ppt full of textframe.textrange & shape.shaperange, etc ....as always i greatly appreciate the help & advice! :) – Justin Apr 22 '11 at 19:44
  • 1
    I really don't know why you're resisting this. Late binding just works. Try it and see. You're making it more complicated than it has to be by insisting on early binding. – David-W-Fenton Apr 26 '11 at 03:37
  • 1
    ...I did try....it does! Thanks guys! – Justin Apr 29 '11 at 00:43