Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preventing Excel VBA compile errors due to users having an older version of MS Office (MS Outlook) installed?

Tags:

excel

vba

If I have a spreadsheet where I've referenced MS Outlook 14.0 Object Library from the VBA editor, and a user who only has MS Outlook 12.0 installed, then when that user opens the spreadsheet, they get a compile error on this line:

Range("export_date") = Date - 1

If they go into Tools, References, in the references list, there is this error:

MISSING: MS Outlook 14.0 Object Library

If they deselect that library, and instead select

MS Outlook 12.0 Object Library

...the code then properly compiles and the spreadsheet works fine for them.

I don't really understand why it fails on the Date() function, as that is VBA function, not an Outlook function. But even more important, is there a way to avoid this situation? The only thing I can think of is to not set references, and just use variables of type Object and instantiate via CreateObject("Outlook.Application"), etc, but I hate to give up strong typing, etc.

Can anyone suggest a superior way to handle this issue of backwards compatibility with older versions of MS Office?

like image 632
tbone Avatar asked Feb 28 '12 23:02

tbone


People also ask

How do I fix compile errors in Excel VBA?

I have resolved same error by following these 4 steps : Open Excel file which is having issue, press Alt + F11 go into its Visual Basic Editor. From the Tools menu select References ( Note, if references option is disabled in tools menu try closing and reopening file and enable Macro before proceeding next steps)

Can VBA interact with Outlook?

You heard it right. This task of writing an email and sending the file can be automated with the help of VBA. The reason is that VBA can use a reference with different Microsoft Objects like outlook, word, PowerPoint, paint, etc. So we can send the email with the help of VBA.

Why am I getting a compile error in VBA?

A compilation error can also occur when VBA doesn't find anything missing while typing the code, but it does when the code is compiled or executed. VBA checks each line as you're typing the code and highlights the syntax error as soon as the line is incorrect and you hit enter.

How do you solve compile error the code in this project must be updated for use on 64 bit systems?

Resolution. To resolve this issue, ignore the "Compile error" and run the VBA code in the 64-bit version of the Office 2010 program.


1 Answers

tbone, what you refer to as Strong Typing is called "Early Binding".

Unfortunately one of the drawbacks with Early Binding is that if the end user doesn't have the same version as yours then you will get those errors.

If you ask me, I prefer Late Binding (where you don't create references and use CreateObject to create an instance)

An interesting read.

Topic: Using early binding and late binding in Automation

Link: http://support.microsoft.com/kb/245115

My Suggestion

Don't give up Early Binding if you like it because of intellisense. However before you distribute your application, change the code to Latebinding. There is not much of a difference in the way you code in Early Binding and Late Binding.

Here is an example

Early Binding

'~~> Set reference to Excel Object Library
Sub Sample()
    Dim oXLApp As Excel.Application
    Dim oXLBook As Excel.Workbook
    Dim oXLSheet As Excel.Worksheet

    '~~> Create a new instance of Excel
    Set oXLApp = New Excel.Application
    '~~> Add a new workbook
    Set oXLBook = oXLApp.Workbooks.Add
    Set oXLSheet = oXLBook.Worksheets(1)

    '
    '~~> Rest of the code
    '
End Sub

Late Binding

'~~> Doesn't require a reference to Excel Object Library
Sub Sample()
    Dim oXLApp As Object
    Dim oXLBook As Object
    Dim oXLSheet As Object

    '~~> Create a new instance of Excel
    Set oXLApp = CreateObject("Excel.Application")
    '~~> Add a new workbook
    Set oXLBook = oXLApp.Workbooks.Add
    Set oXLSheet = oXLBook.Worksheets(1)
    '
    '~~> Rest of the code
    '
End Sub

HTH

Sid

like image 196
Siddharth Rout Avatar answered Sep 21 '22 19:09

Siddharth Rout