PDA

View Full Version : script for adding headings in word



akmon
01-05-2011, 02:49 AM
Hello

When exporting a document from a tool i use to word, the headings are lost and i have to manually put them(bc I want to create a Table of Contents). the formatting for the headings are there(e.g 1.1 Title, 1.2.1 etc) but they appear as "body text+ bold". It's a large document so i have to manually go through each title and apply the heading formatting.
Is there any way i can automate this process in VB?
If so can you please help?

Thank you!

Regards

akmon
01-05-2011, 07:53 AM
Sub macro()
With ActiveDocument.Content.Find

.Style = "Body Text"
.Font.Bold = True

Do While .Execute(FindText:="[0-9] ", Forward:=True, _
Format:=True, MatchWildcards:=True, MatchWholeWord:=True) = True
With .Parent
.Style = "Heading 1"
End With
Loop
End With

End Sub

I came up with the above code for applying "heading 1"
the problem is that it finds all numbers followed by space(for eg it finds 1.1 and applies heading 1 to it)I only want to find rows that start with a number and are followed by a space, so i can apply heading 1
eg
1 Info <<Heading1 here>>
1.1
1.2


2 Products <<also here>>
etc
Any help is appreciated
Regards

gmaxey
01-05-2011, 07:55 AM
You may need to make adjustments more adequately define the conditions but something like this might work:

Sub ScratchMacro()
'A quick macro scratch pad created by Greg Maxey
Dim oPar As Word.Paragraph
For Each oPar In ActiveDocument.Paragraphs
If oPar.Style = "Body Text" Then
If oPar.Range.Font.Bold = True Then
oPar.Style = "Heading 1"
End If
End If
Next
End Sub

akmon
01-05-2011, 08:04 AM
thanks for the reply
this applies heading 1 to every paragraph
i only want the ones that start with "[0-9] "

Tinbendr
01-05-2011, 08:51 AM
Maybe "[0-9] [A-z]^13"

macropod
01-05-2011, 02:11 PM
I'd suggest:
FindText:="^13[0-9]"
It won't 'Find' the first paragraph in the document, but that's a small price to pay (I wouldn't expect it to be a paragraph you'd put in a TOC anyway).

akmon
01-06-2011, 02:59 AM
i tried adding FindText:="^13[0-9]"
however it's not finding anything
other suggestions?

fumei
01-06-2011, 10:06 AM
Post your full current code.

macropod
01-06-2011, 03:12 PM
Even better, attach to your post a stripped-down copy of the document with enough data for us to see what you're working with - with the macro in it.