Manipulate text in a "copied" area?

R

Robin Tucker

Hi there,


I am "copying" text from one location to another in my document, but before
I paste the text, I would like to manipulate it (remove some field codes for
instance). Is there any way I can reference the "copied" text and play
around with it before pasting it?

Here is my copy/paste code:

.....
.....

' Set the range to the position of the start and end field codes

Selection.SetRange fieldStart.Result.End, fieldEnd.Result.Start

' Copy between

Selection.Copy

' Collapse the selection to the end

Selection.Collapse Direction:=wdCollapseEnd

' and paste it into the document once for each record in the recordset.

For i = 0 To adoRS.RecordCount - 2

....... wish to edit the "copied" text here, perhaps to change a
field code or two......

Selection.Range.Paste
Next


.....
.....
 
J

Jezebel

You can't do it directly. But you could create a new, hidden document; paste
your text into it; massage it as needed, re-copy it ...
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Robin Tucker > écrivait :
In this message, < Robin Tucker > wrote:

|| Hi there,
||
||
|| I am "copying" text from one location to another in my document, but
before
|| I paste the text, I would like to manipulate it (remove some field codes
for
|| instance). Is there any way I can reference the "copied" text and play
|| around with it before pasting it?
||

Here is a "dirty trick":

Use a range object instead of a selection (they are easier to manipulate),
as in
Dim MyRange as Range
Set MyRange = Selection.Range

Then do whatever manipulation you want to the range, i.e. tweaking your
field codes, copy the range, undo the manipulations, paste the clipboard
content as many times as you want....

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
P

Peter Hewett

Hi Robin Tucker

Nope. You can either manipulate the data/text before you copy it or after you paste it.
You can't manipulate it while it's on the clipboard.

I'd do it something like this:

Public Sub Test()
Dim rngSource As Word.Range
Dim rngDestination As Word.Range
Dim rngModify As Word.Range
Dim fldEnd As Word.Field
Dim fldStart As Word.Field
Dim lngIndex As Long

' The fields map the range we want to copy
Set fldStart = ActiveDocument.Fields(1)
Set fldEnd = ActiveDocument.Fields(2)

' Text we are going to repeatedly paste
Set rngSource = fldStart.Result
rngSource.Collapse wdCollapseEnd
rngSource.End = fldEnd.Result.Start
rngSource.Copy

' We must specify where we're going to paste the text
Set rngDestination = ActiveDocument.Content
rngDestination.Collapse wdCollapseEnd

' Paste each record
For lngIndex = 0 To adoRS.RecordCount - 2
rngDestination.Paste
Set rngModify = rngDestination.Duplicate

' Use the rngModify object to manipulate the text you just pasted
rngModify.Font.Color = wdColorOrange

' Next paste must occur at the end of the current range
rngDestination.Collapse wdCollapseEnd
Next
End Sub

HTH + Cheers - Peter
 
D

Dave Neve

Hi

I can't get this code to run. On my machine, there is a 'non defined vriable
error' on line

For lngIndex = 0 To adoRS.RecordCount - 2

and adoRS is the highlighted object.

Could anyone help me over this little obstacle

Thanks
 
P

Peter Hewett

Hi Dave Neve

The code's incomplete, but because of the way the OP defined the variable name "adoRS" we
can tell that it's an ADO (ActiveX Data Object) record set object. To use this you need
to create a reference to "Microsoft ActiveX Data Objects 2.n Library", then:
Dim adoRS As ADODB.Recordset

HTH + Cheers - Peter
 

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