Line by Line compare Left Indent greater than or equal to the line before it - VBA MS Word Macro

StackOverflow https://stackoverflow.com/questions/23494722

  •  16-07-2023
  •  | 
  •  

Pregunta

Here's my working code for anyone that needs it in the future.

Sub Test()

Dim prePara As Paragraph
Dim curPara As Paragraph
Dim nextPara As Paragraph
Dim i As Integer

For i = 2 To ActiveDocument.Paragraphs.Count

    Set prePara = ActiveDocument.Paragraphs(i - 1)
    Set curPara = ActiveDocument.Paragraphs(i)

    'in case i need this variable later.
    If i <> ActiveDocument.Paragraphs.Count Then
        Set nextPara = ActiveDocument.Paragraphs(i + 1)
    End If

 'I use heading styles for formatting. If it's not the style I'm using it's probably these. I wouldn't want my entire doc to sequentially indent off of each other   
    If curPara.LeftIndent <= prePara.LeftIndent And curPara.Style = "Normal" Or curPara.Style = "List Paragraph" Then 

        ActiveDocument.Paragraphs(i).Range.Select 'selects the active paragraph
        If Selection.Information(wdWithInTable) = False Then 'makes sure it's not a table
            If curPara.LeftIndent < prePara.LeftIndent Then 'if the current paragraph's indent is less than the one before it then...
                curPara.LeftIndent = prePara.LeftIndent 'make the indent equal to the one before it
            End If
        End If
    End If
Next


End Sub

--------------

I'm a newer coder, sorry if this seams trivial.

I'm trying to create a macro that goes throgh each line and makes sure that any line that has an indent of 0 has an indent greater than or equal to the indent of the line before it. If it is not greater than or equal to the line before it, I want the indent to be equal to the previous line.

I've found Selection.Paragraphs.LeftIndent is the code that I need to work with but I'm not good at constructing simple code like this without an example of how to do it. I was thinking a "For each oPara In ActiveDocument.Paragraphs" loop but unsure on how to actually code it. I was just thinking that this only applies to text that is not "Heading 1"

Dim Para As Object

For Each Para In .Paragraphs

If Para.Paragraphs.LeftIndent = 0 and para.Paragraphs.style <> "Heading 1" Then
   'get paragraph indent of the previous paragraph
   'If then comparitive indent statment
       'make the current paragraph = to previous Para
   'End If
End If
Next 

dl.dropboxusercontent.com/u/22523394/Indent%20Example%20.docx

Update new code based on below...

And I'm very frustrated and lost...this code is terrible and I don't know where to go with it. It doesn't even remotely structurally look like it's goin to accomplish my task.

Dim prevParagraph
Dim currentParagraph

'Use the first item in the list as the first 'prev'
Set prevParagraph = List(1)

'Now, starting with the second item, go through the rest of the list
For i = 2 To List.Count
'Get the current item
Set currentParagraph = List(i).Paragraphs.LeftIndent
Set prevParagraph = List(i - 1).Paragraphs.LeftIndent

If List(i).Paragraphs.LeftIndent = 0 And List(i).Paragraphs.Styles <> "Heading 1" Then
'Finally, make sure to use the current as the prev for the next iteration of the loop


End If
Set prevParagraph = currentParagraph
Next

Newer Code...still bad but better?

Dim prevParagraph
Dim currentParagraph
Dim list

' Use the first item in the list as the first 'prev'
Set prevParagraph = list(1)

'Now, starting with the second item, go through the rest of the list
For i = 2 To list.Count
'Get the current item
Set currentParagraphIndent = list(i).Paragraphs.LeftIndent
Set prevParagraphIndent = list(i - 1).Paragraphs.LeftIndent

If currentParagraphsIndent = 0 And list(i).Paragraphs.Styles <> "Heading 1" Then
'Finally, make sure to use the current as the prev for the next iteration of the loop
    Selection.Paragraphs.LeftIndent = prevParagraphIndent
End If

Set prevParagraph = currentParagraph
Next

Here's a concept from scratch. I just don't know if this would work of the verbage to make it work.

For Each Paragraph In Documents.Active
If currentParagraphs.LeftIndent = 0 And currentParagraphs.Style <> "Heading 1" Then
    If previousParagraphs.LeftIndent > currentParagraphs.Indent Then
        Selection.currentParagraphs.Indent = previousParagraphs.LeftIndent
    End If
End If
currentParagraphs = previousParagraphs

Next

'here's my newest code that works out ish, i need to tinker with it.

Dim prePara As Paragraph
Dim curPara As Paragraph

For i = 2 To ActiveDocument.Paragraphs.Count

    Set prePara = ActiveDocument.Paragraphs(i - 1)
    Set curPara = ActiveDocument.Paragraphs(i)

    If curPara.LeftIndent <= 0 Then
        If curPara.LeftIndent < prePara.LeftIndent Then
           curPara.LeftIndent = prePara.LeftIndent
        End If
    End If
Next
¿Fue útil?

Solución

This kind of situation happens a lot in Computer Science - the need to go through a list of things and keep track of the previous one. Here is some pseudocode to give you an idea of how to handle this:

Dim prev
Dim current

'Use the first item in the list as the first 'prev'
Set prev = List(1)

'Now, starting with the second item, go through the rest of the list
For i = 2 To List.Count
    'Get the current item
    Set current = List(i)

    ' ...
    'Do stuff comparing prev to current
    ' ...

    'Finally, make sure to use the current as the prev for the next iteration of the loop
    Set prev = current
Next

If this doesn't fully answer your questions, drop a comment below.


I didn't want to give you the full code because of course it's much better for you in the long run to understand what's going on, but here's what I would try, line for line (Ignoring "Header 1" for the moment), if it was me:

Dim i As Long
Dim oPrevPara As Paragraph
Dim oCurrPara As Paragraph


'Use the first item in the list as the first 'prev'
Set oPrevPara = ActiveDocument.Paragraphs(1)

'Now, starting with the second item, go through the rest of the list
For i = 2 To ActiveDocument.Paragraphs.Count
    'Get the current item
    Set oCurrPara = ActiveDocument.Paragraphs(i)

    ' ...
    'Do stuff comparing prev to current
    If oCurrPara.LeftIndent = 0 Then
        If oPrevPara.LeftIndent <> oCurrPara.LeftIndent Then
            oCurrPara.LeftIndent = oPrevPara.LeftIndent
        End If
    End If
    ' ...

    'Finally, make sure to use the current as the prev for the next iteration of the loop
    Set oPrevPara = oCurrPara
Next

Does that help?

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top