Word 2007 set paragraph formatting Hanging indents

diana
3StarLounger
Posts: 279
Joined: 01 Jun 2010, 00:27

Word 2007 set paragraph formatting Hanging indents

Post by diana »

Vb.net Does anyone know the property for Paragraph hanging indent.

Due to conflict with heading styles and restart numbering, the current paragraph loses its indentation.
Therefore my code checks if the selected paragraph is a specific heading. If yes then
set left indentation
set hanging indent

Code: Select all

                       If worddoc.Template.TemplateType = WordTemplate.TemplateTypes.Advice Then

                        Dim paraThis As MSWord.Paragraph
                        For Each paraThis In worddoc.Document.Application.Selection.Paragraphs

                            Dim paraStyle As MSWord.Style = CType(worddoc.Library.Application.Application.Selection.Paragraphs.Style, Microsoft.Office.Interop.Word.Style)
                            Dim style2 As MSWord.Style = worddoc.GetStyle("Normal")

                            If InStr(1, paraStyle.NameLocal, "Page 1 Para number", CompareMethod.Text) > 0 Then
                                paraThis.LeftIndent = (0)
                                paraThis.FirstLineIndent = (0.65)
                            End If

                            If InStr(1, paraStyle.NameLocal, "Page 1 (i)", CompareMethod.Text) > 0 Then
                                paraThis.LeftIndent = (0.65)
                                paraThis.FirstLineIndent = (0.85)
                            End If

                        Next
Endif


Using the code the results are for heading style "Page 1 Para number".
The paragraph formatted as:
Left indentation = 0.65cm
FirstLine = 0.02cm
Tab stops = 0.65

The results are for heading style "Page 1 Para number"
The paragraph formatted as:
Left indentation = 1.5cm
FirstLine = 0.03cm

Tab stops = -2.28cm


Im sure I had this issue in the past and cant remember the solution, as it appears Hanging indent isn't as obvious as Hanging indent :groan:


TIA dd

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Word 2007 set paragraph formatting Hanging indents

Post by Rudi »

Hi Diana,

Not sure if you tried this, but in a recorded macro in Word, I notice it does a full left indent first, and then reverses the first line indent.
Would this resolve it for you?

the code is normal Word VBA (not sure if it needs tweaking in VB.Net)

Code: Select all

    With Selection.ParagraphFormat
        .LeftIndent = CentimetersToPoints(1.27)
        .FirstLineIndent = CentimetersToPoints(-1.27)
    End With
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

diana
3StarLounger
Posts: 279
Joined: 01 Jun 2010, 00:27

Re: Word 2007 set paragraph formatting Hanging indents

Post by diana »

Thank you Rudi

.FirstLine property is the FirstLine indent

FirstLine indent is a different paragraph indent to Hanging indent

I was wanting Hanging Indent. I dont know why Handing Indent is available throught the Paragraph dialog box however not available via code automation :scratch:

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Word 2007 set paragraph formatting Hanging indents

Post by Rudi »

I still could be barking up the wrong tree, but if there is no Hanging Indent available in the properties list, I was suggesting to use the FirstLine property with a negative value.

For example:

Code: Select all

    If InStr(1, paraStyle.NameLocal, "Page 1 (i)", CompareMethod.Text) > 0 Then
        paraThis.LeftIndent = (0.65)  'This indents the entire paragraph
        paraThis.FirstLineIndent = (-0.65)  ' This removes the indent from the first line creating a hanging indent.
    End If
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

diana
3StarLounger
Posts: 279
Joined: 01 Jun 2010, 00:27

Re: Word 2007 set paragraph formatting Hanging indents

Post by diana »

okie dokie...ill give that ago! and see how it goes :thankyou:

User avatar
HansV
Administrator
Posts: 78394
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Word 2007 set paragraph formatting Hanging indents

Post by HansV »

An easy way to find out how hanging indents are represented in VBA is to record a macro of setting a hanging indent.
Word will record more than just the indents, but if you scan the code for 'indent', you'll find the essential lines.
This will result in exactly what Rudi suggested (he actually mentions that he recorded a macro).
Translating from VBA to VB.Net is not really difficult.
Best wishes,
Hans

diana
3StarLounger
Posts: 279
Joined: 01 Jun 2010, 00:27

Re: Word 2007 set paragraph formatting Hanging indents

Post by diana »

Thank you all. It took me awhile to get my head around :groan:

.FirstLineIndent

and

.tabhangingindent

http://msdn.microsoft.com/en-us/library ... gingindent(v=office.15" onclick="window.open(this.href);return false;).aspx

which isnt the actual tab stop configurations. however but the number of times you press the Tab key ie the specified number of tab stops.

also tab stops were conflicting, lost, and replaced :scratch:


The final working code is below.

Thank you again all :clapping:

Code: Select all

       If worddoc.Template.TemplateType = WordTemplate.TemplateTypes.Advice Then

                        '  template is formatted with different paragraph and tab configurations.
                        '  Therefore Restart Numbering code results are loses paragraph indentation. following code resolves

                        Dim paraThis As MSWord.Paragraph
                        For Each paraThis In worddoc.Document.Application.Selection.Paragraphs

                            Dim range As MSWord.Range = worddoc.Document.Range
                            Dim Selection As MSWord.Selection = worddoc.Document.Application.Selection
                            Dim paraStyle As MSWord.Style = CType(worddoc.Library.Application.Application.Selection.Paragraphs.Style, Microsoft.Office.Interop.Word.Style)

                            If InStr(1, paraStyle.NameLocal, "Page 1 Para number", CompareMethod.Text) > 0 Then

                                With worddoc.Document.Application.Selection.Range.Paragraphs
                                    .LeftIndent = Document.Application.CentimetersToPoints(0.65)
                                    .FirstLineIndent = Document.Application.CentimetersToPoints(-0.65)
                                End With

                            End If

                            If InStr(1, paraStyle.NameLocal, "Page 1 (i)", CompareMethod.Text) > 0 Then

                                With worddoc.Document.Application.Selection.Range.Paragraphs
                                    .TabStops.ClearAll()
                                    .TabStops.Add(Position:=Document.Application.CentimetersToPoints(1.5))
                                    .LeftIndent = Document.Application.CentimetersToPoints(1.5)
                                    .FirstLineIndent = Document.Application.CentimetersToPoints(-0.85)
                                    .TabStops.Add(Position:=Document.Application.CentimetersToPoints(-2.28)).Clear()
                                End With

                            End If

                            If InStr(1, paraStyle.NameLocal, "Page 1 (a)", CompareMethod.Text) > 0 Then

                                With worddoc.Document.Application.Selection.Range.Paragraphs
                                    .LeftIndent = Document.Application.CentimetersToPoints(0.65)
                                    .FirstLineIndent = Document.Application.CentimetersToPoints(-0.65)
                                    .TabStops.Add(Position:=Document.Application.CentimetersToPoints(0.65))
                                End With

                            End If

                        Next

                    End If

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Word 2007 set paragraph formatting Hanging indents

Post by Rudi »

TX for the feedback, and glad you got it working.
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.