Macro to Select text from a point given to a specific word

GDA

Joined
Mar 3, 2022
Messages
6
Reaction score
0
Hello everyone
My first experiment with the selection through levels didn't end well.
So i tried to find another way and I'm almost there, but I'm sure my code is wrong in some parts.
Here's the code

Sub test()

Dim flag As Boolean
flag = True

While flag = True
Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
If Strings.Right(Selection.Range.Text, 1) = "!" Then
flag = False

End If
Wend

End Sub

This is perfect to select to a specific character, but if i change the selection range from wdcharacter to wdword it doesn't function anymore.
I'm thinking that the problem could be in the text range, but at this point I'm stuck and i can't make it work and as soon as i launch the macro, the program freeze and crash.

Could someone please help me?
Thanks for the attention given
GDA
 

macropod

Microsoft MVP
Joined
Mar 2, 2012
Messages
578
Reaction score
50
What's wrong with:
Code:
Sub Demo()
With Selection
  .MoveEndUntil "!"
  .End = .End + 1
End With
End Sub
 

GDA

Joined
Mar 3, 2022
Messages
6
Reaction score
0
What's wrong with:
Code:
Sub Demo()
With Selection
  .MoveEndUntil "!"
  .End = .End + 1
End With
End Sub
Thanks!
This is a really elegant way to select until a specific character.
Even if I had to do it for a specific word, I could make it work with a hidden control character that is not present in the file.
Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top