簡體   English   中英

VBA 將多個內容控件添加到 Word 中的單個表格單元格

[英]VBA to Add Multiple Content Controls to a Single Table Cell in Word

我需要使用 VBA 將多個內容控件和附加文本添加到 word 中的單個表格單元格中。 這是我需要的一個例子:

<CC1>Moby Dick</CC1> 已被 <CC2>2</CC2> 人閱讀,平均得分為 <CC3>3</CC3>(滿分 5 分)

我知道我可以使用以下語法添加單個內容控件:

With rng.ContentControls.Add(wdContentControlRichText)
 .title = "Book Name"
 .Tag = "title"
End With

如果我的范圍設置為單元格,我將如何使以下偽代碼工作:

Set rng = objDoc.Tables(1).Cell(2, 1).Range

With rng.ContentControls.Add(wdContentControlRichText)
 .title = "Book Name"
 .Tag = "title"
End With

" has been read by "

With rng.ContentControls.Add(wdContentControlRichText)
 .title = "People Count"
 .Tag = "count"
End With

" people who have given it an average score of "

With rng.ContentControls.Add(wdContentControlRichText)
 .title = "Score"
 .Tag = "score"
End With

" out of 5"

這是一些實際的代碼。 它將第三個 insertafter 和內容控件放在第二個 insertafter 和第二個內容控件之間

With rng.ContentControls.Add(wdContentControlRichText)
    .title = "Asset ID"
    .Tag = "asset_id"
End With
rng.InsertAfter " | Rev. "
rng.Collapse Direction:=wdCollapseEnd
rng.Move wdCharacter, -1
With rng.ContentControls.Add(wdContentControlRichText)
    .title = "Revison Number"
    .Tag = "revision_num"
End With
rng.InsertAfter " | Effective Date: "
rng.Collapse Direction:=wdCollapseEnd
rng.Move wdCharacter, -1
With rng.ContentControls.Add(wdContentControlRichText)
    .title = "Effective Date"
    .Tag = "effective_date"
End With

在此處輸入圖像描述

嘗試這個:

   Set rng = objDoc.Tables(1).Cell(2, 1).Range
   rng.Collapse wdCollapseStart
   With rng.ContentControls.Add(wdContentControlRichText)
      .Title = "Effective Date"
      .Tag = "effective_date"
      .SetPlaceholderText Text:="Effective date"
   End With
   rng.Text = " | Effective Date: "
   rng.Collapse wdCollapseStart
   With rng.ContentControls.Add(wdContentControlRichText)
      .Title = "Revison Number"
      .Tag = "revision_num"
      .SetPlaceholderText Text:="Rev Num"
   End With
   rng.Text = " | Rev. "
   rng.Collapse wdCollapseStart
   With rng.ContentControls.Add(wdContentControlRichText)
      .Title = "Asset ID"
      .Tag = "asset_id"
      .SetPlaceholderText Text:="Asset ID"
   End With

暫無
暫無

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

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