Results 1 to 4 of 4
  1. #1
    New Lounger
    Join Date
    Jun 2012
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Help with creating a chapter name and number from a form

    Hey guys,

    I have this code which works fine for the most part.

    It is designed that when you open the doc the macro runs with a form asking for some info to fill in (chapter number, report name and chapter name), It uses this info to create a heading with the the chapter number and name, while the report name is entered in the footer.

    The problem I have is this, if i type in for example 8 as the chapter number is creates 7 blank pages then put the header on the 8th page. Is there a way to make it just create the header on the first page without the blank pages.

    Thanks

    Code:
    Private Sub cmdok_Click()
    Dim HdFt As HeaderFooter
    start:
    'check is chapternumber details have been filled in
    If Me.tbchapternumber.Value = "" Then
      GoTo start
    Else
      Unload Me
      'check if chapter name field has been filled in
      If Me.tbchaptername <> "" Then
        'Enter chapter details
            
        Selection.GoTo What:=wdGoToBookmark, Name:="Begin"
            With ActiveDocument.Bookmarks
                 .DefaultSorting = wdSortByName
                 .ShowHidden = False
            End With
                            
            Selection.Style = ActiveDocument.Styles("Heading 1")
            Selection.Paragraphs(1).ListAdvanceTo Level1:=Me.tbchapternumber
            Selection.TypeText Text:=Me.tbchaptername
            Selection.TypeParagraph
            'insert bookmark begin
            With ActiveDocument.Bookmarks
            .Add Range:=Selection.Range, Name:="Begin"
            End With
        
        With Selection.Sections.First
          For Each HdFt In .Footers
            If HdFt.Range.Tables.Count > 0 Then
              With HdFt.Range.Tables(1)
                .Cell(1, 2).Range.Text = Me.tbreporttitle
                .Cell(2, 2).Range.Text = Me.tbchapternumber
                .Cell(2, 3).Range.Text = Me.tbchaptername
              End With
            End If
          Next
        End With
      Else
        'message for not entering chapter name
        MsgBox "Please enter a heading and try again."
        GoTo start
      End If
    End If
    End Sub
    Last edited by silentbob; 2012-08-25 at 20:24.

  2. #2
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts
    I would approach this a different way. IMHO, this method is a lot simpler maintain in the template and also to code.

    Firstly, I use Document Property Content Controls to hold the Title, ChapterName and ChapterNumber whereever I want these to appear in the document (eg cover, header, footer etc). Insert these by going Insert > Quick Parts > Document Property and choose from the closest available doc property. You can choose to enter the same content control in multiple locations and updating any location changes the other locations.

    Secondly, I get the userform to just update the document property and this will be instantly reflected in your document.

    With ActiveDocument
    .BuiltInDocumentProperties(wdPropertyTitle) = Me.tbreporttitle
    .BuiltInDocumentProperties(wdPropertySubject) = Me.tbchaptername
    .BuiltInDocumentProperties(wdPropertyCategory) = Me.tbchapternumber
    End With
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  3. #3
    New Lounger
    Join Date
    Jun 2012
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks Andrew, but this code is used to add chapters as well and since it inserts the chapter name into both the footer as well as the heading style i dont know if you way will work in the overall scheme of things

  4. #4
    WS Lounge VIP
    Join Date
    Mar 2006
    Location
    Maryland, USA
    Posts
    690
    Thanks
    17
    Thanked 66 Times in 56 Posts
    You are likely getting the extra pages because the Set numbering value dialog is continue from previous list . In that case, Word inserts the intervening numbers as hidden text--and if the chapter heading paragraph styles are set to page break before, each chapter heading will be on a separate hidden page. That won't happen if start new list is the selection.

    I only know how this works in through the menus/ribbon, not how to do it in VBA.

    I reset numbering often so my chapter-numbered items are or stay correct when I break them into separate documents. Note, though, that if you later put the chapters into one file, the numbering will be wrong until you delete the hidden numbering.

    Pam
    Last edited by PamCaswell; 2012-08-28 at 23:29.

Posting Permissions

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