Consulting

Results 1 to 7 of 7

Thread: Word 2016: Define pagenumber start in userform

  1. #1

    Word 2016: Define pagenumber start in userform

    I am working on a number of documents, where the user enters some data in a userform, which is entered into the document via bookmarks.

    Now I need to make a document, where the pagenumber begins with the number entered in the userform.

    The master document has a footer with a page number and another bookmark with the clients name, so all I need is to feed the existing pagenumber with the start value.

    My userform looks like this:

    Attachment 21201

    The start value for the pagenumber is "Start med side" in TextBox 11.

    My code for the macro looks like below. I have no problem entering a value in TextBox11 and placing the value in a bookmark where the pagenumber is located, but if the value is 56, then all pages are numbered 56.

    Private Sub NęsteButton1_Click()
    Dim Kundenavn As Range
        Set Kundenavn = ActiveDocument.Bookmarks("selskabsnavn1").Range
        Kundenavn.Text = Me.TextBox1.Value
        Set Kundenavn = ActiveDocument.Bookmarks("selskabsnavn2").Range
        Kundenavn.Text = Me.TextBox1.Value
        Set Kundenavn = ActiveDocument.Bookmarks("selskabsnavn3").Range
        Kundenavn.Text = Me.TextBox1.Value
        Dim Adresse As Range
        Set Adresse = ActiveDocument.Bookmarks("adresse").Range
        Adresse.Text = Me.TextBox2.Value
        Set Postnr = ActiveDocument.Bookmarks("postnr").Range
        Postnr.Text = Me.TextBox3.Value
        Dim by As Range
        Set by = ActiveDocument.Bookmarks("by").Range
        by.Text = Me.TextBox4.Value
        Dim CVRnummer As Range
        Set CVRnummer = ActiveDocument.Bookmarks("cvr").Range
        CVRnummer.Text = Me.TextBox5.Value
        Dim senesteaccept As Range
        Set senesteaccept = ActiveDocument.Bookmarks("senesteaccept").Range
        senesteaccept.Text = Me.TextBox6.Value
        Dim underskriftdato As Range
        Set underskriftdato = ActiveDocument.Bookmarks("dato").Range
        underskriftdato.Text = Me.TextBox7.Value
        Dim bestyrelsesmedlem1 As Range
        Set bestyrelsesmedlem1 = ActiveDocument.Bookmarks("bestyrelse1").Range
        bestyrelsesmedlem1.Text = Me.TextBox8.Value
        Dim bestyrelsesmedlem2 As Range
        Set bestyrelsesmedlem2 = ActiveDocument.Bookmarks("bestyrelse2").Range
        bestyrelsesmedlem2.Text = Me.TextBox9.Value
        Dim bestyrelsesmedlem3 As Range
        Set bestyrelsesmedlem3 = ActiveDocument.Bookmarks("bestyrelse3").Range
        bestyrelsesmedlem3.Text = Me.TextBox10.Value
    
    Need code here
    
    
    
    
    
    Me.Repaint
    UserForm1.Hide
    End Sub
    
    


    Thanks for any assistance.

    Kristoffer

  2. #2
    Sorry for the odd looking formatting (first post) ;-)

  3. #3
    You can simplify your code (and make changes to the document by running the userform again) if you adopt the following function.
    As for the page numbering, you need to insert a page field in the footer and then update the start number for the current section from your text box as included below, then update the field in the footer.

    Option Explicit
    
    Private Sub NęsteButton1_Click()
    Dim oFooter As HeaderFooter
    Dim iPage As Integer
        FillBM "selskabsnavn1", TextBox1.Value
        FillBM "selskabsnavn2", TextBox1.Value
        FillBM "selskabsnavn3", TextBox1.Value
        FillBM "adresse", TextBox2.Value
        FillBM "postnr", TextBox3.Value
        FillBM "by", TextBox4.Value
        FillBM "cvr", TextBox5.Value
        FillBM "senesteaccept", TextBox6.Value
        FillBM "dato", TextBox7.Value
        FillBM "bestyrelse1", TextBox8.Value
        FillBM "bestyrelse2", TextBox9.Value
        FillBM "bestyrelse3", TextBox10.Value
        iPage = TextBox11.Value
        Set oFooter = Selection.Sections(1).Footers(wdHeaderFooterPrimary)
        With oFooter.PageNumbers
            .NumberStyle = wdPageNumberStyleArabic
            .RestartNumberingAtSection = True
            .StartingNumber = iPage
        End With
        oFooter.Range.Fields.Update
        Hide
    End Sub
    
    Private Sub FillBM(strbmName As String, strValue As String)
    'Graham Mayor - http://www.gmayor.com
    Dim orng As Range
        With ActiveDocument
            On Error GoTo lbl_Exit
            Set orng = .Bookmarks(strbmName).Range
            orng.Text = strValue
            orng.Bookmarks.Add strbmName
        End With
    lbl_Exit:
        Set orng = Nothing
        Exit Sub
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  4. #4
    Great! Thanks a lot - also for the more simple code.

  5. #5
    I have a follow up question:

    Is it possible to make a line of code in the end, which updates the cross-references in the document?

    I have made two crossreferences to two specific pages which returns the page number of the bookmark.

    When the macro has set the starting pagenumber, then the cross-references need to be updated with the correct page numbers.

  6. #6
    Immediately after the line
    oFooter.Range.Fields.Update
    put
    UpdateAllFields
    and add the following macro to the module, and all the fields in the doocument will be updated

    Private Sub UpdateAllFields()
    Dim oStory As Range
        For Each oStory In ActiveDocument.StoryRanges
            oStory.Fields.Update
            If oStory.StoryType <> wdMainTextStory Then
                While Not (oStory.NextStoryRange Is Nothing)
                    Set oStory = oStory.NextStoryRange
                    oStory.Fields.Update
                Wend
            End If
        Next oStory
        Set oStory = Nothing
    lbl_Exit:
        Exit Sub
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  7. #7
    Thanks a lot Graham

Posting Permissions

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