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.
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);
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?
Modifications:
Note - I'm actually performing this on footnotes in my document.
For anyone who may happen to come across this - here's how I resolved:
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);
}