简体   繁体   English

具有.docm扩展名的word文档中的vba代码的说明

[英]Explanation of vba code in a word document having .docm extension

I have a Microsoft Word document with .docm format. 我有一个.docm格式的Microsoft Word文档。 A first glance it does not contain any macros (as when clicking the following on the ribbon; View -> Macros -> View macros pops up a window having an empty list). 乍一看它不包含任何宏(如在功能区上单击以下内容时; View - > Macros - > View macros弹出一个具有空列表的窗口)。

But when enabling the Developer ribbon tab, and clicking the Visual Basic icon there, and then selecting the Document and ContentControlonEnter from the dropdowns in the VB window the following code appears: 但是,当启用Developer功能区选项卡,然后单击其中的Visual Basic图标,然后从VB窗口的下拉列表中选择Document and ContentControlonEnter ,将显示以下代码:

Private Sub Document_ContentControlOnEnter(ByVal ContentControl As ContentControl)
Dim i As Long, j As Long
With ActiveDocument
  If ContentControl.Title = "Classification" Then
ContentControl.DropdownListEntries.Clear
For i = 1 To .ContentControls.Count
  If Left(.ContentControls(i).Title, 5) = "Level" Then
    j = j + 1
    ContentControl.DropdownListEntries.Add Text:=j & " - " & .ContentControls(i).Range.Text
  End If
Next
  End If
End With
End Sub

Selecting the other options in the dropdowns give only "blank" code (that is they contain only function declarations followed by the End keyword). 选择下拉列表中的其他选项仅提供“空白”代码(即它们仅包含函数声明,后跟End关键字)。


My question is what is the code meant to do? 我的问题是代码的意图是什么?


* *

  • Details: 细节:
  • The Word document in question contains hyperlinks to parts of the same document and a couple of links to Word files and Excel files of the same folder. 有问题的Word文档包含指向同一文档部分的超链接以及指向同一文件夹的Word文件和Excel文件的几个链接。 It also contains lots of content control boxes, which I'm guessing is the focus of the code (as the code contains the ContentControl keyword) 它还包含许多内容控制框,我猜测它是代码的焦点(因为代码包含ContentControl关键字)

Content controls can trigger macros when the user enters and exits them. 内容控件可以在用户输入和退出宏时触发宏。 Microsoft made the design decision that all content controls should trigger the same "events" - Document_ContentControlOnEnter / Document_ContentControlOnExit - and that the code in the event needs to check which content control was entered / exited. Microsoft做出了设计决定,即所有内容控件都应触发相同的“事件” - Document_ContentControlOnEnter / Document_ContentControlOnExit - 并且事件中的代码需要检查输入/退出的内容控件。

Content controls are considered as part of the Document because the Document can trigger events. 内容控件被视为文档的一部分,因为文档可以触发事件。 That's why they're in (and MUST be in) the ThisDocument class module. 这就是为什么他们在(并且必须在)ThisDocument 模块中。

(Note: View Macros can only show you PUBLIC SUB procedures with no arguments that are located in "normal" code modules. Any Private Sub, any Function, anything that takes a parameter and anything in a class module will not appear in that list. So you can't use that list to determine whether a document contains any code.) (注意:查看宏只能显示没有参数的PUBLIC SUB过程,这些过程位于“普通”代码模块中。任何私有子,任何函数,任何带参数的东西以及类模块中的任何内容都不会出现在该列表中。因此,您无法使用该列表来确定文档是否包含任何代码。)

The If ContentControl.Title = "Classification" Then checks which content control was entered. If ContentControl.Title = "Classification" Then检查输入了哪个内容控件。 (Note: it usually makes more sense to use Select Case rather than If, especially when the event needs to distinguis between multiple content controls.) What's inside the If only executes if it was a content control with the Title "Classification". (注意:使用Select Case而不是If通常更有意义,特别是当事件需要区分多个内容控件时。)If中的内容只有在具有Title“Classification”的内容控件时才会执行。 (Note that more than one content control can have the same Title, so more than one content control could run the code.) (请注意,多个内容控件可以具有相同的标题,因此可以运行多个内容控件。)

If another content control is entered, the event is still fired, but nothing happens (in this case). 如果输入了另一个内容控件,则仍会触发该事件,但不会发生任何事情(在本例中)。

Catalin Pop correctly explained that the code is, in essence, "resetting" the drop down list. Catalin Pop正确地解释了代码本质上是“重置”下拉列表。

Legacy Form fields use a similar pattern - macros can fire when the user enters/exits an form field. 旧版表单字段使用类似的模式 - 当用户输入/退出表单字段时,宏可以触发。 But the design for that was you had to create a Public Sub and assign that to the form field in the Properties. 但是为此设计的是你必须创建一个Public Sub并将其分配给Properties中的表单字段。

I think the logic here is quite simple. 我认为这里的逻辑非常简单。 Basically the code searches for a content control named Classification within the entire document. 基本上,代码在整个文档中搜索名为Classification的内容控件。 After it finds it, it clears all of its drowdown entries - like a reset. 找到它之后,它会清除所有的drowdown条目 - 比如重置。 After the cleaning part it again searches through the entire document for all content control that start with word "Level" and it collect the text for those controls and their order in appearance. 在清洁部分之后,它再次在整个文档中搜索以“Level”开头的所有内容控件,并收集这些控件的文本及其外观顺序。

With this info collected it then fills the dropdown optios for the classification control above. 收集此信息后,填写上面的分类控制的下拉选项。 (eg 1 Level X, 2 Level Y.. - based on what it finds in the document for controls starting with Level in their name) (例如,1级X,2级Y .. - 基于它在文档中找到的名称中以Level开头的控件)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM