process text in clipboard after pasting

C

Chip Orange

I need to paste some text from the clipboard, and then process just that
pasted text. I'm using selection.paste to do the pasting, is there any way
to easily identify the text that was just pasted (some range that is just
that text?).

thanks.

Chip
 
D

Dave Lett

Hi Chip,

What are you trying to "process"? Do you want to apply formatting to the
range or something like that? If so, then you can use something like the
following:

Dim oRng As Range
Set oRng = Selection.Range
oRng.Paste
oRng.Font.Bold = True

HTH,
Dave
 
C

Chip Orange

thanks Dave; so if I understand, the range of the selection, which may start
out as the insertion point, will expand to include just the text that was
pasted after the paste operation?

(I need to cycle through this text, looking for tables, and applying special
formatting to just the tables that I find).

thanks.

Chip
 
D

Dave Lett

Hi Chip,

Yes, that's sort of it. If you want to cycle through all the tables that
you've just pasted, then you can use something like the following:

Dim oRng As Range
Dim oTbl As Table
Set oRng = Selection.Range
oRng.Paste

For Each oTbl In oRng.Tables
oTbl.Borders(wdBorderDiagonalUp).LineStyle = wdLineStyleDashDotStroked
Next oTbl

HTH,
Dave

Chip Orange said:
thanks Dave; so if I understand, the range of the selection, which may start
out as the insertion point, will expand to include just the text that was
pasted after the paste operation?

(I need to cycle through this text, looking for tables, and applying special
formatting to just the tables that I find).

thanks.

Chip
 
C

Chip Orange

thanks Dave; it's a nice bit of code, and even more, it's helping me to get
towards understanding ranges.

I think this works because you're using the paste method of the range
object, which expands the range to include what was just added? In my first
attempt I was using the past method of the selection object, which cannot
expand by its definition, so that's why this would not work in that case, is
that right?

thanks.

Chip


Dave Lett said:
Hi Chip,

Yes, that's sort of it. If you want to cycle through all the tables that
you've just pasted, then you can use something like the following:

Dim oRng As Range
Dim oTbl As Table
Set oRng = Selection.Range
oRng.Paste

For Each oTbl In oRng.Tables
oTbl.Borders(wdBorderDiagonalUp).LineStyle = wdLineStyleDashDotStroked
Next oTbl

HTH,
Dave

message news:[email protected]...
 
P

Peter Hewett

Hi "Chip Orange" <[email protected][remove-me-and-brackets]>

Here's a little bit of code that does what you want. It will paste whatever is
selected and return a Range object that maps whatever you pasted:

Public Function PasteIt() As Word.Range
Dim rngMark As Word.Range

' Note the start of the current selection because
' that is where the paste will occur
Set rngMark = Selection.Range
rngMark.Collapse wdCollapseStart

' After the paste the pasted text will be selected
Selection.Paste

' The pasted text starts at the old selection and
' extends to the end of whatever we just pasted
rngMark.End = Selection.End

Set PasteIt = rngMark
End Function


You can call it like this:

Public Sub TestMeX()
PasteIt.Select
End Sub

This does the paste and selects it!

HTH + Cheers - Peter
 
D

Dave Lett

Hi Chip,

You're correct on all counts. Glad to see that the code was useful.

Dave

Chip Orange said:
thanks Dave; it's a nice bit of code, and even more, it's helping me to get
towards understanding ranges.

I think this works because you're using the paste method of the range
object, which expands the range to include what was just added? In my first
attempt I was using the past method of the selection object, which cannot
expand by its definition, so that's why this would not work in that case, is
that right?

thanks.

Chip
 
C

Chip Orange

one interesting FWIW, this works fine as described with the range.paste
method, but not with the range.PasteAndFormat method. Strange, and
unfortunate for me as I really needed the equivalent of "match destination
formatting" in my pasting.

Chip


Dave Lett said:
Hi Chip,

You're correct on all counts. Glad to see that the code was useful.

Dave

message news:[email protected]...
thanks Dave; it's a nice bit of code, and even more, it's helping me to get
towards understanding ranges.

I think this works because you're using the paste method of the range
object, which expands the range to include what was just added? In my first
attempt I was using the past method of the selection object, which cannot
expand by its definition, so that's why this would not work in that
case,
 
D

Dave Lett

Hi Chip,

My tests with the following seem to work:

Dim oRng As Range
Dim oTbl As Table
Set oRng = Selection.Range
oRng.PasteAndFormat Type:=wdFormatSurroundingFormattingWithEmphasis

HTH,
Dave

Chip Orange said:
one interesting FWIW, this works fine as described with the range.paste
method, but not with the range.PasteAndFormat method. Strange, and
unfortunate for me as I really needed the equivalent of "match destination
formatting" in my pasting.

Chip
 
C

Chip Orange

Yes, pasting is ok, but, the range object doesn't expand to include the
newly pasted text if you make use of the PasteAndFormat method; try this
(with something in the clipboard):

Dim R As Range
Set R = Selection.Range
R.PasteAndFormat wdFormatSurroundingFormattingWithEmphasis
MsgBox R.Text
R.Paste
MsgBox R.Text




Dave Lett said:
Hi Chip,

My tests with the following seem to work:

Dim oRng As Range
Dim oTbl As Table
Set oRng = Selection.Range
oRng.PasteAndFormat Type:=wdFormatSurroundingFormattingWithEmphasis

HTH,
Dave

message news:[email protected]...
 
P

Peter Hewett

Hi "Chip Orange" <[email protected][remove-me-and-brackets]>

Have you tried the code I posted earlier in this thread???

Cheers - Peter

Yes, pasting is ok, but, the range object doesn't expand to include the
newly pasted text if you make use of the PasteAndFormat method; try this
(with something in the clipboard):

Dim R As Range
Set R = Selection.Range
R.PasteAndFormat wdFormatSurroundingFormattingWithEmphasis
MsgBox R.Text
R.Paste
MsgBox R.Text




Dave Lett said:
Hi Chip,

My tests with the following seem to work:

Dim oRng As Range
Dim oTbl As Table
Set oRng = Selection.Range
oRng.PasteAndFormat Type:=wdFormatSurroundingFormattingWithEmphasis

HTH,
Dave

message news:[email protected]...
one interesting FWIW, this works fine as described with the range.paste
method, but not with the range.PasteAndFormat method. Strange, and
unfortunate for me as I really needed the equivalent of "match destination
formatting" in my pasting.

Chip


Hi Chip,

You're correct on all counts. Glad to see that the code was useful.

Dave

in
message thanks Dave; it's a nice bit of code, and even more, it's helping me to
get
towards understanding ranges.

I think this works because you're using the paste method of the range
object, which expands the range to include what was just added? In my
first
attempt I was using the past method of the selection object, which
cannot
expand by its definition, so that's why this would not work in that
case,
is
that right?

thanks.

Chip


Hi Chip,

Yes, that's sort of it. If you want to cycle through all the tables
that
you've just pasted, then you can use something like the following:

Dim oRng As Range
Dim oTbl As Table
Set oRng = Selection.Range
oRng.Paste

For Each oTbl In oRng.Tables
oTbl.Borders(wdBorderDiagonalUp).LineStyle =
wdLineStyleDashDotStroked
Next oTbl

HTH,
Dave

"Chip Orange"
wrote
in
message thanks Dave; so if I understand, the range of the selection, which
may
start
out as the insertion point, will expand to include just the text
that
was
pasted after the paste operation?

(I need to cycle through this text, looking for tables, and applying
special
formatting to just the tables that I find).

thanks.

Chip


Hi Chip,

What are you trying to "process"? Do you want to apply formatting
to
the
range or something like that? If so, then you can use something
like
the
following:

Dim oRng As Range
Set oRng = Selection.Range
oRng.Paste
oRng.Font.Bold = True

HTH,
Dave

"Chip Orange"
<[email protected][remove-me-and-brackets]>
wrote
in
message I need to paste some text from the clipboard, and then process
just
that
pasted text. I'm using selection.paste to do the pasting, is
there
any
way
to easily identify the text that was just pasted (some range
that
is
just
that text?).

thanks.

Chip

HTH + Cheers - Peter
 
D

Dave Lett

Hi Chip,

My mistake. I thought you were saying that it doesn't work with the
PasteandFormat method because of the formatting (not the range). Anyway,
yeah, if you need to use the PasteAndFormat method, then you could use the
routine with the extra lines that Peter posted. It'll be exactly what you
need.

Dave

Chip Orange said:
Yes, pasting is ok, but, the range object doesn't expand to include the
newly pasted text if you make use of the PasteAndFormat method; try this
(with something in the clipboard):

Dim R As Range
Set R = Selection.Range
R.PasteAndFormat wdFormatSurroundingFormattingWithEmphasis
MsgBox R.Text
R.Paste
MsgBox R.Text




Dave Lett said:
Hi Chip,

My tests with the following seem to work:

Dim oRng As Range
Dim oTbl As Table
Set oRng = Selection.Range
oRng.PasteAndFormat Type:=wdFormatSurroundingFormattingWithEmphasis

HTH,
Dave

in
message news:[email protected]... me
to
In
my
first
attempt I was using the past method of the selection object, which
cannot
expand by its definition, so that's why this would not work in that
case,
is
that right?

thanks.

Chip


Hi Chip,

Yes, that's sort of it. If you want to cycle through all the tables
that
you've just pasted, then you can use something like the following:

Dim oRng As Range
Dim oTbl As Table
Set oRng = Selection.Range
oRng.Paste

For Each oTbl In oRng.Tables
oTbl.Borders(wdBorderDiagonalUp).LineStyle =
wdLineStyleDashDotStroked
Next oTbl

HTH,
Dave

"Chip Orange"
wrote
in
message thanks Dave; so if I understand, the range of the selection, which
may
start
out as the insertion point, will expand to include just the text
that
was
pasted after the paste operation?

(I need to cycle through this text, looking for tables, and applying
special
formatting to just the tables that I find).

thanks.

Chip


Hi Chip,

What are you trying to "process"? Do you want to apply formatting
to
the
range or something like that? If so, then you can use something
like
the
following:

Dim oRng As Range
Set oRng = Selection.Range
oRng.Paste
oRng.Font.Bold = True

HTH,
Dave

"Chip Orange"
<[email protected][remove-me-and-brackets]>
wrote
in
message I need to paste some text from the clipboard, and then process
just
that
pasted text. I'm using selection.paste to do the pasting, is
there
any
way
to easily identify the text that was just pasted (some range
that
is
just
that text?).

thanks.

Chip
 
C

Chip Orange

thanks Peter; because of a problem with my news reader I had missed this
originally, but it looks perfect.

thanks again,

Chip


Peter Hewett said:
Hi "Chip Orange" <[email protected][remove-me-and-brackets]>

Here's a little bit of code that does what you want. It will paste whatever is
selected and return a Range object that maps whatever you pasted:

Public Function PasteIt() As Word.Range
Dim rngMark As Word.Range

' Note the start of the current selection because
' that is where the paste will occur
Set rngMark = Selection.Range
rngMark.Collapse wdCollapseStart

' After the paste the pasted text will be selected
Selection.Paste

' The pasted text starts at the old selection and
' extends to the end of whatever we just pasted
rngMark.End = Selection.End

Set PasteIt = rngMark
End Function


You can call it like this:

Public Sub TestMeX()
PasteIt.Select
End Sub

This does the paste and selects it!

HTH + Cheers - Peter


I need to paste some text from the clipboard, and then process just that
pasted text. I'm using selection.paste to do the pasting, is there any way
to easily identify the text that was just pasted (some range that is just
that text?).

thanks.

Chip
 

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