vivka,
I'm sorry, but your code doesn't do that exactly. Your code is altering every subsequent paragraph to the first paragraph regardless is the subsequent paragraph meets the condition for change. The condition for change is "Is the subsequent paragraph S index 1 more than the reference paragraph. Running your code on the last sample provided returns:
<S1> AAA
< S1- S1-S2> BBB
< S2-S3> CCC
< S3-S4> CCC
Running it on a sample where the one or more of the next paragraphs are NOT one index higher e.g.,
<S1> AAA
<S2> BBB
<S2> bbb (note this paragraph is not indexed 1 higher than the previous)
<S3> CCC
<S4> DDD
Returns this:
<S1> AAA
< S1- S1-S2> BBB
< S2-S2> bbb
< S2-S3> CCC
< S3-S4> CCC
With that said, Balavaka your examples are still as clear as mud.
How do you go from <S4> CCC to <S3-S4) DDD?
This is not a simple process. However, the following might get you close:
Code:
Sub ScratchMacro()
'A basic Word Macro coded by Gregory K. Maxey
Dim lngIndex As Long
Dim oRng As Range
Dim oParRng As Range
Dim arrIndexParts() As String
Dim strRefIndex As String
Set oRng = ActiveDocument.Range 'or selection range
For lngIndex = 1 To oRng.Paragraphs.Count - 1
'See if the paragraph and subsequent paragraph have the indexing flag.
If InStr(oRng.Paragraphs(lngIndex).Range.Text, "<S") = 1 And InStr(oRng.Paragraphs(lngIndex + 1).Range.Text, "<S") = 1 Then
Set oParRng = oRng.Paragraphs(lngIndex).Range
'Move the range until after the "<S"
oParRng.MoveStart wdCharacter, 2
'See if the paragraph is a simple or complex index e.g., "S1" or "S1-S2"
If InStr(oParRng.Text, "-S") > 0 And InStr(oParRng.Text, "-S") < InStr(oParRng.Text, "> ") Then
'Complex, move range passed the second index flag.
oParRng.MoveStartUntil Cset:="S"
oParRng.MoveStart wdCharacter, 1
'Get the index number.
arrIndexParts = Split(oParRng.Text, "> ")
strRefIndex = arrIndexParts(0)
Else
'Get the index number
arrIndexParts = Split(oParRng.Text, "> ")
strRefIndex = arrIndexParts(0)
End If
'Get the subsequent paragraph index number and compare to previous
Set oParRng = oRng.Paragraphs(lngIndex + 1).Range
oParRng.MoveStart wdCharacter, 2
arrIndexParts = Split(oParRng.Text, "> ")
If strRefIndex + 1 = arrIndexParts(0) Then
'Condition met. Create complex index.
oParRng.InsertBefore strRefIndex & "-S"
End If
End If
Next lngIndex
lbl_Exit:
Exit Sub
End Sub