Results 1 to 6 of 6
  1. #1
    New Lounger
    Join Date
    Dec 2001
    Location
    Jerusalem, Israel
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Character before and after selection (Word 2003)

    Hi,

    I'm writing a macro to replace occurrences of a term with a cross reference to a glossary. The macro finds the term, cuts it, and inserts the cross reference.

    For reasons I don't understand, sometimes the Selection.Cut (after finding the term) removes the space before, though it doesn't appear selected.

    I need either to be able to control the Cut or examine the character before the selection to see if I need a space.

    I can't see any way to get the index of the current location in the Document.Characters collection so I can check index-1.
    I've also had no luck expanding or extending the selection so I can look at the characters immediately before and after the original selection by looking at the expanded selection's character collection.

    The requirement is to have one and only one space before and after the inserted cross reference, unless the char before or after is not in 0-9, a-z, or A-Z.

    Any suggestions?

    TIA,
    Steve Silberman

  2. #2
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Character before and after selection (Word 200

    Hi Steve,

    If you want the character immediately preceding the start of the currently selected text, you can use the following:
    <pre>Selection.Document.Range(Selection.Range.Star t -1, Selection.Range.Start).Text
    </pre>


    And if you're interested, here's a general purpose function to collapse the ends of the current selection so that it doesn't include any spaces or paragraph marks:
    <pre>Function ChompSelection(ByRef sel As Selection)
    If sel.Range.Characters.Count = 1 Then Exit Function
    sel.MoveStartWhile cset:=(Chr$(32) & Chr$(13)), Count:=sel.Characters.Count
    sel.MoveEndWhile cset:=(Chr$(32) & Chr$(13)), Count:=-sel.Characters.Count
    End Function
    </pre>


    Hope this helps!

  3. #3
    New Lounger
    Join Date
    Dec 2001
    Location
    Jerusalem, Israel
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Character before and after selection (Word 200

    Hi Andrew,
    Thanks very much. There are a lot of places I can use this.
    As it happens, I do much of my best work asleep. I woke up this morning and added this to the main routine:

    Dim PasteAdust As Boolean 'To save option setting

    'Save user's option setting, then turn off smart cut & paste
    ' so that Selection Delete leaves surrounding spaces and other characters
    PasteAdust = Options.PasteSmartCutPaste
    Options.PasteAdjustWordSpacing = False

    ....

    'Restore user's option setting
    Options.PasteSmartCutPaste = PasteAdust
    End Sub

    Someday I'll learn that when I'm stuck on something that looks simple, I should leave it for a while - but I'll probably retire first.

    In any case, I'll find plenty of use for your hint in other macros.

    Thanks,

    Steve

  4. #4
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Character before and after selection (Word 200

    Operating on the professional premise that if it works it should be fixed (grin)
    <pre>Function lngChompRange(ByRef rng As Range, strChars As String) As Long
    If rng.Characters.Count = 1 Then
    Exit Function
    Else
    Dim lng As Long
    lng = lng + rng.MoveStartWhile(cset:=(strChars), Count:=rng.Characters.Count)
    lng = lng + Abs(rng.MoveEndWhile(cset:=(strChars), Count:=-rng.Characters.Count))
    lngChompRange = lng
    End If
    'Sub TESTChompRange()
    ' Dim rng As Range
    ' Set rng = Selection.Range
    ' MsgBox lngChompRange(rng, Chr$(32) & Chr$(13))
    ' rng.Select
    'End Sub
    End Function

    </pre>


    I wouldn't have taken so much time to post, but fifteen minutes were spent wondering why the dickens the second MoveEndWhile not only "failed to work", but caused my lng to be set to zero.

    'Twas an accident that caused me to realise that *my* test data had exactly five whitespace characters before, and five after the text. It was then that I decided to introduce the ABS function, complicating my function even further.


    Anyway, it's working now (again!), but with ranges.

  5. #5
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Character before and after selection (Word 200

    Please post a sample of the data you're talking about -- I couldn't replicate the problem with text preceded and followed by 5 spaces, it just collapsed the selection as expected.

  6. #6
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Character before and after selection (Word 200

    Attached is the crude document.

    You'll find that Andrew's code works just fine, as does mine, now.

    If you remove the ABS call from my code, and select my entire document, you'll note that the 5 whitespace characters preceding the text generate a value of "5" in my first assignment to lng, and the second five contribute "minus 5" to the lng total (once the ABS is removed).

    Adding -5 to 5 gave me my puzzling result of zero!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •