Cannot iterate through Shapes collection
C# and word interop, I have a word document with some textboxs (msoTextBox shapes), the problem that I can't iterate through the shapes collection with the code below :
foreach (Shape shape 开发者_如何学编程in WordDocument.Shapes)
{}
although when setting a breakpoint in the loop line I can see that WordDocument.Shapes.Count returns 4.
I note that textboxs are inserted using open xml sdk.
I've found there's a problem when textboxes are used. Take a look at this solution.
From 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);
}
}
From what you said, you look like you do the right thing.
Can you give us the Error StackTrace ?
PS : I know my question should have been in the comments, but it wouldn't have been readable :)
So,
Replace :
foreach (Shape shape in WordDocument.Shapes)
{
}
By:
foreach (Range rangeStory in WordDocument.StoryRanges)
{
foreach (Shape shape in rangeStory.ShapeRange)
{
}
}
It's work perfectly.
精彩评论