Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Selecting, Deleting, and Inserting Text 1

Status
Not open for further replies.

noHandlebars

Technical User
Jun 25, 2008
46
US
How can you select a specific section of text, delete that section, and then insert a new section in its place using a macro.

 
Which application ?
Did you try to macro-record when doing it manually ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Define "specific section of text". Specific how? By format? By location? By actual words?
Code:
Dim r As Range
Set r = ActiveDocument.Range
With r.Find
   .Text = "quick brown fox"
   .Replacement.Text = "yellow tall giraffe"
   .Execute Replace:=wdReplaceAll
End With
would deal with a "specific section of text" ("quick brown fox"), "delete it" and "insert a new section in its place". Although it does not really "delete it", it replaces it.

Essentially you seem to be asking about a Find and Replace operation.

Try recording a macro and when you have a specific (detailed) question, ask.

faq219-2884

Gerry
My paintings and sculpture
 
it's in microsoft word. finding a specific section of words and deleting them, then inserting a page from another document
 
So, what have you tried so far and where in your code are you stuck ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
You may also try actually answering questions when asked of you.
Define "specific section of text". Specific how? By format? By location? By actual words?

What you are generally asking, is quite easy to do.

That is, go to a specific location, replace that text with other text - the fact it is text from another document is not all that important. The fact that you use the word "page" IS important.

I will tell you why, if you tell me the answers to the questions. You can try to answer PHV's questions while you are at it. What have you tried so far, and where are you having any difficulty.

I also suggest you look at the FAQ for asking questions. It may help you....get answers.

faq219-2884

Gerry
My paintings and sculpture
 
ive tried using the record macro function to select the text i want to highlight and replace but when i then run the macro it does not select the correct text. The text is specific by LOCATION and the actual words that it is.
 
noHandlebars,

A little game:

I would like you to help me find a passage of text in a newspaper. It is specific by LOCATION and the actual words that it is.

That's all the information you get; do you think you can help me?

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 
I'm not sure what information you are looking for, I can give you word for word the text but that would be pointless. As I've said before i need to delete a certain section of text from a word document and insert it with page from another document. I've tried the record a macro function but its not working, but when it runs it does not select the right section that i selected during the recording. Any real help would be benificial.
 
Some way of identifying the text would be helpful. Is it in a certain style? Does it follow some trigger text? Is it always the same words? Or is it bound in quotes, or parentheses? Is it at the start of the document? Is it in a paragraph by itself? I don't know: you tell me. How do you visually identify it when you look at the screen?


Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 
To identify the text, it has the same heading all the time (i.e. Section C.1 ) and all of the section needs to be deleted until the next section (i.e. Section C.2).
 
There may well be better ways but this will find your text and replace it with something else. I don't know what you want to replace it with - or how you have it stored, so can't really expand on that:
Code:
[blue]    Dim TextStart   As Long
    
    With ActiveDocument.Content
        .Find.Execute FindText:="Section C.1"
        TextStart = .Start
        .Collapse wdCollapseEnd
        .Find.Execute FindText:="Section C.2"
        .Collapse wdCollapseStart
        .Start = TextStart
        .Delete
        .InsertAfter "Something else"
        .InsertParagraphAfter
    End With
[/blue]

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 
Thanks a lot that worked. For the insert, I need to insert a new page into the document at the point where the previous section was deleted. Is there a way to ensure a whole new page is inserted?
 
You can insert page breaks ...

Code:
[blue]    Dim TextStart   As Long
    
    With ActiveDocument.Content
        .Find.Execute FindText:="Section C.1"
        TextStart = .Start
        .Collapse wdCollapseEnd
        .Find.Execute FindText:="Section C.2"
        .Collapse wdCollapseStart
        .Start = TextStart
        .Delete
        [red].InsertBreak wdPageBreak[/red]
        .InsertAfter "Something on a new page"
        .InsertParagraphAfter
        [red].Collapse wdCollapseEnd
        .InsertBreak wdPageBreak[/red]
    End With[/blue]

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top