Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Regex in Word VBA

Posted on 2014-01-10
13
Medium Priority
?
1,314 Views
Last Modified: 2014-01-20
Hello,
     This is related to this question: http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Word/Q_28334735.html 

What I am trying to do is find text that matches this regex:
(ARTICLE)(\s+)(\d+)(\s+)(–)(\s+)(.*)(\n)

Open in new window

(The string ARTICLE followed by a space, followed by a digit, followed by a space, followed by an em dash, followed by a space, followed by any number of words, then a newline. An example is "ARTICLE 5 – DESCRIPTION...")

And this regex:
(\d+)(\.)(\d+)(?:\.)(?:\d+)(\s+)(.*)(\n)

Open in new window

(An integer, followed by a period, followed by another integer with an optional period and integer, followed by any text, and a newline. An example is "2.1.3 Subsection 2...")

If text matches, the macro should apply a style that includes heading level so I can generate a Table of Contents and populate the navigation pane. If my regex is incorrect, please correct me.

Thank you!
0
Comment
Question by:indigo6
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 3
13 Comments
 
LVL 15

Expert Comment

by:DrTribos
ID: 39772860
To give you an idea of the syntax, this will find up to Description...
(ARTICLE )[0-9]( )^=( )<[0-9A-z ]*>

Need to turn Wild Cards on in the search
0
 
LVL 15

Accepted Solution

by:
DrTribos earned 1500 total points
ID: 39772996
And this:

(ARTICLE )[0-9]( )^+( )<[0-9A-z ]*[^13]

will find the first paragraph (including marker) starting with "Article #"

note:
em dash is ^+
en dash is ^=
0
 
LVL 15

Assisted Solution

by:DrTribos
DrTribos earned 1500 total points
ID: 39773010
And this should do the second part...
[0-9]{1,}[.][0-9]{1,}[*][! ]( )<[0-9A-z ]*[^13]
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 39773064
You mention VBA, so I assume that you are implementing the VBScript regex objects.  In that case, your pattern string for the 2nd example should be:

(\d+)(\.)(\d+)(\.)?(\d+)?(\s+)(.*)(\n)

Not that DrTribos's recommendation is for using the search function built into Word, which is accessible via VBA.
0
 
LVL 15

Expert Comment

by:DrTribos
ID: 39773067
Ahhhh... I was wondering.  I've never used RegEx.
0
 

Author Comment

by:indigo6
ID: 39777322
DrTribos, actually that's fine, I was wondering about what the wildcard syntax would be! I tweaked the second one to be (ARTICLE )[0-9]( )^=( )<[0-9A-z ]*[^13] since it was an En dash, and I had to run a second round with (ARTICLE )[0-9][0-9]( )^=( )<[0-9A-z ]*[^13] to detect article numbers 10 and larger. But that worked great!

     The second one has me perplexed though. It detected strings like the following:
2.2 Access
2.7 Employee Time Off
21.11.3 Class Size (Unit 3)

But not:

5.2 No Lockout
5.4 Savings Clause

From what I can tell, they are formatted the same way. I'm not sure why it does not find these.

matthewspatrick, thanks for the VBA regex! How would I write a macro that changes the style of strings that match that regex?

Thanks!
0
 
LVL 15

Expert Comment

by:DrTribos
ID: 39778446
Sorry I thought I posted this yesterday - apparently it failed.  Here is some code to make the changes using the wild card...
Sub UpdateSections()

Dim para As Paragraph
Dim str As String

str = "(ARTICLE )[0-9]( )^=( )<[0-9A-z ]*[^13]"

ActiveDocument.Range.Select
    With Selection.Find
        .Text = str
        .Replacement.Text = ""
        .Forward = True
        .Font.Name = "Arial Narrow"
        .Font.Size = 24
        .MatchWildcards = True
        .Forward = True
        .Replacement.Style = "Heading 1"
        .Execute Replace:=wdReplaceAll
    End With

End Sub

Open in new window


You'll have to change the font names and sizes to suit... :-)
0
 
LVL 15

Expert Comment

by:DrTribos
ID: 39778450
Gah! My other comment is also missing - regarding the strange results... perhaps there is a double space or something confusing the issue.  Can you copy a few of the offending lines and paste them into a sample document?

Also, not sure if you are in the habit of using show - hide... the backward P thing on the home tab of the ribbon (paragraph marker in the paragraph section)... this will show spaces and might make diagnosis easier :-)
0
 
LVL 15

Expert Comment

by:DrTribos
ID: 39778456
Another possible reason that the text is not detected is because there is a soft return instead of a hard return... soft return looks like the symbol on the keyboard for the enter button. Hard return is the 'backward P' paragraph marker....  The wild card search is explicitly looking for a hard return (^13), not sure how other characters would impact...
0
 

Author Comment

by:indigo6
ID: 39783981
They seem to all be hard returns. Any other reasons why this could be?
0
 
LVL 15

Expert Comment

by:DrTribos
ID: 39784155
Pls copy the offending paragraphs to a sample document and upload.
Sorry for the brevity - on phone
0
 

Author Closing Comment

by:indigo6
ID: 39795552
I would have to strip too much of the info out, plus the requirements changed, so I'll close this question. It got me moving in the right direction though. Thanks!
0
 
LVL 15

Expert Comment

by:DrTribos
ID: 39796168
No worries - glad to help :-)
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question