0

チェック ボックス コントロールを含む Word 2013 文書があります。このチェックボックスのTagプロパティをfooCheckBox次のように設定しました。

Word 2013 チェックボックスのプロパティを示すスクリーンショット

ここで、Open XML SDK 2.5 を使用して、その特定のチェックボックスをプログラムで見つけて操作したいと思います。SdtContentCheckBoxチェックボックスを検索/列挙する方法は知っていますが、Tagプロパティで特定のものを見つける方法がわかりません:

が与えられた場合、 Tagプロパティを使用して特定のWordprocessingDocument docを取得するにはどうすればよいですか?SdtContentCheckBox

(私は答えとして投稿している作業コードを持っています(以下を参照)。ただし、これが正しい方法であるかどうかはわかりません。したがって、誰かがより適切で適切な方法を知っている場合は、やり方を見てください。)

4

1 に答える 1

3

どうやら、SdtContentCheckBoxオブジェクトの.Parentプロパティは、子孫SdtPropertyを照会できるコレクションを参照しているようです。Tag

このオブジェクト モデリングの背後にあるロジックはわかりませんが、これを使用して作業を完了できます。

// using DocumentFormat.OpenXml.Packaging;
// using System.Diagnostics;
// using System.Linq;

SdtContentCheckBox TryGetCheckBoxByTag(WordprocessingDocument doc, string tag)
{
    foreach (var checkBox in doc.MainDocumentPart.Document.Descendants<SdtContentCheckBox>())
    {
        var tagProperty = checkBox.Parent.Descendants<Tag>().FirstOrDefault();
        if (tagProperty != null)
        {
            Debug.Assert(tagProperty.Val != null);
            if (tagProperty.Val.Value == tag)
            {
                // a checkbox with the given tag property was found
                return checkBox;
            }
        }
    }
    // no checkbox with the given tag property was found
    return null;
}
于 2015-06-15T15:35:56.773 に答える