[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Help with Selection.IsEqual

Posted on 2008-01-29
3
Medium Priority
?
170 Views
Last Modified: 2010-07-27
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

0
Comment
Question by:baker_eliz
  • 2
3 Comments
 
LVL 77

Accepted Solution

by:
GrahamSkan earned 2000 total points
ID: 20776006
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
 
LVL 77

Expert Comment

by:GrahamSkan
ID: 20776017
I seem to have included your code and mine, and called them both IndentByFind. My apologies for the confusion.
0
 

Author Comment

by:baker_eliz
ID: 20779879
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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

611 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question