无法遍历Shapes集合

问题描述 投票:2回答:3

C#和word interop,我有一个带有一些文本框(msoTextBox形状)的word文档,我无法使用下面的代码遍历形状集合的问题:

foreach (Shape shape in WordDocument.Shapes)
        {}

虽然在循环行中设置断点时我可以看到WordDocument.Shapes.Count返回4。

我注意到使用open xml sdk插入了文本框。

ms-word interop vsto
3个回答
2
投票

我发现使用文本框时会出现问题。看看这个solution


0
投票

来自Code Project

    // Get the word count from all shapes
    foreach (Word.Shape shape in wordDocument.Shapes)
    {
        if (shape.TextFrame.HasText < 0)
        {
            count+=GetCountFromRange(shape.TextFrame.TextRange,wordDocument,word);
        }
    }

从你说的话来看,你看起来像是做对了。

你能给我们Error StackTrace吗?

PS:我知道我的问题应该在评论中,但它不会是可读的:)


0
投票

所以,

替换:

foreach (Shape shape in WordDocument.Shapes)
   {
   }

通过:

     foreach (Range rangeStory in WordDocument.StoryRanges)
     {
        foreach (Shape shape in rangeStory.ShapeRange)
        {

        }
     }

这很完美。

© www.soinside.com 2019 - 2024. All rights reserved.