Consulting

Results 1 to 9 of 9

Thread: script for adding headings in word

  1. #1
    VBAX Newbie
    Joined
    Jan 2011
    Posts
    4
    Location

    script for adding headings in word

    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

  2. #2
    VBAX Newbie
    Joined
    Jan 2011
    Posts
    4
    Location
    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

  3. #3
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    You may need to make adjustments more adequately define the conditions but something like this might work:

    [VBA]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
    [/VBA]
    Greg

    Visit my website: http://gregmaxey.com

  4. #4
    VBAX Newbie
    Joined
    Jan 2011
    Posts
    4
    Location
    thanks for the reply
    this applies heading 1 to every paragraph
    i only want the ones that start with "[0-9] "

  5. #5
    VBAX Expert Tinbendr's Avatar
    Joined
    Jun 2005
    Location
    North Central Mississippi (The Pines)
    Posts
    993
    Location
    Maybe "[0-9] [A-z]^13"

  6. #6
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    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).
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  7. #7
    VBAX Newbie
    Joined
    Jan 2011
    Posts
    4
    Location
    i tried adding FindText:="^13[0-9]"
    however it's not finding anything
    other suggestions?

  8. #8
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Post your full current code.

  9. #9
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    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.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •