Help with Selection.IsEqual

I am trying to format some HTML text in Word (simply because it's easier there) and would like to indent all paragraphs between <Style> and </Style>. The first paragraph in the file consists solely of the word <Style> just so the IsEqual function has comething to compare with.

The Watch pane for expressions shows both Selection and the ActiveDocument.Paragraphs(1).Range to be equal (i checked character by character), yet the IsEqual function does not show true. What am i missing? Thanks.

Sub IndentStyles()
     With Selection.Find
        .Forward = True
        .ClearFormatting
        .MatchWholeWord = True
        Do While .Execute(FindText:="<Style ", Forward:=True) = True
            With Selection
                .Move Unit:=wdParagraph, Count:=1
                .Expand Unit:=wdSentence
                Do Until .IsEqual(ActiveDocument.Paragraphs(1).Range)
                    .Paragraphs.Indent
                    .Move Unit:=wdParagraph, Count:=1
                    .Expand Unit:=wdSentence
                Loop
            End With
        Loop
    End With
End Sub

Open in new window

Elizabeth Bakerexe asstAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GrahamSkanRetiredCommented:
If you want to assure yourself that the ranges are the same, examine (debug.print) the .Start and .End properties to see if they are the same for both ranges. Also watch out for the effect of MatchWholeWord. It might not define a word in the same way that you do.

Actually, I would probably just walk through the paragraphs collection. That would especially helpf if you will need several levels of indentation. see 'IndentByParagraph' macro.

I always avoid using the Selection object, preferring Range objects instead. In this case you really need two Finds, so you also need two range objects, so ranges are imperative, not just a preference. The 'IndentByFind' Sub shows how.
Sub IndentByParagraph()
    Dim IndentValue As Single
    Dim para As Paragraph
    Const IndentIncrement = 1
    For Each para In ActiveDocument.Paragraphs
        para.Format.LeftIndent = IndentValue
        Select Case Left$(para.Range.Text, Len(para.Range.Text) - 1) 'drop paragraph mark
            Case "<Style>", "<Tag1>", "<Tag2>"
                IndentValue = IndentValue + CentimetersToPoints(IndentIncrement)
            Case "</Style>", "</Tag1>", "</Tag2>"
                IndentValue = IndentValue - CentimetersToPoints(IndentIncrement)
        End Select
    Next para
End Sub
 
Sub IndentByFind()
     With Selection.Find
        .Forward = True
        .ClearFormatting
        .MatchWholeWord = True
        Do While .Execute(FindText:="<Style ", Forward:=True) = True
            With Selection
                .Move Unit:=wdParagraph, Count:=1
                .Expand Unit:=wdSentence
                Do Until .IsEqual(ActiveDocument.Paragraphs(1).Range)
                    .Paragraphs.Indent
                    .Move Unit:=wdParagraph, Count:=1
                    .Expand Unit:=wdSentence
                Loop
            End With
        Loop
    End With
End Sub
 
Sub IndentByFind()
    Dim para1 As Paragraph
    Dim para2 As Paragraph
    Dim rng1 As Range
    Dim rng2 As Range
    Dim IndentWidth As Single
    
    IndentWidth = CentimetersToPoints(1)
    
    'Set rng1 = Selection.Range.Duplicate ' if you need to limit action to the selection
    Set rng1 = ActiveDocument.Range.Duplicate ' whole document
    rng1.Paragraphs.LeftIndent = 0 'align all paragraphs
    With rng1.Find
        .Text = "<Style>"
        Do While .Execute
            Set rng2 = rng1.Duplicate
            rng2.End = ActiveDocument.Range.End
            With rng2.Find
                .Text = "</Style>"
                If .Execute Then
                    rng2.Collapse wdCollapseStart
                    rng2.Start = rng1.Paragraphs(1).Range.End
                    rng2.Paragraphs.LeftIndent = IndentWidth
                Else
                    MsgBox "Unmatched <Style> tag"
                End If
            End With
        Loop
    End With
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GrahamSkanRetiredCommented:
I seem to have included your code and mine, and called them both IndentByFind. My apologies for the confusion.
0
Elizabeth Bakerexe asstAuthor Commented:
Thanks, Graham. The indentByParagraph kept decrementing the paragraph indents. I suppose I could have gotten it to work for me, but I could not invest the time right now. However, the IndentByFind was just what I was looking for. (I still don't understand why my .IsEqual wansn't working, though.)

Thanks, again. You guys are the best. You always come through for me.

--e
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.