Avatar of cool12399
cool12399 asked on

cut & paste text in ms word, plus search/replace

Hi,

How would I cut and paste text from one msword document to a second msword document multiple times, and then find and replace? (I.e., I will be copying multiple sections from the first document to the second document, and then finding and replacing text in the second document).

i.e., using the MSWord Object Library
Set wordApp = CreateObject("Word.Application")

basically, I want to copy a selection of text from the first document, then paste it into the second. And do this multiple times.

2. I want to find and replace text in the second document (entire scope). How would I do that?
Visual Basic ClassicMicrosoft Word

Avatar of undefined
Last Comment
GrahamSkan

8/22/2022 - Mon
GrahamSkan

When you say 'selection', do you mean the user-selected text, i.e. the Selection object, or do you want to define the area to be copied in some other way.
It make a difference, because if you use CreateObject, you will not get the Word instance that is already open and hence the one with the Selection.

 
ASKER
cool12399

Hi Graham,

Thanks for your quick reply.

I am selecting the text via msword (not the user),  i.e., like this: (for now just testing the entire document)

Dim wordApp As Word.Application
Set wordApp = CreateObject("Word.Application")
Dim wordDest As Object
Set wordDest = CreateObject("Word.Application")
wordApp.ActiveDocument.Range.copy

I then want to 'paste' this text into the second document, and I am assuming it
is something like:
wordDest.Documents.Add DocumentType:=wdNewBlankDocument

but here I don't know how to 'paste' my selection into the 'wordDest'. I want it to 'append' to the
end of of the document. (For now I just want to use the entire range, later
I will be getting groups of text to paste)

And then what I want to to do is basically 'find' and 'replace' text in the entire scope of
the second document... any ideas?

Thanks!
GrahamSkan

This code illustrates how to get the selection from an already open Word document, copy it to a new document, replace some text on the new document, save and close it. It uses early binding, so you need a reference to the Microsoft Word Object Library.
Sub CopyAndReplace()
    Dim wdApp As Word.Application
    Dim wdDoc1 As Word.Document
    Dim wdDoc2 As Word.Document
   
    Set wdApp = GetObject(, "Word.Application")
    wdApp.Visible = True
    Set wdDoc1 = wdApp.ActiveDocument
    Set wdDoc2 = wdApp.Documents.Add
    wdApp.Selection.Copy
    wdDoc2.Range.Paste
    With wdDoc2.Range.Find
        .Text = "Old text"
        .Replacement.Text = "New text"
        .Execute Replace:=wdReplaceAll
    End With
    wdDoc2.SaveAs "C:\MyFolder|MyFile.Doc"
    wdDoc2.Close False
    Set wdDoc2 = Nothing
    Set wdApp = Nothing
End Sub

Open in new window

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
GrahamSkan

Sorry. I hadn't seen your comment, so my reply hasn't taken it into account.
GrahamSkan

Not too many changes. Your code create two instances of the Word application, which aren't needed.
Sub CopyAndReplace()
    Dim wdApp As Word.Application
    Dim wdDoc1 As Word.Document
    Dim wdDoc2 As Word.Document
   
    Set wdApp = CreateObject("Word.Application")
    wdApp.Visible = True
    Set wdDoc1 = wdApp.ActiveDocument
    Set wdDoc2 = wdApp.Documents.Add
    wdDoc1.Range.Copy
    wdDoc2.Range.Paste
    With wdDoc2.Range.Find
        .Text = "Old text"
        .Replacement.Text = "New text"
        .Execute Replace:=wdReplaceAll
    End With
    wdDoc2.SaveAs "C:\MyFolder|MyFile.Doc"
    wdDoc2.Close False
    Set wdDoc2 = Nothing
    Set wdApp = Nothing
End Sub

Open in new window

ASKER
cool12399

Hi --

Thanks for the reply... I'm not sure if you accidentally posted the same code twice...

But how do I get it to 'paste' at the end of the second document multiple times?
If I use the 'range.paste', it seems to overwrite whatever text I originally had. (Specifically I am opening up two MS word documents, I open the first, copy some text, 'paste' it into my destination, then close the first document, then open a 2nd ms word document, copy some text, and 'paste' that into my destination as well).

So how would I 'paste' the text multiple times, specifically 'inserting' my new text at the end of the 'destination' document?

Thanks!
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
GrahamSkan

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
cool12399

Excellent! Thanks! (that works) (I didn't know about the collapse! :P)

PS -- one last question -- how would I insert a 'page' break between the multiple pastes?
I.e., say I have

rng.Paste

then I want a 'page' break right after the text I just inserted, then I can do

rng.Paste

again, etc?

(I'll award the points shortly!) Thanks!
ASKER
cool12399

Hi Thanks! P.S., how would I add a 'line break' between the ranges? (Please see the question for details).

GrahamSkan

Thanks.

Line break can be done in many ways. This inserts a new paragraph.

rng.Collapse wdCollapseEnd
rng.Text = vbCr

This will do the page break

rng.Collapse wdCollapseEnd
rng.InsertBreak wdPageBreak
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck