ユーザーが Word ドキュメント内のさまざまなテキストを選択し、そのテキストをコンテンツ コントロール (リッチ テキスト) でラップするリボンのボタンをクリックできるようにする Word アドインを作成しています。最終的に、これらのコンテンツ コントロールは XML にマップされます。
これまでのコードは次のようになります。
public partial class Ribbon1
{
private RichTextContentControl titleRichTextControl;
private void Ribbon1_Load(object sender, RibbonUIEventArgs e)
{
}
private void addTitle_Click(object sender, RibbonControlEventArgs e)
{
AddRichTextControlAtSelection();
}
private void AddRichTextControlAtSelection()
{
word.Document currentDocument = Globals.ThisAddIn.Application.ActiveDocument;
currentDocument.ActiveWindow.Selection.Range.Select();
Document extendedDocument = Globals.Factory.GetVstoObject(currentDocument);
titleRichTextControl = extendedDocument.Controls.AddRichTextContentControl("titleRichTextControl");
titleRichTextControl.PlaceholderText = "Enter the title";
titleRichTextControl.Title = "Title";
titleRichTextControl.Tag = "title";
}
}
これはすべて問題なく、ボタンが初めてクリックされたときに機能します。ただし、追加が必要な「タイトル」(この場合) が複数あり、ユーザーがもう一度ボタンを押すと、次のエラーがスローされます。
The control cannot be added because a control with the name titleRichTextControl already exists in the Controls collection.
不平を言う理由は明らかですが、ボタンを複数回クリックして、同じタイプ(リッチテキストコンテンツコントロール)と同じ名前(たとえば「タイトル」)の複数のコンテンツコントロールを生成できるようにする正しい方法が思いつきません)。
誰でも私を正しい方向に向けることができますか?