Click here to Skip to main content
15,892,737 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

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


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.
Posted

1 solution

To anyone who needs this solution:

C#
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);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900