首页 文章

c#Word Interop - 设置段落缩进

提问于
浏览
0

我要求以编程方式(在C#中)打开或关闭特定段落的悬挂缩进 .

我创建了一个添加,带有一个按钮,当单击时,执行代码,我(尝试)执行此操作 . 它是一个切换,所以首先单击添加悬挂缩进,第二次单击应删除它 .

单词,它在 Paragraph>Indentation 中的设置,然后设置“ Special ”等于 NoneHanging .

enter image description here

我最好的尝试是使用以下代码:

foreach (Footnote rngWord in Globals.OSAXWord.Application.ActiveDocument.Content.Footnotes)
    rngWord.Range.ParagraphFormat.TabHangingIndent(
        rngWord.Range.ParagraphFormat.FirstLineIndent == 0 ? 1 : -1);

由于某种原因,它只修改段落中的最后一行 . 我需要它是除了第一个之外的所有线 . 我究竟做错了什么?

修改:

enter image description here

注意 - 我实际上是在我的文档中的脚注上执行此操作 .

1 回答

  • 2

    对于任何碰巧遇到这种情况的人 - 这是我如何解决的:

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

相关问题