簡體   English   中英

VBA Word 動態插入文本 - ContentControl 的問題 - 替代方案?

[英]VBA Word Insert text dynamically - Problems with ContentControl - Alternatives?

在我的 Word VBA 項目中,我在 ContentControl 方面遇到了很多困難。 有許多內容控制文本字段都具有相同的名稱(它們具有相同的名稱,因為一開始所需字段的總數是未知的,因此我根據需要多次復制和粘貼這些字段)。 現在我想遍歷內容控制字段並根據各個項目的索引更改字段的名稱(例如文檔中的第一個字段=“One”,文檔中的第二個字段=“two”等等) .

但是,正如其他線程中提到的,內容控制元素的索引與文檔中的 position 不對應(我不知道,它對應於什么)。 因此,我沒有按順序獲取字段,而是得到例如“四”->“一”->“三”->“二”(或任何其他可能的組合)。

字段的內容來自用戶窗體文本框。 文本框命名為 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

在運行代碼之前

運行代碼后

有沒有辦法以正確的順序命名內容控制字段? 如果不是,那將是實現我的目標的另一種方法。 我認為遺留文本字段不是一種選擇,因為必須保護文檔; 我沒有過多地研究 ActiveX 文本字段。 文本框(形狀)可能是另一種選擇,但它們可能有其自身的缺點。

內容控制字段的行為如此怪異,而看似非常簡單直接的事情卻如此復雜(至少對我而言),這真的令人沮喪。

編輯:修正了標題中的錯字。

我會在我的例程中插入所需的文本和內容控件,而不是使用復制和粘貼,就像這樣。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM