Results 1 to 4 of 4
  1. #1
    New Lounger
    Join Date
    Jan 2001
    Location
    Toronto, ON, Cayman Brac, Canada
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Grouping of Characters Macro

    I suspect this will be a no-brainer for many of you, but it has me scratching my head (never done a macro in Word yet, but very eager to learn!)

    What I'd like to be able to do is select a series of characters in the document (such as 'ajskdlfajskdlfa') and then trigger a macro that would automatically insert one space after every three characters, for the duration of the selected range. Using the example again, the result would be 'ajs kdl faj skd lfa'.

    Would anyone be able to get me started in the right direction?

    Thanks!

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Grouping of Characters Macro

    Is the style of the text important to preserve? If not, you could read the selection into variable, insert the needed spaces, and replace the selection with that. I'm sure someone has some time to put that together. (The fun part is the math.)

    On the other hand, if you need to preserve the style of the text, or maybe even if you don't, the other approach would be to get a sense of where the beginning and end of the selection are, then, starting from the beginning, move the insertion point right three spaces and type a space, repeating until you exit your original selection. One way to do this is to mark your selection with a bookmark, and then check to see if you are past the end of the bookmark before typing a space and, if you are, stop and delete the bookmark (not the contents, just the bookmark). This stuff-happening-on-screen approach is generally slower to execute, but if your selections are not very big, the difference won't matter.

    Hope this at least gives you some ideas.

  3. #3
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,853
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: Grouping of Characters Macro

    How about something along these lines
    <pre>Sub Macro2()
    Dim x As Long
    x = Selection.Range.Characters.count
    While x / 3 > 1
    Selection.Characters(3).InsertAfter " "
    Selection.MoveStart Unit:=wdCharacter, count:=4
    x = Selection.Range.Characters.count
    Wend
    End Sub</pre>

    Andrew Lockton, Chrysalis Design, Melbourne Australia

  4. #4
    New Lounger
    Join Date
    Jan 2001
    Location
    Toronto, ON, Cayman Brac, Canada
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Grouping of Characters Macro

    Thank you both for the help on this one! The sample code worked like a charm - and provides a great starting point for me to do some more VBA exploring on my own!

    Cheers,

    Trevor.

Posting Permissions

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