问题
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
回答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
来源:https://stackoverflow.com/questions/19569407/how-to-use-word-2013-parameter-in-vba-template-and-compile-in-word-2010