Sub startCheckElement()
checkElement(1)
End Sub
Function checkElement(ByVal counter As Long)
Dim p As Paragraph
Set p = ActiveDocument.Paragraphs(counter)
'if you come to an Start
If Right(p.Style, 5) = "Start" Then
checkInner (counter + 1)
End If
counter = counter + 1
'Checks that we don't run into an eternal loop
If counter >= ActiveDocument.Paragraphs.Count Then
Exit Function
End If
'the function calls itself with counter as arg.
Set p = Nothing
checkElement(counter)
End Function
Function checkInner(ByVal counter As Long)
Dim p As Paragraph
Set p = ActiveDocument.Paragraphs(counter)
If Right(p.Style, 5) = "Start" Then
insideAStyleSet = True
checkInner2 (counter + 1)
On Error GoTo feil
'making the new style
Dim styleName2
styleName2 = p.Style + " Niv2"
makeStyle styleName2, p.Style
feil:
'setting the style
setStyle styleName2, counter
End If
If Right(p.Style, 5) = "Slutt" Then
If insideAStyleSet = False Then
Exit Function
Else
On Error GoTo feil2
'Making new style
styleName2 = p.Style + " Niv2"
makeStyle styleName2, p.Style
feil2:
'setting the style
setStyle styleName2, counter
insideAStyleSet = False
'checking for more stylesets
Set p = Nothing
checkInner (counter + 1)
End If
End If
counter = counter + 1
Set p = Nothing
checkInner (counter)
End Function
Function makeStyle(ByVal styleName As String, ByVal s As String)
With ActiveDocument
.Styles.Add Name:=styleName, Type:=wdStyleTypeParagraph
.Styles(styleName).BaseStyle = s
End With
End Function
Function setStyle(ByVal name As String, ByVal counter As Long)
Dim p As Paragraph
Set p = ActiveDocument.Paragraphs(counter)
p.Style = ActiveDocument.Styles(name)
End Function
Function checkInner2(ByVal counter As Long)
Dim p As Paragraph
Set p = ActiveDocument.Paragraphs(counter)
If Right(p.Style, 5) = "Start" Then
insideAStyleSet2 = True
On Error GoTo feil
'Making new style
Dim styleName2
styleName2 = p.Style + " Niv3"
makeStyle styleName2, p.Style
feil:
'setting the style
setStyle styleName2, counter
End If
If Right(p.Style, 5) = "Slutt" Then
If insideAStyleSet2 = False Then
Exit Function
Else
On Error GoTo feil2
'making a new style
styleName2 = p.Style + " Niv3"
makeStyle styleName2, p.Style
feil2:
'setting the style
setStyle styleName2, counter
insideAStyleSet2 = False
'checking for more stylesets
Set p = Nothing
checkInner2 (counter + 1)
Exit Function
End If
End If
counter = counter + 1
Set p = Nothing
checkInner (counter)
End Function