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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Select Text In Microsoft Word 1

Status
Not open for further replies.

Kevsim

Instructor
Apr 18, 2000
385
AU
Using Microsoft Word, I wish to have VB code to do the following –
Select unknown text length after a given word then delete that text.
For example after the word Building: and delete what comes after on the same line.
I would appreciate your advise.
kevsim
 
Code:
public sub DeleteRndAfterBuilding()
set myrange = activedocument.Content
myrange.find.execute ("building")
myrange.setrange start:=myrange.end, end:=(myrange.end + Int((6 * Rnd) + 1))
myrange.delete
end
 
If by 'line' you mean paragraph, which I suspect you do, then you can do it with a single Find & Replace:

Find text: (building)*(^13)
Replace with: \1\2
Check "Use wildcards"
Press "Replace All"


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
 
mintjulep,
Thank you for the info.
In this case the word after Building: is a space then Little House, when I run your code it only removes the space and Lit, the rest of the text stays there.
When I speak of “what comes after on the same line”, I meant to the end of the paragraph.

The idea of the VB code I am writing is to place data into Word, Excel, MS Project and many others. If any data changes, the VB code can be rerun and all corrections made.

Is there a way to find the range of text after Building to the End of Paragraph then delete it.
In Excel this is very easy.
kevsim

TonyJollens,
Thank you for the info.
However I do not understand what you mean.
kevsim
 
No VBA. In Word, with your document open, press Ctrl+H to invoke the Find and Replace dialogue. This has two textboxes at the top, one labeled "Find what" - in this one enter [blue][tt](building)*(^13)[/tt][/blue] - and the other labeled "Replace with" - in this one enter [blue][tt]\1\2[/tt][/blue]. Below the textboxes there is a button labeled "More"; press this and the dialogue will expand to include several checkboxes, one labeled "Use wildcards": check this one. Finally press the button labeled "Replace All".

If you want VBA, you can record what you are doing.

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
 
TonyJollans,
Thank you for the info.
I tried as you stated but have mixed results.
sometimes it also selects the two paragraphs underneath as well as Building.
Other times it tells me the replace text is out of range.
kevsim
 
Occasionally the unique form of regular expression used by Find and Replace can produce odd results but in this case it's fairly straightforward.

The Find text: [blue][tt](building)*(^13)[/tt][/blue], looks for [blue][tt]building[/tt][/blue] followed by anything ([blue][tt]*[/tt][/blue]) followed by a paragraph mark ([blue][tt]^13[/tt][/blue]). The parentheses identify and delimit individual terms within it, [blue][tt](building)[/tt][/blue] as [blue][tt]\1[/tt][/blue], and [blue][tt](^13)[/tt][/blue] as [blue][tt]\2[/tt][/blue].

If it finds more than a paragraph, something is definitely odd. The most likely explanation is that your lines are not actually paragraphs. If you display paragraph marks (press Ctrl+* to toggle them on and off), are the (selected) lines separated by pilcrows (backward P shapes), or some other symbol?

Replace text out of range is, again, unusual. Could you have missed one of the parentheses out, by any chance?

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
 
kevsim, Tony is completely correct. Either your additional "lines" are not real paragraphs (they are not terminated by paragraph marks), or you have missed something with your syntax.

Tony's Find and Replace should work, does work. No VBA needed.

If you really do want to use VBA, here is an alternative:
Code:
Sub yadda()
Dim r As Range
Set r = ActiveDocument.Range
With r.Find
   Do While .Execute(FindText:="building", _
      Forward:=True) = True
      With r
         .Collapse 0
         .MoveEndUntil Cset:=vbCrLf
         .Delete
      End With
   Loop
End With
End Sub
This finds "building", collapses the found Range to just after "building", moves the End to just before the next paragraph mark, and deletes the range. Then moves on.

In other words, it deletes everything from "building" to the next paragraph mark. If the above deletes other "lines", then those are NOT paragraphs.

faq219-2884

Gerry
My paintings and sculpture
 
Fumei,
Thanks for the information, it works perfect, just what I required.
Also, if the text I wanted to delete was at a (or several) Tab position(s) how much different would the code be?

kevsim

TonyJollans
Still having a problem using Find and Replace, will keep trying until I find the answer.
It is definatly a paragraph where I am trying to make the change.
The word Building is a Bookmark and the text after that was written using VB.

kevsim
 
Wow! ".MoveEndUntil Cset:=vbCrLf" ?
That's one helluva nice construction!

Me needs to archive this post!
[thumbsup]

[navy]"We had to turn off that service to comply with the CDA Bill."[/navy]
- The Bastard Operator From Hell
 
[blue][tt].MoveEndUntil Cset:=vbCrLf[/tt][/blue] is, strictly, not the best, although it will work. The [blue][tt]Cset[/tt][/blue] parameter takes a string and will move the end of the Range to the first found character matching any in the string. [blue][tt]vbCrLf[/tt][/blue] is a string of two characters, Chr(13) and Chr(10) when the requirement, in this case, is simply to find a Chr(13); [blue][tt]Cset:=vbCr[/tt][/blue] is sufficient and more technically correct.

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
 
True. I usually use .expand unit:=wdparagraph or similar.
But I do like the construction per se; I've never seen that before, and it does have a certain aesthetic look and feel to me.
:)

[navy]"We had to turn off that service to comply with the CDA Bill."[/navy]
- The Bastard Operator From Hell
 
Tony is correct...as usual. vbCr works, and is more technically correct. Cset will find the CR - Chr(13) - before the Chr(10).

I would recommend playing with Cset before you use it seriously. You have to read Help (under the item MoveEndUntil) very carefully.

"Moves the end position of the specified range or selection until any of the specified characters are found in the document. If the movement is forward in the document, the range or selection is expanded."

My bolding. This is VERY significant!

Say you have a document with the total content:

Yadda yadda blah whatever.

Run the following.
Code:
Sub bleech()
Dim r As Range
Dim var
Set r = ActiveDocument.Range(0, 0)
   MsgBox r.Text
var = r.MoveEndUntil(Cset:="whatever")
   r.Select
   MsgBox var & "  " & r.Text
End Sub
If Cset is a string (which it is) and the range end is moved until "whatever", you may think the new range selected would be:

Yadda yadda blah

I.e. just before "whatever". Cset moves the range end to just before the found Cset.

Except..... Cset moves the end to the first found instance of ANY (and more importantly, the first) character in the Cset string. This is why Cset is rarely used (if at all) for more than a single character string.

The result of the code above will be a message box:

2 Y

2 being the returned number of characters moved. "Y" being the new range. Why? Because the first found instance of ANY of the characters in the Cset string...is the "a" of "whatever". and that is at:

Ya[/color red]dda yadda blah whatever.

Cset can be useful, but you really do have to know how it works.

faq219-2884

Gerry
My paintings and sculpture
 
BTW: Cset can also work backwards in the document. And just to confuse things, there is of course a MoveStartUntil...which can ALSO move forward, or backward.

Ummm, you get an error if you end up with Start > End.

faq219-2884

Gerry
My paintings and sculpture
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top