Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word 2003 VBA & Form - strange behaviour of Doc focus 1

Status
Not open for further replies.

MakeItSo

Programmer
Oct 21, 2003
3,316
DE
Hi friends,

Something is getting me a bit confused here. I am currently developing an app which will allow me to check tech doc. for common errors, such as wrong terminology, awkward style, or simply double spaces, space before punctuation etc.

For this, I stored terminology, forbidden terms, and regexp patterns and replacements in a database.
On executing a check on an opened document, I query this db, execute the REGEXPs and display the affected segment in a user form together with the suggested correction - and this is where the problem lies:
The "Selection" Range is set to the affected text portion, but word won't visibly "jump" to said position!

Here's some code snippet, hope it'll help you see the problem:
Code:
Set rs = con.Execute("SELECT * FROM expressions")

Do While Not rs.EOF
    pat = rs("expression").Value
    rep = rs("replacement").Value
    comt = rs("comment").Value
    Act = rs("action").Value
    
    With objRegEx
        .Global = True
        .IgnoreCase = False
        .MultiLine = True
        .Pattern = pat
        Set objMatch = .Execute(ActiveDocument.Content.Text)
    End With

    For Each mMatch In objMatch
...
...
[b]Selection.SetRange mMatch.FirstIndex + 1, mMatch.FirstIndex + mMatch.Length - 2
            Selection.SetRange Selection.Sentences(1).Start, Selection.Sentences(1).End[/b]
The bolded part is what should make the document display jump to that sentence. Alas it doesn't!
Why?
[ponder]

Thanks for any hint!

Cheers,
Andy

[navy]"We had to turn off that service to comply with the CDA Bill."[/navy]
- The Bastard Operator From Hell
 
The bolded part is what should make the document display jump to that sentence. Alas it doesn't!"

Sorry, but that is incorrect. It makes the Selection that range, but that does not (as you have discovered) mean the display is moved to that part of the document.

A simple way to get this is to use a Range object (set to your Start and End values), and then select it. Then the Selection (AND the display!) will indeed "jump" to that location.
Code:
Dim r As Range

Set r = ActiveDocument.Range(Start:=[i]whatever[/i], _
              End:=[i]theOtherWhatever[/i])
r.Select
[COLOR=red]'  you can still use this part to
'  get the sentence[/color red]
Selection.SetRange Selection.Sentences(1).Start, _
   Selection.Sentences(1).End

faq219-2884

Gerry
My paintings and sculpture
 
Ha!
A simple solution that works just as it should!
All fine now. :)

Thanks Gerry!
[thumbsup2]

[navy]"We had to turn off that service to comply with the CDA Bill."[/navy]
- The Bastard Operator From Hell
 
To get the sentence Selected, you can make it even simpler by Expanding the range object, rather than using the Selection.SetRange.
Code:
Dim r As Range

Set r = ActiveDocument.Range(Start:=[i]whatever[/i], _
              End:=[i]theOtherWhatever[/i])
With r
    .Expand Unit:=wdSentence
    .Select
End With


faq219-2884

Gerry
My paintings and sculpture
 
Yes, Gerry.
I had decided for the "SetRange" only because I wanted to jump to the respective part of the document. Since this doesn't work, I can just as well take the "range" route.

Good proposal. Thanks!

[navy]"We had to turn off that service to comply with the CDA Bill."[/navy]
- The Bastard Operator From Hell
 
Ah. This is a very good example of the difference between range and selection.

Range is not actioned through the GUI.
Selection is.

That is the power of Range, as you can do a bunch of stuff without any use of GUI resources, or stuff happening on screen.

Using SetRange even on the Selection does NOT go through the GUI...because it is the RANGE that is being actioned. True, it is the range of the Selection, and if you scroll to where ever it is, you will see its range is indeed changed. However, the method is SetRange - it is the Range that is being actioned. Therefore it is NOT actioned using the GUI. Thus, it does not jump the GUI to that location.

faq219-2884

Gerry
My paintings and sculpture
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top