如何在VBA模板中使用Word 2013参数并在Word 2010中编译 [英] How to use Word 2013 parameter in VBA template and compile in Word 2010

查看:47
本文介绍了如何在VBA模板中使用Word 2013参数并在Word 2010中编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为需要在许多 Word 版本(2003 到 2013,减去 2008)中工作的模板编写代码.我已经接受它不会在所有这些版本中编译.我主要在 Word 2010 中编写代码.到目前为止,我一直能够在 2010 年编译它,但现在我遇到了绊脚石.

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.

我需要使用我定义的特定用户名向文档添加注释.为此,我将当前用户名和用户首字母保存在变量中,将它们设置为我想要的,添加我的评论,然后重置参数.足够简单.但是,在 Word 2013 中,必须设置一个附加参数才能使其工作:Application.Options.UseLocalUserInfo.由于 Word 2010 中不存在此文件,因此我无法编译.

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).

基本代码如下.谢谢!

    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

推荐答案

我没有 Word 2013,因此无法在该环境中进行测试,但我有 Word 2010,如果您使用后期,此代码会编译 -绑定:

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

这篇关于如何在VBA模板中使用Word 2013参数并在Word 2010中编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆