0

I'm writing code for a template that needs to work in many Word versions (2003 through 2013, minus 2008). I've accepted that it won't compile in all of those versions. I mostly write my code in Word 2010. Up until now I've always been able to get it to compile in 2010, but now I've hit a stumbling block.

I need to add a comment to a document with a specific user name that I define. I do this by holding the current username and userinitials in variables, setting them to what I want, adding my comment, and then resetting the parameters. Simple enough. However, in Word 2013 an additional parameter must be set in order to get this to work: Application.Options.UseLocalUserInfo. As this doesn't exist in Word 2010, I can't compile.

I recognize this is mostly just an inconvenience (I can comment out that code, check for compile issues, and then uncomment it). But I wanted to check and see if there was a better, less hinky solution. Most of my experience with cross-version compatibility is in using compiler constants and late binding, neither of which help here (as far as I can tell).

The basic code is below. Thanks!

    With Application
       sDocAuthorName = .UserName
       sDocAuthorInitials = .UserInitials
       If IsWord2013 = true Then bUseLocal = GetUseLocalUserInfo
   End With

   With Application
       .UserName = sQAAuthorName
       .UserInitials = sQAAuthorInitials
       If IsWord2013 = true Then .Options.UseLocalUserInfo = True
   End With

   'Do something

   With Application
       .UserName = sDocAuthorName
       .UserInitials = sDocAuthorInitials
       If IsWord2013 = true Then .Options.UseLocalUserInfo = bUseLocal
   End With
4

1 回答 1

1

I don't have Word 2013 so I can't test in that environment, but I do have Word 2010, and this code compiles if you use late-binding:

Sub Foo()
Dim wdApp As Object
Dim IsWord2013 as Boolean
    IsWord2013 = False
    Set wdApp = Application

    If IsWord2013 Then wdApp.Options.UseLocalUserInfo = True


End Sub
于 2013-10-24T16:08:19.057 回答