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

Finding blocks of text in VBA 1

Status
Not open for further replies.
Jun 5, 2002
417
AU
Hi,

I get documents where the numbering of points is part of the text. I have a macro to change the relevant text to being formatted as numbered, but I have to find each occurrence.

How do I set up a macro which will find and select all text between "^p1." and "^p^p" automatically so I can insert a call to my macro, and repeat this exercise for all occurrences in a document?

Thanks in advance.

Peter Moran
 
Hi Peter,

Using a wildcard Find:
Find = ^13[0-9]{1,2}.[!^13]{1,}^13^13
This will find any paragraph break followed by a 1-2 digit number, followed by a string of text and two further consecutive paragraph breaks.

Cheers
Paul Edstein
[MS MVP - Word]
 
Hi Paul,

Many thanks, but not quite what I needed.

It actually selects from the last number preceeding the two para marks, instead of from the first no to the 2 paras.

eg:

1.
2.
3. ^p^p

It selects from "3." to the 2 paras instead of from "1." - the start will always be "^p1.".

I will study wildcards and should be able to modify accordingly.

Thanks again.

Peter Moran
 
Hi peter,

Your original post said:
find and select all text between "^p1." and "^p^p" automatically
The code I posted does that. It's also more flexible in that the # doesn't have to be 1. If you don't want that flexibility, you could use:
Find = [^13]1.[!^13]{1,}^13^13

Your reply that:
It actually selects from the last number preceeding the two para marks, instead of from the first no to the 2 paras.
Is wrong. The first character selected is a paragraph mark (as per your specification) and the rest of what gets selected is as per my previous post. If the "^p^p" pair followed the "^p1." the Find expression would have found it.

What you latest post seems to be saying is that there's actually more text between the "^p^p" pair that you didn't previously mention. In that case, try:
Find = [^13]1.[!^13]{1,}^13[!^13]{1,}^13


Cheers
Paul Edstein
[MS MVP - Word]
 
Hi Paul,

Thanks again for your efforts. Maybe I have not explained my need very clearly, so I will try again.

Here is the text in my documents with "^p" showing where the paragraph marks are at present:

Prior paragraph text.^p
^p
1. Text text text.^p
2. Text Text text.^p
3. Text text text text.^p
^p
Next para text.^p

Currently I select the para before the 1. to the two paras at the end of the 3. para and then run my macro to make points 1, 2, and 3 numbered formats and remove all the numbers, dots and spaces from the start of each paragraph.

I have tried the examples you provided on my text.

The first solution (wildcard) only selects as above from point 3 to the end in every instance, not from point 1.

I have been unable to get anything selected by either of your last two suggestions.

I do appreciate your assistance.

Regards,

Peter Moran

 
Hi Peter,

Are the paragraphs always numbered 1., 2., 3.?

If they are, you can use a wildcard Find/Replace coded as:
Find = 1.[ ^t]{1,}(*^13)2.[ ^t]{1,}(*^13)3.[ ^t]{1,}(*^13)
Replace = \1\2\3
and apply whatever Style you want for the replacement text.

No vba required.

If they are not, you can use a wildcard Find/Replace coded as:
Find = [0-9]{1,2].[ ^t]{1,}(*^13)
Replace = \1
and apply whatever Style you want for the replacement text. This will find any paragraph starting with a 1-2 digit number followed by a period then any number of spaces & tabs before the text and strip off everything before the text.

Again, no vba required.

Cheers
Paul Edstein
[MS MVP - Word]
 
Hi Paul,

Thanks again for your contribution, but still no luck here with your result, maybe because I indicated there was a space after the "1." which I have double checked and I was in error. My apologies.

Let me try again.

I want to select all characters inclusive between "^p1." (para mark followed by "1" and ".") and two paras together "^p^p" which always comes at the end of the numbered text.

Sometimes there are only three points to be converted, but variable with sometimes up to 15, but on my above definition that is not significant.

Regards,

Peter Moran

 
Hi Paul,

I have found that a wildcard search using "[^13]1.*[^13]{2}" does exactly what I want in finding the first selection and enables me to run the macro to change to numbered format.

However when I try to use Shift+F4 to repeat the search it does not work, at least not on Word 2K. The Find window shows "^p^#." which I suspect is only the first part of my search to find the next selection.

I would have to use Ctrl+F and then paste the search text each time until all occurrences have been found.

I may have up to 20 sets of text in a document that I want to change. Hence my pursuit of a solution which will find all occurrences in a document and change them.

Thanks again for your help, Paul.

Regards,

Peter Moran
 
Hi Peter,

If find your problem description ambiguous at best.

In Word's Find/Replace parlance, '^p' only represents a paragraph break, and that is only ever a complete paragraph if there's no text in the paragraph. So, what do you actually see with Word's formatting display turned on? For example -

1. Some Text¶


2. Some Text¶


3. Some Text¶


This approximates what you're describing when you say:
I want to select all characters inclusive between "^p1." (para mark followed by "1" and ".") and two paras together "^p^p" which always comes at the end of the numbered text.
or maybe:

Some Text¶

1. Some Text¶
2. Some Text¶
3. Some Text¶

Some Text¶
This approximates what you're describing when you say:
Prior paragraph text.^p
^p
1. Text text text.^p
2. Text Text text.^p
3. Text text text text.^p
^p
Next para text.^p
Clearly, the two scenarios are quite different and a Find expression tailored to one can't be expected to work for the other.

Since it now seems there is no space after the '1.', try deleting the '[ ^t]{1,}' string(s) from the last set of Find expressions.

If that doesn't work, you're going to have to post back with a clear description of what you're working with (eg an actual copy of some of the text, with simulated paragraph breaks as per the above)

Cheers
Paul Edstein
[MS MVP - Word]
 
Hi Peter,
I have found that a wildcard search using "[^13]1.*[^13]{2}" does exactly what I want in finding the first selection and enables me to run the macro to change to numbered format.
If that works,then using:
Find = [0-9]{1,2}.(*^13)
Replace = \1
with the appropriate Style as part of the replacement parameter, Word will strip off the numbers from all such paragraphs and apply the Style, with its numbering format, to the paragraphs concerned. You don't need vba for this.

Cheers
Paul Edstein
[MS MVP - Word]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top