VBA Word 动态插入文本 - ContentControl 有问题 - 替代方案?

VBA Word Insert text dynamically - Problems with ContentControl - Alternatives?

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

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

字段的内容来自用户窗体文本框。文本框命名为 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 文本字段;文本框(形状)可能是另一种选择,但它们可能有自己的缺点。

令人沮丧的是,内容控制字段的行为如此怪异,看似非常简单 straight-forward 却如此复杂(至少对我而言)。

编辑:修正了标题中的拼写错误。

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

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