OK, I've got it...
Now, I noticed something interesting: though the code is working exactly as expected, I didn't realize before running it through a test that it would actually erase the next correct pair of tags (following a bad pair)!
e.g. original text with tags
#<correct tags>#
#<wrong closing tag#
#<correct tags>#
would give at the very end of the loop:
correct tags
#<wrong closing tag#
correct tags
And when I think about it, this makes more sense than what I actually wanted to accomplish... So I thought I could get the code to do it directly instead. The code is also simpler that way!
I copied below both options (of course, it is either one or the other). The first one is the one I was talking about in my previous post. And I applied what I learnt with your help! Hopefully I got it right.
I also commented each line to make sure I understood properly its meaning and action.
I would really appreciate if you could double check both the code and my comments and let me know if I made a mistake somewhere!
Dim oRng As Range, oRng2 As Range, fnd As Range, oTextRng As Range, oTextRng2 As Range
For Each oSection In ActiveDocument.Sections
For Each oHF In oSection.Headers
With oHF
'1st option
If .LinkToPrevious = False Or oSection.Index = 1 Then
Set oRng = oHF.Range 'Whole header
Set oRng2 = oHF.Range 'Whole header
With oRng.Find
.Text = strOTag 'Opening tag
While .Execute 'Find opening tag (oRng)
oRng2.Start = oRng.End 'Starting after opening tag found above
With oRng2.Find
.Text = strCTag 'Closing tag
If .Execute Then 'Find closing tag (oRng2)
Set oTextRng = oRng.Duplicate 'Duplicating oRng. Now oTextRng = opening tag
oTextRng.Collapse wdCollapseEnd 'Starting position of oTextRng is now the end of Opening tag
oTextRng.End = oRng2.Start 'End of oTextRng = before Closing tag. Therefore oTextRng is the string between opening/closing tags
If Not InStr(oTextRng.Text, strOTag) > 0 Then 'No extra opening tag found in oTextRng, therefore pair of tags is correct. Processing tags and highlighting text
oRng.Delete 'Delete opening tag
oRng2.Delete 'Delete closing tag
oTextRng.HighlightColorIndex = Couleur1_Selectionnee 'Highlight string (this color is chosen by the user)
Else 'opening tag found within the string. Highlighting tags only in another color.
oRng.HighlightColorIndex = Couleur2_Selectionnee 'Highlight opening tag (this color is chosen by the user, though it will probably be red)
oRng2.HighlightColorIndex = Couleur2_Selectionnee 'Highlight closing tag
Set oTextRng2 = oTextRng.Duplicate 'Duplicate string range
With oTextRng2.Find
.Text = strOTag 'Opening tag
If .Execute Then 'Find opening tag in string
oTextRng2.HighlightColorIndex = Couleur2_Selectionnee 'Highlight extra opening tag
End If
End With
End If
End If
End With
Wend
End With
End If
'2nd solution
If .LinkToPrevious = False Or oSection.Index = 1 Then
Set oRng = oHF.Range 'Whole header
Set oRng2 = oHF.Range 'Whole header
With oRng.Find
.Text = strOTag 'Opening tag
While .Execute 'Find opening tag (oRng)
oRng2.Start = oRng.End 'Starting after opening tag found above
With oRng2.Find
.Text = strCTag 'Closing tag
If .Execute Then 'Find closing tag (oRng2)
Set oTextRng = oRng.Duplicate 'Duplicating oRng. Now oTextRng = opening tag
oTextRng.Collapse wdCollapseEnd 'Starting position of oTextRng is now the end of Opening tag
oTextRng.End = oRng2.Start 'End of oTextRng = before Closing tag. Therefore oTextRng is the string between opening/closing tags
If Not InStr(oTextRng.Text, strOTag) > 0 Then 'No extra opening tag found in oTextRng, therefore pair of tags is correct. Processing tags and highlighting text
oRng.Delete 'Delete opening tag
oRng2.Delete 'Delete closing tag
oTextRng.HighlightColorIndex = Couleur1_Selectionnee 'Highlight string
Else 'opening tag found within the string. Highlighting tags only in another color.
oRng.HighlightColorIndex = Couleur2_Selectionnee 'Highlight opening tag
End If
End If
End With
Wend
End With
End If
End With
Next oHF
Next oSection
Also, I still haven't gone through all the process renaming variables in all of my code. If you have more advise about a good rule of thumb... string variables would start by "str". integers, by "int" I assume? any other suggestion?