18

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

  • 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:

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 content is

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

while WithoutActiveX.dotm content is

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

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

Diego Queiroz
  • 3,198
  • 1
  • 24
  • 36
  • What is the difference in the documents? This seems like a question to ask the developers (i.e. Microsoft) - or possibly on SuperUser? – JustinJDavies Nov 28 '13 at 21:21
  • The difference is that one document has a button and the other doesn't. If this is something related with the design of the language, here is the right place to ask. But I am afraid this is only a bug, or a non documented side effect of using ActiveX controls. – Diego Queiroz Nov 29 '13 at 22:04
  • I meant, what is the difference with respect to "If you create a new file from these templates, you'll realize the results are different." You already stated that the button is the different in the templates. – JustinJDavies Nov 29 '13 at 22:07
  • My fault. I edited the question to clarify. The string produced in the documents by the same code are different because "Me" seems to be pointing to different objects. – Diego Queiroz Nov 30 '13 at 16:56
  • I can't reproduce this with files of my own creation under Word 2013, as of June 2016. – ThunderFrame Jun 12 '17 at 23:35
  • @ThunderFrame you are right, the problem described here appear to be partially fixed, but it is still possible to reproduce. If there is no instance of Word opened when you open the file, they present the same behavior as expected. But if there is an instance of Word opened (e.g. a Blank document) before you create the template, they behave differently. I just tested it with Office 365. – Diego Queiroz Jun 14 '17 at 01:25

1 Answers1

1

That is a bug.

According to MSDN:

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.

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

HackSlash
  • 4,944
  • 2
  • 18
  • 44