Consulting

Results 1 to 11 of 11

Thread: Number Field bookending a Style

  1. #1

    Number Field bookending a Style

    Sometime in my document I'd like to add Numbers to the Styles in numeric order of course.

    Maybe its a new style that replaces another one but I want the number to go on the first line of the paragraph to the left and to the right of that line. Possibly a field or something.

    I came across this formula in a macro that did something similar:
    "AUTONUMLGL \e "

    The routine is below

    [VBA] Sub AddNum()

    Selection.InsertBreak Type:=wdSectionBreakContinuous
    Selection.Style = ActiveDocument.Styles("Slug1")
    Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
    "AUTONUMLGL \e ", PreserveFormatting:=False
    Selection.TypeText Text:=vbTab
    Slug1.Show
    Selection.TypeText Text:=vbTab
    Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
    "AUTONUMLGL \e ", PreserveFormatting:=False
    Selection.TypeParagraph
    End Sub[/VBA]

    Can someone explain how this works and how I can do the same for my template? I don't want a form called Slug1 as this suggests. There are no forms in my template at the moment.

    Thanks for help!

  2. #2
    VBAX Regular
    Joined
    Sep 2004
    Posts
    65
    Location
    Greetings, Firstly you don't need the form so you can safely delete the line Slug1.Show.
    The code creates a new section, changes the style to the STYLE Slug1 (nothing to do with the form). A AUTONUM field is then inserted followed by a tab.

    My guess is that the Slug1 UserForm is a sort of input that allows the user to add whatever text is to go between the two tabs.
    If you replace the Style Slug1 with say Heading 1 which is a default style, and replace Slug1.Show with Selection.TypeText Text:="This is my sentence" you'll see exactly what this code does.

  3. #3
    Thanks Bilby,

    That makes sense.
    Now I want this macro to run through the whole document and find all my Scene Heading styles and add this number bookending it.

    Here's my code so far.

    [VBA] Sub AddNum()
    Selection.InsertBreak Type:=wdSectionBreakContinuous
    Selection.Style = ActiveDocument.Styles("Scene Heading")
    Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
    "AUTONUMLGL \e ", PreserveFormatting:=False
    Selection.TypeText Text:=vbTab
    Selection.TypeText Text:=vbTab
    Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
    "AUTONUMLGL \e ", PreserveFormatting:=False
    Selection.TypeParagraph
    End Sub[/VBA]

    It only adds a "1 1" at the begining of the document before the firs tScen Heading style.

    Can you help?

  4. #4
    VBAX Regular
    Joined
    Sep 2004
    Posts
    65
    Location
    VBA Fun NOT isn't it? I should use the range object but I'm more comfortable with Selection.
    anyway the following loops thro all paragraphs in the document

    [VBA]Sub AddNum()
    Dim oDoc As Document
    Dim pPara As Paragraph

    Set oDoc = ActiveDocument
    For Each pPara In oDoc.Paragraphs
    '' Go thro' all paragraphs action on "Scene Heading" styled paras only
    If pPara.Style = "Scene Heading" Then
    pPara.Range.Select
    With Selection
    ' collapse to start of para and insert field and tab
    .Collapse Direction:=wdCollapseStart
    .Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
    "AUTONUMLGL \e ", PreserveFormatting:=False
    .TypeText Text:=vbTab

    ' collapse to end of text and insert tab and field
    ' Paragraph selection includes the marker, move back one character
    pPara.Range.Select
    .MoveEnd Unit:=wdCharacter, Count:=-1
    .Collapse Direction:=wdCollapseEnd
    .TypeText Text:=vbTab
    .Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
    "AUTONUMLGL \e ", PreserveFormatting:=False
    End With
    End If
    Next pPara
    End Sub[/VBA]

  5. #5
    Thanks again Bilby...

    I got it to work by changing the name Scene Heading to ALL CAPS in the code.

    Here's another quirk. My SCENE HEADING paragraphs formats are:
    Norma + Font: All caps, Indent: Left 0.75" Right 0.75", Space before 12 pt

    I'm trying to get the numbering flush left where my margin starts:
    Left: 0.5"
    Right: 0.5"
    Yet keep the actual start of the paragraph at the indent.

    I was thinking of developing a new style maybe called SCENE HEADING NUM where it would flush right and the numbering would go flush right then tab or indent the paragraph to the right to 0.75". Is this possible? Also, how do you set the numbering to the flush right margin of Right: 0.5"???

    Thanks for all your help. It's getting there!

    FAK!

  6. #6
    VBAX Regular
    Joined
    Sep 2004
    Posts
    65
    Location
    Sorry I don't understand what your trying to do, maybe a sample would help?

    I've never tried Auto numbering on the right margin but I suppose its the same as 'left' numbering, you can check it out in Help.

  7. #7
    I tried to attach the word file in a message and it said it was wrong format or something.

    Can I email it to you?

  8. #8
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    You need to zip the Word document first. Then you will be able to attach it.

  9. #9
    Cool.

    I uploaded the template and a small sample. Basically, I want this numbering to book end the Scene Headings on the furthest left margin and furthest right margin as specified on the page margin setup. The headings need to remain in the same position at all times. If a field is 2 or 3 digits I don't want the headings to move to the right an extra 1 or 2 spaces.

    Of course this will be done as a Macro and reversed when need be. The document is sometimes printed without the numbering and sometimes with.

    Is this possible.

    It's a Screenplay Template that I'm trying to build here.

    Thanks,
    FAK

  10. #10
    VBAX Regular
    Joined
    Sep 2004
    Posts
    65
    Location
    Sorry FAK,

    Your requirement is beyond me. With the indents as they are, the only way I can see of getting a number on the left hand margin is with an outline numbered style (which is easy enough). But to also get numbering on the right margin automatically is something I've never had to do.

    The only way I can see of doing it is to include a tab position on the right hand margin and use the TabKeyMacroMove() to insert a tab & field on the margin.

    I'll have a closer look at it tonight if I've got the time, but hopefully someone with more experience/creativity can help you out.

  11. #11
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    You are not asking for much are you? It COULD be done, but it definitely not be trivial, especialy if you wanted to be able to toggle. And very especially if you want the text in between the numbering to remain constant. You specify if the digit is 3 characters you do NOT want the following text to move over 3 characters. This is not the way word processors function. You insert text, and any text to the right to moved over the amount of the inserted text.

    Essentially, you need to put two form fields at the beginning and end of the first line of the paragraph with the specified style.

    go to the start of the first line
    pick up the Tab stops, and indents, for the style and store as PUBLIC variables
    run logic on the tab stops and indents which includes checking for form fields at the begining of the line.

    The logic is basic:

    IF there is a form field, THEN
    the numbering is in place, so
    delete the form field
    adjust the tab stops or indents
    ELSE there is no form field
    the numbering is not in place
    insert a form field at the line start
    adjust the tab stops
    insert a form field at line end


    Maybe use the SEQ field, rather than form field. It would be complicated.

    Your sample doc does not really help it does not show EXACTLY what it is supposed to look like. There is no numbering on the right for example.

    Can you duplicate what it is to look like?

Posting Permissions

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