Text Selection with VBA

J

Jack

Hello, I am new to VBA in Word and I was wondering if
someone could help me please!

I have a string in a Word document that I want to copy
from Word and then copy to an Excel worksheet. But the
string has a hard return at the end of it. So how do I
copy from the beginning of the string to the hard return?
I know the position in the document that the string starts
at but I do not know how to copy only to the carriage
return. For examle, when you select "Show/Hide ¶" to Show
it looks like:

125 North Street¶

How do I use VBA to select only up to the hard return? I
hope this makes sense. Any help would be appreciated.

Thanks.
 
D

Dave Lett

Hi Jack,

You can use something like the following:

Dim oRng As Range
Set oRng = Selection.Range
oRng.End = oRng.Paragraphs(1).Range.End
oRng.MoveEnd Unit:=wdCharacter, Count:=-1

HTH,
Dave

Hello, I am new to VBA in Word and I was wondering if
someone could help me please!

I have a string in a Word document that I want to copy
from Word and then copy to an Excel worksheet. But the
string has a hard return at the end of it. So how do I
copy from the beginning of the string to the hard return?
I know the position in the document that the string starts
at but I do not know how to copy only to the carriage
return. For examle, when you select "Show/Hide ¶" to Show
it looks like:

125 North Street¶

How do I use VBA to select only up to the hard return? I
hope this makes sense. Any help would be appreciated.

Thanks.
 
R

Ravi

Hi Jack,

May be this also helpful for you.

Dim SelText As String
SelText = Selection.Text
If Selection.Characters(Len(Selection.Text)) = Chr(13) Then
Selection.MoveLeft unit:=wdCharacter, Count:=1, Extend:=wdExtend
End If
Selection.Copy

-Ravi
 

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