Me 关键字的行为不一致 [英] Inconsistent behavior for Me keyword

查看:57
本文介绍了Me 关键字的行为不一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到模板文件中VBA代码中Me关键字的行为会有所不同,具体取决于文档是否具有ActiveX控件.

I noticed the behavior of Me keyword in VBA code in template files is different depending if the document has ActiveX controls or not.

  • 带有 ActiveX 控件的文档:Me 引用从模板创建的新文件.
  • 没有 ActiveX 控件的文档:Me 引用模板而不是新文件.
  • Document with ActiveX controls: Me references the new file created from template.
  • Document without ActiveX controls: Me references the template instead of the new file.

为了演示这种奇怪的行为,我制作了两个示例文件:

To demonstrate this strange behavior I made two sample files:

两个文件是相同的,它们之间的唯一区别是一个有按钮而另一个没有.两个文件的VBA代码如下:

Both files are identical, the only difference between them is that one has a Button and the other doesn't. The VBA code of both files is the following:

Private Sub Document_New()
    Selection.TypeText "Me keyword is referencing """ & Me.Name & """."
End Sub

如果您从这些模板创建一个新文件,您会发现结果是不同的:

If you create a new file from these templates, you'll realize the results are different:

WithActiveX.dotm 内容是

WithActiveX.dotm content is

Me 关键字正在引用Document1".<-- 上面代码生成的字符串

Me keyword is referencing "Document1". <-- string generated by the code above

而没有ActiveX.dotm的内容是

while WithoutActiveX.dotm content is

Me 关键字引用WithoutActiveX.dotm".<-- 上面代码生成的字符串

Me keyword is referencing "WithoutActiveX.dotm". <-- string generated by the code above

我进行了几次测试,得出的结论是不一致的根源在于 ActiveX 控件的存在(没有与控件关联的代码:它仅存在于文档中).

I made several tests to conclude that the source of the inconsistency is the presence of ActiveX controls (there is no code associated with the control: it is merely present in the document).

这是有意为之还是只是一个错误?

Is this something by design or just a bug?

EDIT 2017-06-13:此处描述的错误似乎已部分修复,但仍可重现.现在,只有在您使用这些模板之一(如空白文档)创建新文档之前打开了 Word 实例时,才会发生这种情况.

EDIT 2017-06-13: The bug described here appear to be partially fixed, but it is still reproducible. Now it happens only if there is an instance of Word opened BEFORE you create a new document using one of these templates (like a blank document).

推荐答案

这是一个错误.

根据 MSDN:

Me 提供了一种引用正在执行代码的类的特定实例的方法.

Me provides a way to refer to the specific instance of the class where the code is executing.

对我来说,这意味着它应该始终是新文档.确保您没有意外运行模板中的代码.

To me this means that it should always be the new document. Ensure that you aren't running the code in the template by accident.

来源:https://msdn.microsoft.com/en-us/vba/language-reference-vba/articles/me-keyword

这篇关于Me 关键字的行为不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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