【发布时间】:2015-11-13 17:09:22
【问题描述】:
我以编程方式在 Word 文档中创建了一个图表。
嗯,ToF 样式居中,我希望它保持缩进。 为此(设置段落缩进),我必须获取 ToF 所在的段落。
这是 r 访问 ToF 的方式:
wordApp.ActiveDocument.TablesOfFigures[1]
有什么想法吗?
【问题讨论】:
-
你想选择一个答案吗?
我以编程方式在 Word 文档中创建了一个图表。
嗯,ToF 样式居中,我希望它保持缩进。 为此(设置段落缩进),我必须获取 ToF 所在的段落。
这是 r 访问 ToF 的方式:
wordApp.ActiveDocument.TablesOfFigures[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;
}
【讨论】:
如果你只有一张图表,你可以试试这个:
With wordApp.ActiveDocument.TablesOfFigures(1).Range
'Setting the indent
.ParagraphFormat.LeftIndent = CentimetersToPoints(1)
End With
我只使用 Word 对其进行了测试,它会选择图表然后将其缩进 1 厘米
【讨论】: