【问题标题】:VBA Word Insert text dynamically - Problems with ContentControl - Alternatives?VBA Word 动态插入文本 - ContentControl 的问题 - 替代方案?
【发布时间】:2021-05-04 21:52:21
【问题描述】:

在我的 Word VBA 项目中,我在 ContentControl(s) 方面遇到了很多困难。 有许多内容控制文本字段都具有相同的名称(它们具有相同的名称,因为一开始所需字段的总数是未知的,所以我根据需要多次复制和粘贴这些字段)。现在我想遍历内容控制字段并根据各个项目的索引更改字段的名称(例如文档中的第一个字段=“One”,文档中的第二个字段=“two”等等) .

但是,正如其他线程中提到的,内容控制元素的索引与其在文档中的位置不对应(我不知道它对应于什么)。 因此,我没有按顺序排列字段,而是得到例如"四" --> "一" --> "三" --> "二"(或任何其他可能的组合)。

字段的内容来自用户窗体文本框。文本框被命名为 Text_Box_1 到 Text_Box_4:

Private Sub Test() 'Note: The actual code is more complex, this is just to demonstrate my problem.

Dim i As Integer

UserForm1.TextBox1 = "one"
UserForm1.TextBox2 = "two"
UserForm1.TextBox3 = "three"
UserForm1.TextBox4 = "four"    

For i = 1 To 4 - 1 'Since there are four text boxes in the UserForm in this example, the text snippet containing the text field gets copied and pasted three times; Note: Here the number of textboxes is pre-determined and fixed, in the actual project, it is variable.
    ActiveDocument.Bookmarks(Index:="Copy").Range.Copy '
    ActiveDocument.Bookmarks(Index:="Paste").Range.Paste
Next i

For i = 1 To 4 'This code is supposed to loop through the four content control text fields and insert text from the corresponding UserForm text box. However, content control text field 1, unfortunately does no correspond to UserForm.TextBox1 for some reason.
    ActiveDocument.SelectContentControlsByTitle("Number").Item(i).Range.Text = UserForm1.Controls("TextBox" & i)
Next i

End Sub

Before running the code

After runnning the code

有没有办法以正确的顺序命名内容控制字段? 如果不是,那将是实现我的目标的另一种方法。 我认为遗留文本字段不是一种选择,因为必须保护文档;我没有过多地研究 ActiveX 文本字段。文本框(形状)可能是另一种选择,但它们可能有自己的缺点。

内容控制字段的行为如此怪异,而看似非常简单直接的事情却如此复杂(至少对我而言),这真的令人沮丧。

编辑:修正了标题中的错字。

【问题讨论】:

标签: vba ms-word word-contentcontrol


【解决方案1】:

我会在我的例程中插入所需的文本和内容控件,而不是使用复制和粘贴,就像这样。

Private Sub Test()

   Dim i As Integer

   UserForm1.TextBox1 = "one"
   UserForm1.TextBox2 = "two"
   UserForm1.TextBox3 = "three"
   UserForm1.TextBox4 = "four"
   
   Dim cc As ContentControl
   Dim rng As Range
   Dim ccLocation  As Range

   For i = 4 To 1 Step -1  'Insert in reverse order to ensure that they are correct in the document
      Set rng = ActiveDocument.Bookmarks("Paste").Range
      rng.InsertAfter Text:="Number: "
      rng.Collapse wdCollapseEnd
      Set ccLocation = rng.Duplicate
      rng.InsertAfter vbCr & "----------------------------------------" & vbCr
      Set cc = ccLocation.ContentControls.Add(wdContentControlText)
      cc.Range.Text = UserForm1.Controls("TextBox" & i).Text
      cc.Title = "Number" & i
   Next i

End Sub

如果您无法删除现有内容并且必须使用现有内容,那么您可以使用以下内容:

Private Sub Test()

   Dim i As Integer

   UserForm1.TextBox1 = "one"
   UserForm1.TextBox2 = "two"
   UserForm1.TextBox3 = "three"
   UserForm1.TextBox4 = "four"
   
   ActiveDocument.SelectContentControlsByTitle("Number").Item(i).Range.Text = UserForm1.Controls("TextBox1").Text

   Dim cc As ContentControl
   Dim rng As Range
   Dim ccLocation  As Range

   For i = 4 To 2 Step -1  'Insert in reverse order to ensure that they are correct in the document
      Set rng = ActiveDocument.Bookmarks("Paste").Range
      rng.InsertAfter Text:="Number: "
      rng.Collapse wdCollapseEnd
      Set ccLocation = rng.Duplicate
      rng.InsertAfter vbCr & "----------------------------------------" & vbCr
      Set cc = ccLocation.ContentControls.Add(wdContentControlText)
      cc.Range.Text = UserForm1.Controls("TextBox" & i).Text
      cc.Title = "Number" & i
   Next i

End Sub

【讨论】:

  • 感谢您的回答。在最终文档中实际上有两个内容控制字段(由于布局:每个页面包含两个段落,每个段落都有一个内容控制字段)所以我必须考虑您的方法是否可行。不过,手动添加内容控件的想法可能是我可以使用的。
  • 我不太清楚代码,我不敢说。你能向我解释一下“rng.Collapse wdCollapseEnd”这一行的作用吗?它是否将焦点设置到刚刚插入的内容的末尾(在“数字:”之后)?那里有什么 Duplicate?为什么它必须在这个特定的位置?感谢您的所有努力!
  • 如果有您不熟悉的属性或方法,建议您在在线帮助中查找。 Duplicate 用于创建范围的独立副本,以便将内容控件插入正确的位置。 CollapseRange 的起点和终点设置为刚刚插入文本的末尾。
  • 我查了一下,但我不完全理解复制范围的含义。如果我现在理解正确,这意味着当前范围(在最后一次插入的末尾)被复制,以便以后可以在原始范围因插入附加文本而更改时访问它。谢谢你的帮助!我会在接下来的几天里对代码进行更多测试。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-28
  • 1970-01-01
  • 2014-04-06
相关资源
最近更新 更多