Cut/Paste text?

BaseBallBatBoy

New Member
Joined
Sep 12, 2011
Messages
24
Hi,

So far I wrote into a word doc line after line. Just like this:

Code:
With wrdDoc[INDENT]        .range(0).Style = .Styles(wdStyleHeading1) 
        .Content.InsertAfter "HEAD foo"
        .Content.InsertParagraphAfter
        
        .Content.InsertAfter "foo"
        .Content.InsertParagraphAfter
[INDENT]Set wrdTable = wrdDoc.Tables.Add(range:=.range(.Characters.Count - 1), NumRows:=1, NumColumns:=2)
[/INDENT]        With wrdTable
            .cell(1, 1).range.InsertAfter "Name"
            .cell(1, 2).range.InsertAfter "foo"[INDENT]End With
[/INDENT]        With .Paragraphs(.Paragraphs.Count).range
                .Collapse Direction:=wdCollapseEnd
                .InsertBreak Type:=wdPageBreak
        End With
        
        .range(.Characters.Count - 1).Style = .Styles(wdStyleHeading1)
        .Content.InsertAfter "HEAD bar"
        .Content.InsertParagraphAfter

        .Content.InsertAfter "bar"
        .Content.InsertParagraphAfter
[INDENT]Set wrdTable = wrdDoc.Tables.Add(range:=.range(.Characters.Count - 1), NumRows:=1, NumColumns:=2)
[/INDENT]        With wrdTable
            .cell(1, 1).range.InsertAfter "Last name"
            .cell(1, 2).range.InsertAfter "bar"[INDENT]End With[/INDENT][/INDENT]End With

This works perfect so far. But now I no longer open a new document and just write, instead I open an existing one, search for a keyword which I would like to replace by some text (with text i mean text with some styles applied to them, different tables, line breaks, page breaks and so on, just like the output of the snippet before). But of course it doesn't work as I expected. This is how I search for a keyword to replace it:

Code:
    toReplace = "[[MYKEYWORD]]"

    For Each myParagraph In wrdDoc.Paragraphs
        If InStr(1, myParagraph.range.text, toReplace, vbTextCompare) <> 0 Then
' now here should be my new text  
        End If
    Next

The problem here is that I'm no longer at the end of the file but somewhere right in the middle. So InsertAfter doen't work. Also I have now a Word.Paragraph object instead of a Word.Document object

Do you have an idea what I could do to replace my keyword with the output my first snippet would produce?

My idea was to add all the text at the end of the document and define a range object over it. Then cut it out and paste it where it belongs:

Code:
For Each myParagraph In wrdDoc.Paragraphs
        If InStr(1, myParagraph.range.text, toReplace, vbTextCompare) <> 0 Then
           
            With wrdDoc
                Set startCopyRange = .range(.Characters.Count)
                
                .Content.InsertAfter "THIS IS SOME TEXT ADDED AT THE VERY END OF THIS DOCUMENT"
                
                Set endCopyRange = .range(.Characters.Count)
                
                Set myRange = .range(startCopyRange, endCopyRange)
                myRange.Cut
            End With

            myParagraph.range.Paste
        End If
    Next

But this cut/paste stuff doesn't do anything.... Maybe I'm simply doing it the wrong way, I don't know.

Thanks in advance, your help is very much appreciated!

Regards
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
This was posted in the Excel Questions forum but doesn't appear to me to relate to Excel so I have moved it to the General Excel Discussion & Other Questions forum. If I am wrong, please advise.
 
Upvote 0
This was posted in the Excel Questions forum but doesn't appear to me to relate to Excel so I have moved it to the General Excel Discussion & Other Questions forum. If I am wrong, please advise.

Those operations are in a Word document, but the content I'm adding there comes out of Excel. So it's some kind of a hybrid. But as long as someone has a good idea to solve my issue, I don't care to which forum it belongs ;)
 
Upvote 0

Forum statistics

Threads
1,225,606
Messages
6,185,956
Members
453,333
Latest member
BioCoder84

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top