Show-N-Tell Thursday - Writing code for multiple versions of an Office application

March 2 2006

ShowAndTell.jpg This week is back to a technical tip (for Ed's sake, I will try to alternate between technical and case study each week). This is a very simple one that showcases how you can write code that supports multiple versions of a Microsoft Office application. Many times, code you write for Word 2003 will not work in Word XP or 2000. To deal with this, a simple Select Case statement in LotusScript will allow you to create branches in your code. The code:

dim hwd

Set hwd = CreateObject("Word.Application")
Select Case hwd.version
Case "8.0"
     'This is a Microsoft Office97 application
Case "9.0"
     'This is a Microsoft Office2000 application
Case "10.0"
     'This is a Microsoft OfficeXP/2002 application
Case "11.0"
     'This is a Microsoft Office2003 application
Case "12.0"
     'This is a Microsoft Office12 application
End Select

That is just an example of the Select Case statement. I use this in my mail merge code when I need to deal with the way that different versions of Word supports Mail Merge. For instance, when creating the data file that I pass into Word, I have a block of code like this:

Select Case hwd.version
Case "8.0"
    LibMMCreateDataFile_97(notescollection, fieldstoinclude, selecteduser)
Case "9.0"
    LibMMCreateDataFile_2000(notescollection, fieldstoinclude, selecteduser)
Case "10.0"
    LibMMCreateDataFile_XP(notescollection, fieldstoinclude, selecteduser, "WordDocument")
Case "11.0"
    LibMMCreateDataFile_2003(notescollection, fieldstoinclude, selecteduser, "CSV")
Case "12.0"
    LibMMCreateDataFile_2007(notescollection, fieldstoinclude, selecteduser)
End Select

This just calls different functions based on the user's version of Microsoft Word. I use the same action button for all versions, so I do not have to deal with more design elements than needed. Just different functions in a Script Library. For those wondering, the fourth parameter of my functions for Word XP and 2003 determine what data file format I use. Starting with Word XP, you can pass a CSV file into the Mail Merge object, not just a Word document with a table. For Word 2007, I am moving to an XML file. More on that when 2007 Microsoft Office System Microsoft Office Word 2007 ships. :)