无法迭代 Shapes 集合

发布于 2024-10-09 11:18:27 字数 251 浏览 0 评论 0原文

C# 和单词互操作, 我有一个带有一些文本框(msoTextBox 形状)的 Word 文档,问题是我无法使用下面的代码迭代形状集合:

foreach (Shape shape in WordDocument.Shapes)
        {}

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

我注意到文本框是使用 open xml sdk 插入的

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.

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

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

发布评论

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

评论(3

黯然#的苍凉 2024-10-16 11:18:27

我发现使用文本框时存在问题。看看这个 解决方案

I've found there's a problem when textboxes are used. Take a look at this solution.

长发绾君心 2024-10-16 11:18:27

来自代码项目

    // 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);
        }
    }

从你所说的来看,你看起来像你做正确的事。

您能给我们错误 StackTrace 吗?

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

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 :)

故人如初 2024-10-16 11:18:27

因此,

将 : 替换

foreach (Shape shape in WordDocument.Shapes)
   {
   }

为:

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

        }
     }

效果很好。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文