首页 > 解决方案 > WdInformation.wdVerticalPositionRelativeToPage 在 VBA 和 C# 中给出不同的值

问题描述

我正在使用 Microsoft.Office.Interop.Word 将一段用 VBA 编写的代码翻译成 C# 我观察到, 在 C# 中,与在其中p是 Paragraph 对象p.Range.Information[WdInformation.wdVerticalPositionRelativeToPage] 的 VBA 中使用时相比,它给出了不同的值 。p.Range.Information(wdVerticalPositionRelativeToPage)

每当 Word 文档页面上出现形状时,就会发生这种情况。我想知道为什么会这样?当通过 C# 与 VBA 打开文档时,文档的呈现有什么不同吗?

PS - 在这两种情况下,单词可见性都设置为 false。

编辑:更新 - 我尝试将可见性设置为 true,并且在使用 C# 与使用 VBA 启动 Document 时,我确实观察到文档呈现的差异。我该如何解决?

标签: comoffice-interop

解决方案


So I figured out the problem. My code was trying to access header section like this

foreach (Section sect in objDoc.Sections)
        {
            foreach (HeaderFooter hf in sect.Headers)
            {
                someFunction(hf.Range.Paragraphs, false, false);
                if (hf.Shapes.Count > 0) // to handle shapes in Headers
                {
                    someFunction(hf.Shapes);
                }
            }
        }

For some reason, accessing headers changes the position of paragraphs in rendering causing the different values. To fix this, I now access headers as the last piece in my code. Hope this helps somebody.


推荐阅读