• Word VBA Footer problem (Word XP)

    Author
    Topic
    #420131

    [indent]


    ‘—————-Footer Section ——————————————-
    ‘————–remove protection———————–
    If ActiveDocument.ProtectionType wdNoProtection Then
    ActiveDocument.Unprotect
    End If

    If foot_add = True Then

    ‘————————————–Add Footer text————-
    If foot_page = True Then ‘———–show text on first page
    ActiveDocument.PageSetup.DifferentFirstPageHeaderFooter = False
    ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range.Text = txtourref.Text
    Else ‘———— dont show footer text on first page
    ActiveDocument.PageSetup.DifferentFirstPageHeaderFooter = True
    ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range.Text = txtourref.Text
    End If

    ‘————————————-Add Page Numbers————-

    End If

    If foot_num = True Then
    If foot_first = True Then ‘———–show number on first page
    ActiveDocument.Sections(1).Footers(1).PageNumbers.Add PageNumberAlignment:= _
    wdAlignPageNumberRight, FirstPage:=True
    Else ‘—————-don’t show number on first page
    ActiveDocument.Sections(1).Footers(1).PageNumbers.Add PageNumberAlignment:= _
    wdAlignPageNumberRight, FirstPage:=False
    End If
    End If

    ‘———–reprotect————
    ActiveDocument.Protect Type:=wdAllowOnlyFormFields, _
    noreset:=True
    ‘—————————-


    [/indent]
    This code is called from a form which has the following two option boxes on it.

    Add Footer ?
    Add Page Numbers ?

    Selecting either of them, give the next options respectivly

    Show Footer on first page ?
    Show Page Number on First Page ?

    The Values in the above code are as follows

    Add Footer = foot_add
    Add Pagenumbers = foot_num

    Show footer on first page = foot_page
    show page on first page = foot_first

    nb. the idea behind this, is if they select footer then the text of the value txtourref.Text is entered into the footer.

    I know the names are a bit confusing, but unfortunatly not my choice.

    The problem i am having is, its all working well, except when i want to

    Show Footer,
    Show footer on first page
    Show page number
    show page number not on the first page

    anyone see where i am going wrong

    Viewing 0 reply threads
    Author
    Replies
    • #950476

      Which section is it that you actually want to attack. Your code deals with Section(1) and also with the complete document. If there are already page numbers there and the user choses not to add a page number then your code doesn’t appear to remove the existing numbering.

      When you ask to ‘Show the Footer’ are you asking to display the headers and footer on screen OR in a user form OR just do what the code intended?

      I would take a step back and create four different autotext entries. These entries should contain each of the Footer variants:
      1. First page numbered
      2. First page non-numbered
      3. Primary footer numbered
      4. Primary footer non-numbered

      Then your code should replace the existing footers completely by adding the autotext entry to that location. Example code below:

        Dim aRange1F As Range, aRangeF As Range
          Set aRange1F = ActiveDocument.Sections(1).Footers(wdHeaderFooterFirstPage).Range
          Set aRangeF = ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range
      
          ActiveDocument.AttachedTemplate.AutoTextEntries("FooterFirst").Insert Where:=aRange1F, RichText:=True
          aRange1F.Collapse Direction:=wdCollapseEnd
          aRange1F.Delete  'removes the extra return which wasn't part of the autotext
      • #950479

        This code is called when the user creates a new document from one of their templates. There is a custom toolbar which they use to load these templates.

        when they choose a template, a form pops up asking them if they want to include X/Y/Z in the document and also creates a reference. at the bottom of this form is the section I am working on.

        With the option “show footer” This puts the reference generated by the form , into the footer of the document with the option whether to show this on the first page or not.

        • #950484

          [indent]


          ‘—————-Footer Section ——————————————-

          ‘————–remove protection———————–
          If ActiveDocument.ProtectionType wdNoProtection Then
          ActiveDocument.Unprotect
          End If

          If foot_add = True Then

          ‘————————————–Add Footer text————-
          If foot_page = True Then ‘———–show text on first page
          ActiveDocument.PageSetup.DifferentFirstPageHeaderFooter = True
          ActiveDocument.Sections(1).Footers(wdHeaderFooterFirstPage).Range.Text = txtourref.Text
          ActiveDocument.Sections(1).Footers(wdHeaderFooterFirstPage).Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
          ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range.Text = txtourref.Text
          ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
          Else ‘———— dont show footer text on first page
          ActiveDocument.PageSetup.DifferentFirstPageHeaderFooter = True
          ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range.Text = txtourref.Text
          ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
          End If

          ‘————————————-Add Page Numbers————-

          End If

          If foot_num = True Then
          If foot_first = True Then ‘———–show number on first page

          ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).PageNumbers.Add PageNumberAlignment:= _
          wdAlignPageNumberRight, FirstPage:=True
          Else ‘—————-don’t show number on first page
          ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).PageNumbers.Add PageNumberAlignment:= _
          wdAlignPageNumberRight, FirstPage:=False
          End If
          End If

          ‘———–reprotect————
          ActiveDocument.Protect Type:=wdAllowOnlyFormFields, _
          noreset:=True
          ‘—————————-


          [/indent]

          This is where i am so far, this works exactly how i want except for the combination

          Show Footer = true
          Show page numbers = true
          show page numbers on first page = true
          show footer on first page = false

          • #950493

            I assume that under those conditions that you intend to turn off the DifferentFirstPage HeaderFooter so you would need to modify the following code to say False
            Else ‘———— dont show footer text on first page
            ActiveDocument.PageSetup.DifferentFirstPageHeaderFooter = True

            • #950666

              that doesnt have the desired effect.

            • #950673

              I don’t think you will ever get this code to work the way you expect.

              I have just looked at the help file for PageNumbers.Add FirstPage:=False and it seems to describe what you expect – but I don’t believe it.

              You need to make one decision about whether to have a different first page or not – and then stick with it. If you want the first page footer different in any way, including display of page number, then set DifferentFirstPageHeaderFooter = True and create two footers with the correct content. If you want both footers identical then set DifferentFirstPageHeaderFooter = False.

              StuartR

    Viewing 0 reply threads
    Reply To: Word VBA Footer problem (Word XP)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: