Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Word version dependent VBA code (Word 2000 / Word 2002)

    I need to write a Macro that uses a Word 2002 specific parameter when calling a function if it is running on Word 2002, but that calls a different Word 2000 function when it runs on Word 2000.

    Word 2000 gives a compile error if I include the Word 2002 syntax, even if it doesn't need to execute it
    I don't want to create and maintain 2 different templates - one for each version.

    I vaguely remember a syntax that would cause the VBE run time environment to conditionally skip compiling code, depending on the version, but a google search failed to show anything helpful.

    Does anyone have a clue what I am talking about or a pointer to something helpful.

    StuartR

  2. #2
    2 Star Lounger
    Join Date
    Feb 2001
    Location
    Brussels, Brussel, Belgium
    Posts
    159
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Word version dependent VBA code (Word 2000 / Word 2002)

    hi Stuart,

    put the 2002 code in a separate module and leave it uncompiled.
    there's no constant to use in a compiler directive to distinguish between 2k & 2k2(both vba 6.x), only between vba 5.x & 6.x

  3. #3
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Word version dependent VBA code (Word 2000 / Word 2002)

    Thanks, a separate module is a sensible approach, and I can just call the W2002 version of the routine based on Application.Version

    StuartR

  4. #4
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts

    Re: Word version dependent VBA code (Word 2000 / Word 2002)

    Hi Stuart,

    I think the following might have come from the post you're referring to, which isn't accessible at the moment:

    If coding macros that have to work differently under different Office versions, use the VBA compiler constants to select which code runs.
    #IF VBA6 then
    ' VBA 6 code here for Office 2000 and beyond
    #ELSE
    ' VBA 5 code here for earlier versions
    #End If
    Alternatively, if the feature has more to do with the application version and less to do with the language version:
    if Application.Version = "9.0" Then 'Office 2000

    Select Case Application.Version
    Case "8.0" 'Office 97
    Case Else 'Office 2000, XP, ...
    The #IF directive will do the correct thing with Properties and Methods that produce compile time errors. The Application.Version check is a run time check, so compile errors may still occur.

    Cheers
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  5. #5
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Word version dependent VBA code (Word 2000 / Word 2002)

    Thank you, that was just what I couldn't remember!
    Pity there isn't a constant to distinguish 2000/2002.

    StuartR

  6. #6
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Word version dependent VBA code (Word 2000 / Word 2002)

    You could always test the version in an autostart and then call a routine with a static variable that holds the version indicator. After that, you could call the function and have it just return the static variable. Then you could use the result to handle the conditional code. Here's some air code to illustrate what I mean.

    Public Function WordVersion(blnReset As Boolean) As String
    Static strVersion As String

    If strVersion = "" Or blnReset Then
    strVersion = Application.Version
    End If

    WordVersion = strVersion
    End Function
    Charlotte

  7. #7
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Word version dependent VBA code (Word 2000 / Word 2002)

    But would I be able to use that variable in a conditional compilation

    #If Variable Then
    Function that would generate a compile error in Word 2000
    #Else
    Word2000 version of the function
    #End If

    StuartR

  8. #8
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Word version dependent VBA code (Word 2000 / Word 2002)

    I haven't tried it that way but it should work. You don't have to use a compiler constant to branch conditional compilation code. The first time you called the function it would populate the static variable and return it. After that, if you called it again, it would simply return the value in the static variable.

    I have to warn you, though, that I haven't used conditional compilation recently, and then only in Access 2000.
    Charlotte

  9. #9
    2 Star Lounger
    Join Date
    Feb 2001
    Location
    Brussels, Brussel, Belgium
    Posts
    159
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Word version dependent VBA code (Word 2000 / Word 2002)

    this won't work. only actual compiler constants can be used in compiler directives.

  10. #10
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    New York
    Posts
    214
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Word version dependent VBA code (Word 2000 / Word 2002)

    Stuart,

    Did you ever find a solution to this problem of conditional compilation that you liked?

  11. #11
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Word version dependent VBA code (Word 2000 / Word 2002)

    Guy,

    I just created separate modules for each version, and called the appropriate code from the main module. This is modular, easy to understand, and avoids compiling the code for the wrong version during run time.

    StuartR

  12. #12
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    New York
    Posts
    214
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Word version dependent VBA code (Word 2000 / Word 2002)

    But that also means, doesn't it, that you can't compile the entire project without getting an error. So the template (as a whole) is distributed "uncompiled."

    That would do for what I'm trying to do, I think. I'm doing a Startup Add-in. Is that compiled once on startup? Or only when individual modules are called?

    Seems there should be constants for different versions -- considering that the sytax of VBA is changing from one to the other.

  13. #13
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Word version dependent VBA code (Word 2000 / Word 2002)

    You are correct, but the alternatives all seemed much worse to me.

    StuartR

  14. #14
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    New York
    Posts
    214
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Word version dependent VBA code (Word 2000 / Word 2002)

    I wouldn't be surprised if there were compiler constants for the application version, and MS simply didn't document them.

  15. #15
    4 Star Lounger
    Join Date
    Dec 2003
    Location
    Zoetermeer, Zuid-Holland, Netherlands
    Posts
    559
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Word version dependent VBA code (Word 2000 / Word 2002)

    <hr>Is that compiled once on startup? Or only when individual modules are called?<hr>
    That depends. In the VBE menu Tools, Options, General tab, there's a setting Compile on demand. When that is checked the code isn't compiled until it's called.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •