How do I select what I've just pasted?

E

Ed

I open one doc, select all its contents and copy. I then activate a second
doc and paste. I want to then select everything I've just pasted in and
work with it. What's the best way to do that?

Ed
 
J

Jean-Guy Marcil

Ed was telling us:
Ed nous racontait que :
I open one doc, select all its contents and copy. I then activate a
second doc and paste. I want to then select everything I've just
pasted in and work with it. What's the best way to do that?

Try something like this:
With range and document objects, no need to activate or use the selection
object which will quickly lead to pulling your hair out after a while...

'_______________________________________
Dim doc1 As Document
Dim doc2 As Document

Set doc1 = Documents(2) 'use document names instead of numbers...
Set doc2 = Documents(1)

doc1.Range(0, doc1.Range.End - 1).Copy
'-1 to remove the last paragraph mark which is not a good idea to copy/paste
'because it carries section formatting information and document wide
settings
'you may want to add a temp ¶ at the end, copy, then remove it

Dim pasteRge As Range

Set pasteRge = doc2.Range
With pasteRge
.Collapse wdCollapseEnd
.Paste
'Do what you want with the pasted stuff, e.g.:
.Font.Color = wdColorBlueGray
End With

Set doc1 = Nothing
Set doc2 = Nothing
Set pasteRge = Nothing
'_______________________________________

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

Ed

I have to open a number of documents, copy all their info (about 18-20
lines), and paste it into another doc at the last paragraph. I then need to
take the lines I've just pasted and convert to a table and apply formatting.
I had the macro to convert to table and so forth, when I realized I had a
LOT of these things to do, and opening, copying, and pasting manually was
going to drive me nuts! I got it down to opening the table info docs,
copying, and pasting, and I had the conversion macro.

I tried setting a range to the last paragraph of the document, pasting into
that range, and then working with it, but I can't get a "handle" on what's
been pasted to format it. When I tried range.Select, the text I had just
pasted in was not selected, which would mean it wasn't part of the range,
right? The only thing I can see I did different was I did not have the
Collapse in the code. Is that my critical error?

Ed
 
J

Jay Freedman

Hi Ed,

I wouldn't dink around with copy/paste. Besides being hard to work
with, it clobbers anything you might have had on the clipboard.
Instead, use the range object's .FormattedText property to transfer
the stuff directly. The advantage is that the text goes *into* the
range instead of *before* or *after* it.

Sub foo()
Dim doc1 As Document, doc2 As Document
Dim oRg1 As Range, oRg2 As Range

Set doc1 = ActiveDocument
Set doc2 = Documents("Document2") ' replace with
' Documents.Open(...) as needed

Set oRg1 = doc1.Range
oRg1.MoveEnd unit:=wdCharacter, Count:=-1
Set oRg2 = doc2.Paragraphs.Last.Range

oRg2.FormattedText = oRg1.FormattedText

With oRg2
.Bold = True
.Font.Size = 14
End With
End Sub
 
E

Ed

Wow! Thank you, Jay. I hadn't thought of using ranges between documents.
This can open up new worlds.

Ed
 

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