c#Word Interop - 设置段落缩进 [英] c# Word Interop - Setting paragraph indentation
问题描述
我要求以编程方式(在C#中)打开或关闭特定段落的悬挂缩进。
我创建了一个添加,单击一个按钮,执行我(尝试)执行此操作的代码。它是一个切换,所以第一次点击添加悬挂缩进,第二次点击应该删除它。
简而言之,它在段落>缩进,然后设置特殊等于 无或 挂。
Img1 [ ^ ]
我的最好的尝试是使用以下代码:
I have a requirement to programatically (in C#) either switch on or off the hanging indent of a particular paragraph.
I have created an add in, with a button that when clicked, executes code where I (attempt) to do this. Its a toggle, so first click adds the Hanging indent and second click should remove it.
In word, its the setting in Paragraph>Indentation, followed by the setting "Special" equal to None or Hanging.
Img1[^]
My best attempt at this is with the following code:
foreach (Footnote rngWord in Globals.OSAXWord.Application.ActiveDocument.Content.Footnotes)
rngWord.Range.ParagraphFormat.TabHangingIndent(
rngWord.Range.ParagraphFormat.FirstLineIndent == 0 ? 1 : -1);
由于某种原因,它只修改段落中的最后一行。我需要它是除了第一个之外的所有行。我做错了什么?
Img2 [ ^ ]
注意 - 我实际上是在我的文档中的脚注上执行此操作。
It ONLY amends the last line in the paragraph for some reason. I need it to be all lines which hang except the very first. What am I doing wrong?
Img2[^]
Note - I'm actually performing this on footnotes in my document.
推荐答案
对于需要此解决方案的任何人:
To anyone who needs this solution:
try
{
// Loop through each footnote in the word doc.
foreach (Footnote rngWord in Microsoft.Office.Interop.Word.Application.ActiveDocument.Content.Footnotes)
{
// For each paragraph in the footnote - set the indentation.
foreach (Paragraph parag in rngWord.Range.Paragraphs)
{
// If this was not previously indented (i.e. FirstLineIndent is zero), then indent.
if (parag.Range.ParagraphFormat.FirstLineIndent == 0)
{
// Set hanging indent.
rngWord.Range.ParagraphFormat.TabHangingIndent(1);
}
else
{
// Remove hanging indent.
rngWord.Range.ParagraphFormat.TabHangingIndent(-1);
}
}
}
// Force the screen to refresh so we see the changes.
Microsoft.Office.Interop.Word.Application.ScreenRefresh();
}
catch (Exception ex)
{
// Catch any exception and swollow it (i.e. do nothing with it).
//MessageBox.Show(ex.Message);
}
这篇关于c#Word Interop - 设置段落缩进的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!