VSTO查找Word文档的ContentControls

发布于 2024-08-08 01:13:42 字数 200 浏览 1 评论 0原文

有没有办法使用 VSTO 查找 WordDocument 的所有 ContentControl(包括页眉、页脚、文本框...中的 ContentControl)?

Microsoft.Office.Tools.Word.Document.ContentContols 仅返回主文档的 ContentControl,而不返回页眉/页脚内的 ContentControl。

Is there a way to find all ContentControls of a WordDocument (including ContentControls in Headers, Footers, TextBoxes ...) using VSTO?

Microsoft.Office.Tools.Word.Document.ContentContols returns only the ContentControls of the Main-Document, not the one inside the Headers/Footer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

合久必婚 2024-08-15 01:13:42

复制自 http://social. msdn.microsoft.com/Forums/is/vsto/thread/0eb0af6f-17db-4f98-bc66-155db691fd70

public static List<ContentControl> GetAllContentControls(Document wordDocument)
    {
      if (null == wordDocument)
        throw new ArgumentNullException("wordDocument");

      List<ContentControl> ccList = new List<ContentControl>();

      // The code below search content controls in all
      // word document stories see http://word.mvps.org/faqs/customization/ReplaceAnywhere.htm
      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;
    }

Copied from http://social.msdn.microsoft.com/Forums/is/vsto/thread/0eb0af6f-17db-4f98-bc66-155db691fd70

public static List<ContentControl> GetAllContentControls(Document wordDocument)
    {
      if (null == wordDocument)
        throw new ArgumentNullException("wordDocument");

      List<ContentControl> ccList = new List<ContentControl>();

      // The code below search content controls in all
      // word document stories see http://word.mvps.org/faqs/customization/ReplaceAnywhere.htm
      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;
    }
日记撕了你也走了 2024-08-15 01:13:42

试试这个:

foreach (Word.ContentControl contentcontrol in this.Application.ActiveDocument.ContentControls)
{
    //Some action on all contentcontrol objects
}

如果这不起作用,请尝试迭代文档的 故事范围

Try this:

foreach (Word.ContentControl contentcontrol in this.Application.ActiveDocument.ContentControls)
{
    //Some action on all contentcontrol objects
}

If that doesn't work try to iterate on all ranges (for contentcontrols) in a document's StoryRanges

最后的乘客 2024-08-15 01:13:42

我正在处理同样的问题,但从 MATLAB 驱动 Word。 Word MVP 的此页面为我解决了问题:

http://www. word.mvps.org/FAQs/MacrosVBA/FindReplaceAllWithVBA.htm

本质上,您必须:

  1. 循环所有 Document.StoryRanges 以获取每种故事类型的第一个范围。
  2. 在每个范围内,在 range.ContentControls 上完成您的工作。
  3. 范围 = 范围.NextStoryRange.
  4. 重复2-4,直到范围为空。

I'm dealing with the same problem, but driving Word from MATLAB. This page by a Word MVP solved the problem for me:

http://www.word.mvps.org/FAQs/MacrosVBA/FindReplaceAllWithVBA.htm

Essentially, you have to:

  1. Loop over all Document.StoryRanges to get the first range of each story type.
  2. Within each range, do your job on range.ContentControls.
  3. range = range.NextStoryRange.
  4. Repeat 2-4 until range is empty.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文