COM Word——>获取所有ContentControls

本文关键字:ContentControls Word COM 获取 | 更新日期: 2023-09-27 18:15:37

我想获得一个Word-Document的所有ContentControls。目前,我使用以下函数来完成此操作。

    private static List<ContentControl> GetAllContentControls(Document wordDocument)
    {
        if (null == wordDocument)
            throw new ArgumentNullException("wordDocument");
        List<ContentControl> ccList = new List<ContentControl>();
        Range rangeStory;
        foreach (Range range in wordDocument.StoryRanges)
        {
            rangeStory = range;
            do
            {
                try
                {
                    foreach (ContentControl cc in rangeStory.ContentControls)
                    {
                        ccList.Add(cc);
                    }
                }
                catch (COMException) { }
                rangeStory = rangeStory.NextStoryRange;
            }
            while (rangeStory != null);
        }
        return ccList;
    }

问题是要在文本字段中获得内容控件

COM Word——>获取所有ContentControls

我明白了

    private static List<ContentControl> GetAllContentControls(Document wordDocument)
    {
        if (null == wordDocument)
            throw new ArgumentNullException("wordDocument");
        List<ContentControl> ccList = new List<ContentControl>();
        Range rangeStory;
        foreach (Range range in wordDocument.StoryRanges)
        {
            rangeStory = range;
            do
            {
                try
                {
                    foreach (ContentControl cc in rangeStory.ContentControls)
                    {
                        ccList.Add(cc);
                    }
                    foreach (Shape shapeRange in rangeStory.ShapeRange)
                    {
                        foreach (ContentControl cc in shapeRange.TextFrame.TextRange.ContentControls)
                        {
                            ccList.Add(cc);
                        }
                    }
                }
                catch (COMException) { }
                rangeStory = rangeStory.NextStoryRange;
            }
            while (rangeStory != null);
        }
        return ccList;
    }