Solved

Apply Styles to Existing Manually Formatted Document

Posted on 2014-01-09
22
369 Views
Last Modified: 2014-01-16
Hello,
     I have a 900 page document that has manually formatted sections. I don't want to go through and change the styles for each of these sections so I can view them in the navigation pane. All the section headings have a common format of 14pt Bold Arial. Is there a way or a macro to tell Word to apply a custom style to test formatted in this way?

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
  • 12
  • 9
22 Comments
 
LVL 32

Assisted Solution

by:Paul Sauvé
Paul Sauvé earned 100 total points
ID: 39769315
>>All the section headings have a common format of 14pt Bold Arial.

Do all these section headings have a unique paragraph style? If so, you can use Find and Replace to modify the document.

If ONLY the section headings use 14pt Bold Arial, then you can also use Find and Replace to modify.
0
 
LVL 15

Expert Comment

by:DrTribos
ID: 39770228
I'd use the solution proposed by Paulsauve, however as stated it does require ONLY the section headings to be 14pt Bold Arial - i.e. Other text may also be converted.

Failing that - if you know that the start of each section has a heading (or that headings are only at the start of each section) then you could use a macro that converts the first paragraph in each section to a heading, this would be quick.

The approach below looks only at the first paragraph in each section.
Sub Headings()

Dim oSect As Section

For Each oSect In ActiveDocument.Sections
If oSect.Range.Paragraphs(1).Range.Font.Size = 14 And oSect.Range.Paragraphs(1).Range.Font.Bold = True And oSect.Range.Paragraphs(1).Range.Font.Name = "Arial" Then

oSect.Range.Paragraphs(1).Range.Style = "Heading 1"

End If
Next oSect

End Sub

Open in new window

0
 

Author Comment

by:indigo6
ID: 39772039
The find and replace worked well at first, but then there were other parts of the document that happen to be 14pt Arial Bold. I'm thinking though, is it possible to maybe use regex to match? The article sections are always formatted like
ARTICLE XX – TEXT
and the sections are always:
X.X
with an optional third digit, like
X.X.X

So something like this:
(ARTICLE) (\\s+) (\\d+) (\\s+) (–) (\\s+) ((?:[a-z][a-z]+)|(\\s+))+ 

Open in new window


My regex is not strong, so I don't know for sure if that would work.

Thank you!
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:indigo6
ID: 39772117
Actually something more like this for the article numbers:
(ARTICLE)(\s+)(\d+)(\s+)(–)(\s+)((?:[A-Z][A-Z]+)|(\s+))+

Open in new window


And then something like this for the sections:
(\d+)(\.)(\d+)(?:\.)(?:\d+)(\s+)(.*)

Open in new window


This works in my regex tester, but it doesn't seem to work in find and replace in Word using wildcards.
0
 
LVL 15

Assisted Solution

by:DrTribos
DrTribos earned 400 total points
ID: 39772821
Ok... these are great when you get them working; I think word requires slightly different formatting to what you used.  Have you seen this MS this MS Office site or this on the MPV site?
0
 

Author Comment

by:indigo6
ID: 39777330
I have seen those, Word's wildcard syntax is a little foreign to me. I have actually found that each section has invisible text used for creating a table of contents. Is there a way to use that to populate the navigation pane? Thanks for your help!
0
 
LVL 15

Expert Comment

by:DrTribos
ID: 39777753
When you say invisible text... do you mean field codes,  hidden text,  the mark up codes, or something else?
Sorry for the brevity  using phone.
0
 

Author Comment

by:indigo6
ID: 39777785
No problem, I believe it is a field code. Thanks!
0
 
LVL 15

Expert Comment

by:DrTribos
ID: 39777801
The field code basically displays and formats text that has a "heading style". Same thing for navigation panel. Need to put headings in first.
0
 

Author Comment

by:indigo6
ID: 39777863
Right, but I would like to populate the navigation pane using that data. Is that possible?
0
 
LVL 15

Expert Comment

by:DrTribos
ID: 39778441
Yes... once the headings are in the document (as Heading Styles) they should automatically appear in the navigation pane...  sorry not sure I understand where your question is coming from
0
 
LVL 15

Expert Comment

by:DrTribos
ID: 39778460
Macro like this will help with applying headings:
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

0
 

Author Comment

by:indigo6
ID: 39779965
No Problem, I was just asking if I could use the information in the field code to populate the Navigation Pane. So like adjust heading styles everywhere there is a field code. Sorry if I was being vague!
0
 
LVL 15

Expert Comment

by:DrTribos
ID: 39780460
You can do that via the styles pane, but only once the styles are applied.  Incidently you can also apply styles via the styles pane :-)
0
 

Author Comment

by:indigo6
ID: 39783992
Ok, so how would I do that? Is there a way to select all the text that had field codes?
0
 
LVL 15

Expert Comment

by:DrTribos
ID: 39784161
Can be done using vba.  What field types are you targeting? Rclick and show field codes.
Sorry for the brevity - on phone
0
 
LVL 15

Expert Comment

by:DrTribos
ID: 39784229
Back at PC - this might help:
Sub FindFields()

Dim oFld As Field
Dim strHead As String

For Each oFld In ActiveDocument.Fields
If oFld.Type = wdFieldRef Then
' Apply Style based on other conditions?
strHead = InputBox("Enter a heading level for this text" & vbCr & vbCr & oFld.Code.Paragraphs(1).Range.Text)
If strHead = "0" Or strHead = "" Then
Else
oFld.Code.Paragraphs(1).Style = "Heading " & strHead
End If
End If
strHead = 0
Next oFld

End Sub

Open in new window

0
 

Author Comment

by:indigo6
ID: 39784283
Here is an example of the field code. Thanks!
Field code
0
 
LVL 15

Expert Comment

by:DrTribos
ID: 39784307
Is the "2" the heading level that you wish to apply?  Is this field in the Table of Contents or elsewhere in the document?
0
 
LVL 15

Accepted Solution

by:
DrTribos earned 400 total points
ID: 39784341
This should do the job:
Sub FindFields()

Dim oFld As Field
Dim strHead As String

For Each oFld In ActiveDocument.Fields
If oFld.Type = wdFieldTOCEntry Then
' Apply Style based on other conditions?
'strHead = InputBox("Enter a heading level for this text" & vbCr & vbCr & oFld.Code.Paragraphs(1).Range.Text)
'If strHead = "0" Or strHead = "" Then
'Else
oFld.Code.Paragraphs(1).Style = "Heading " & Mid(oFld.Code.Text, InStr(oFld.Code.Text, "\L ") + 4, Len(oFld.Code.Text) - InStr(oFld.Code.Text, "\L ") - 5)
'End If
End If
strHead = 0
Next oFld

End Sub

Open in new window

0
 
LVL 15

Expert Comment

by:DrTribos
ID: 39784344
Oh... it will fail if the \L or number is missing
0
 

Author Closing Comment

by:indigo6
ID: 39786297
This all looks good. My question changed a bit throughout, so a solution based on this should work. Thanks!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…

732 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