headings format/RegEx

M

major

I was curious whether there was regular expressions in vba similar to perl.
This is what I want to do.
I have a documents of 150 pages long. I am trying to programmatically format
it. I am looping through paragraphs to apply heading styles. All headings
begin with section numbers. For ex:
2.4 Overview
---body text
2.4.1 Intro
---body text
2.4.1.2 etc
Now I want to apply heading styles based on numbers. So if paragraph begins
with #.(1 number) apply heading 1, if #.(2 numbers) apply heading 2. The
question is how to pattern search for paragraph begins with number followed
by 1(or 2 #'s etc)? This isn't probably the best method to this either, if
someone had an alternative logic it would be helpful.

Many thanks in advance.
 
H

Helmut Weber

Hi,
see
http://word.mvps.org/FAQs/General/UsingWildcards.htm
You might need something like:
^013[0-9]{1,}.
Paragraph-mark followed by 1 or more
digits followed by a period (English Version)

Though one might consider another approach using
for each paragraph ...
and checking whether it starts with a sequence
of digits etc...

Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word 2002, Windows 2000
 
K

Klaus Linke

Plus, you might insert tags in the wildcard search, and then apply styles in a regular replacement.
Say,
Find what: (^013)([0-9]@.[0-9]@[ ^t^s])
(this matches a paragraph mark ¶, followed by numbers, a dot, numbers, and whitespace)
Replace with: \1<H2>\2
(then replace <H3> with style="Heading 3", then replace <H3> with nothing)

Greetings,
Klaus
 
M

major

Thanks for all your help!

But turns out I might not end up using this logic after all. I am new to VB
and need help with another apporach I want to take.

This is what I intentially aim to do, I want to automatically apply styles
to paragraphs in the document. Styles vary from
headings/tables/footnotes/references etc. Now there isn't a set way for me to
determine what is heading and what is body text. So this is what I want to do.

I want to iterate thru all paragraphs is document and display in grid (in
userform) with drop down list populated with styles, preview of text in
paragraph, and allow users to choose style they want.

Design of Grid:
Paragraph Index Style-used Preview Style-New Apply

Is this possible? Can someone help me by pointing me to right place? Like I
don't know how to display in grid (would it be text box in form?)

Thanks once more!

~ MS


Klaus Linke said:
Plus, you might insert tags in the wildcard search, and then apply styles in a regular replacement.
Say,
Find what: (^013)([0-9]@.[0-9]@[ ^t^s])
(this matches a paragraph mark ¶, followed by numbers, a dot, numbers, and whitespace)
Replace with: \1<H2>\2
(then replace <H3> with style="Heading 3", then replace <H3> with nothing)

Greetings,
Klaus


Helmut Weber said:
Hi,
see
http://word.mvps.org/FAQs/General/UsingWildcards.htm
You might need something like:
^013[0-9]{1,}.
Paragraph-mark followed by 1 or more
digits followed by a period (English Version)

Though one might consider another approach using
for each paragraph ...
and checking whether it starts with a sequence
of digits etc...

Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word 2002, Windows 2000
 
H

Helmut Weber

Hi Major,

everything a computer can do, is possible for a computer.
You may add all the features of Autocad to Word,
if you got a billion dollars to spend. ;-)

If you are new to VB, and here it is VBA, then,
I'd say, hardly possible with justifiable effort.
But if you make it an obsession, yes.

How about this idea, as the task pane for formatting
offers a lot of what you are aiming for.
Switch the taskpane for formatting on:

Dim l As Long
Dim r As Range
Set r = ActiveDocument.Range
With ActiveDocument.Paragraphs
For l = 1 To .Count
.Item(l).Range.Select
Wait 2
r.End = Selection.Range.End
l = r.Paragraphs.Count
Next
End With

Public Sub Wait(ByVal aNum As Integer)
Dim aTim As Single
aTim = Timer
While Timer < aTim + aNum
DoEvents
Wend
End Sub

This would give you 2 seconds to apply a formatting.
If you're not that fast, increase the time to wait,
or, select the paragraph in question again.
A case, where resetting a loop aviable in a loop
might be helpful.

Not suitable for end users, of course.

Greetings from Bavaria, Germany

Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
M

major

Helmut,

Thank you very much. This does exactly what I wanted..thanks soo much. I
really like this forum, in a matter of few days I have learnt quite a lot
about VBA programming...which is impressive considering I am a scientist with
no previous serious programming experience :)

Keep it up!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top