【问题标题】:Get Paragraph in Word document获取Word文档中的段落
【发布时间】:2015-11-13 17:09:22
【问题描述】:

我以编程方式在 Word 文档中创建了一个图表。

嗯,ToF 样式居中,我希望它保持缩进。 为此(设置段落缩进),我必须获取 ToF 所在的段落。

这是 r 访问 ToF 的方式:

wordApp.ActiveDocument.TablesOfFigures[1]

有什么想法吗?

【问题讨论】:

  • 你想选择一个答案吗?

标签: c# ms-word


【解决方案1】:

试试下面的代码。假设 TablesOfFigures[1] 存在(否则会出现缓冲区溢出)。

// Check in which paragraph TablesOfFigures[1] is found
for (int i=1; i <= wordApp.ActiveDocument.Paragraphs.Count; i++)
{
    if (IsInRange(wordApp.ActiveDocument.TablesOfFigures[1].Range, wordApp.ActiveDocument.Paragraphs[i].Range))
    {
        MessageBox.Show("ToF is in paragraph " + i);
    }

}

// Returns true if 'target' is contained in 'source'
private bool IsInRange(Range target, Range source)
{
    return target.Start >= source.Start && target.End <= source.End;
}

【讨论】:

  • 感谢您的回复。我尝试了代码,但找不到 ToF(绝对确定已定义)!有什么想法吗?
  • 这很奇怪.. 如果你没问题,也许你可以上传你正在处理的 Word 文件(或至少一个类似的文件),所以我可以看看。
  • 该文件是我通过代码打开到 Word 应用程序的现有文件,并通过附加已定义 ToF 的模板来添加 ToF。这些细节会发现重点吗?
【解决方案2】:

如果你只有一张图表,你可以试试这个:

 With wordApp.ActiveDocument.TablesOfFigures(1).Range

     'Setting the indent
     .ParagraphFormat.LeftIndent = CentimetersToPoints(1)

 End With

我只使用 Word 对其进行了测试,它会选择图表然后将其缩进 1 厘米

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    • 2019-11-06
    • 2016-10-02
    • 2022-01-04
    相关资源
    最近更新 更多