indigo6
asked on
Apply Styles to Existing Manually Formatted Document
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!
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!
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
So something like this:
My regex is not strong, so I don't know for sure if that would work.
Thank you!
ARTICLE XX – TEXTand the sections are always:
X.Xwith an optional third digit, like
X.X.X
So something like this:
(ARTICLE) (\\s+) (\\d+) (\\s+) (–) (\\s+) ((?:[a-z][a-z]+)|(\\s+))+
My regex is not strong, so I don't know for sure if that would work.
Thank you!
ASKER
Actually something more like this for the article numbers:
And then something like this for the sections:
This works in my regex tester, but it doesn't seem to work in find and replace in Word using wildcards.
(ARTICLE)(\s+)(\d+)(\s+)(–)(\s+)((?:[A-Z][A-Z]+)|(\s+))+
And then something like this for the sections:
(\d+)(\.)(\d+)(?:\.)(?:\d+)(\s+)(.*)
This works in my regex tester, but it doesn't seem to work in find and replace in Word using wildcards.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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!
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.
Sorry for the brevity using phone.
ASKER
No problem, I believe it is a field code. Thanks!
The field code basically displays and formats text that has a "heading style". Same thing for navigation panel. Need to put headings in first.
ASKER
Right, but I would like to populate the navigation pane using that data. Is that possible?
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
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
ASKER
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!
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 :-)
ASKER
Ok, so how would I do that? Is there a way to select all the text that had field codes?
Can be done using vba. What field types are you targeting? Rclick and show field codes.
Sorry for the brevity - on phone
Sorry for the brevity - on phone
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
Is the "2" the heading level that you wish to apply? Is this field in the Table of Contents or elsewhere in the document?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Oh... it will fail if the \L or number is missing
ASKER
This all looks good. My question changed a bit throughout, so a solution based on this should work. Thanks!
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.
Open in new window