English 中文(简体)
如何将所有内容控制清单列入该文件?
原标题:How to get the list of all content controls in the document?

我正在使用Interop,我希望获得文字文件(身体、形状、头 head)所载所有内容管制清单。 这样做的正确和最佳方式是:

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

  List<ContentControl> ccList = new List<ContentControl>(); ;
  // Body cc
  var inBodyCc = (from r in wordDocument.ContentControls.Cast<ContentControl>()
          select r);
  ccList.AddRange(inBodyCc);

  // cc within shapes
  foreach (Shape shape in wordDocument.Shapes)
  {
    if (shape.Type == Microsoft.Office.Core.MsoShapeType.msoTextBox)
    {
      ccList.AddRange(WordDocumentHelper.GetContentControlsInRange(shape.TextFrame.TextRange));
    }
  }

  // Get the list of cc in the story ranges : wdFirstPageHeaderStory, wdFirstPageFooterStory, wdTextFrameStory (textbox)... 
  foreach (Range range in wordDocument.StoryRanges)
  {
    ccList.AddRange(WordDocumentHelper.GetContentControlsInRange(range));
  }
  return ccList;
}

public static List<ContentControl> GetContentControlsInRange(Range range)
{
  if (null == range)
    throw new ArgumentNullException("range");

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

  foreach (ContentControl cc in range.ContentControls)
  {
    returnValue.Add(cc);
  }

  return returnValue;
}

在这方面。

最佳回答

这里的路程要短得多(VBA),但可以进入C#:

Sub GetCCs()
    Dim d As Document
    Set d = ActiveDocument
    Dim cc As ContentControl
    Dim sr As Range
    Dim srs As StoryRanges
    For Each sr In d.StoryRanges
        For Each cc In sr.ContentControls
              # do your thing
        Next
    Next
End Sub
问题回答

是 拉斯 Hol、you是正确的, head子和脚 foot中文本箱内的内容控制缺失。

/// <summary>
/// Get all content controls contained in the document.
/// </summary>
/// <param name="wordDocument"></param>
/// <returns></returns>
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 range.ContentControls)
                {
                    ccList.Add(cc);
                }

                // Get the content controls in the shapes ranges
                foreach (Shape shape in range.ShapeRange)
                {
                    foreach (ContentControl cc in shape.TextFrame.TextRange.ContentControls)
                    {
                        ccList.Add(cc);
                    }

                }
            }
            catch (COMException) { }
            rangeStory = rangeStory.NextStoryRange;

        }
        while (rangeStory != null);
    }
    return ccList;
}

关于

这样做的正确方式是:

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);
  }
      }
      catch (COMException) { }
      rangeStory = rangeStory.NextStoryRange;

    }
    while (rangeStory != null);
  }
  return ccList;
}

关于





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签