Consulting

Results 1 to 3 of 3

Thread: Writing to Header and Footer while Changing Text Color

  1. #1
    VBAX Newbie
    Joined
    Jan 2017
    Posts
    1
    Location

    Post Writing to Header and Footer while Changing Text Color

    Hi,
    I just started coding in VBA on Monday and have run into some challenges.

    I am trying to develop a Form in MS Word and one of the Form Fields is called "subject" and it has a drop down menu with a list of subjects

    1. If any subject is selected from the drop down menu, I want to be able to write the selection to the header and footer
    2. The font color of the item written to the header and footer would vary depending on the item selected.
    3. The item written to the header and footer would be preceeded by the word "Subject:" in standard black font

    This is the code snipet I have put together so far...any help and insight would be greatly appreciated.

    'This subroutine sets the classification in the header
    Sub SetHeadersFooters()
    Select Case ActiveDocument.FormFields("ddsubject").Result
    Case "MATH"
    ActiveDocument.Unprotect Password:="mathematics"
    For i = 1 To ActiveDocument.Sections.Count
    With ActiveDocument.Sections(i)
    .Headers(wdHeaderFooterPrimary).Range.Text = "SUBJECT: MATHEMATICS"
    .Footers(wdHeaderFooterPrimary).Range.Text = "SUBJECT: MATHEMATICS"
    Selection.Font.ColorIndex = wdRed
    End With
    Next
    Case "ENGLISH"
    ActiveDocument.Unprotect Password:="topics"
    For i = 1 To ActiveDocument.Sections.Count
    With ActiveDocument.Sections(i)
    .Headers(wdHeaderFooterPrimary).Range.Text = "SUBJECT: ENGLISH"
    .Footers(wdHeaderFooterPrimary).Range.Text = "SUBJECT: ENGLISH"
    Selection.Font.ColorIndex = wdRed = wdBlue
    End Select
    End Sub

  2. #2
    You cannot use 'Selection' in this way to format the header footer as the header footer is not selected. You need to work with the range as you already began to do.
    Using separate passwords depending on the content of the dropdown is fraught with problems. It relies on the user selecting only the dropdown entry that relates to the password, so in that case why have a dropdown at all - if you can only make one usable selection?. The following code has a common password (and reprotects the document with that password after processing the entry).
    If you have the values in the dropdown that you want in the header and footer, there is no real need for the macro. You can use REF or STYLEREF fields instead and avoid the need for macros in a form which you are presumably going to distribute. However given that you are learning VBA, the following should work for you

    Sub SetHeadersFooters()
    Dim oRng As Range
    Dim sPassword As String
    Dim strHeading As String
    Dim i As Long
        sPassword = "topics" 'Use a common password
        If Not ActiveDocument.ProtectionType = wdNoProtection Then
            ActiveDocument.Unprotect Password:=sPassword
        End If
        Select Case ActiveDocument.FormFields("ddsubject").Result
            Case "MATH": strHeading = "MATHEMATICS"
            Case "ENGLISH": strHeading = "ENGLISH"
        End Select
    
        For i = 1 To ActiveDocument.Sections.Count
            With ActiveDocument.Sections(i)
                'process the headers
                Set oRng = .Headers(wdHeaderFooterPrimary).Range
                oRng.Text = "SUBJECT: "    'enter the common range text
                oRng.Collapse 0    'collapse the range to its end
                oRng.Text = strHeading    'add the variable text
                oRng.Font.ColorIndex = wdRed    'colour the variable text
                'Repeat for the footers
                Set oRng = .Footers(wdHeaderFooterPrimary).Range
                oRng.Text = "SUBJECT: "
                oRng.Collapse 0
                oRng.Text = strHeading
                oRng.Font.ColorIndex = wdRed
            End With
        Next
        ActiveDocument.Protect wdAllowOnlyFormFields, Password:=sPassword
    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

  3. #3
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,337
    Location
    What version of Word? Have you considered using content controls instead of Formfields?

    Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
    Dim oCC As ContentControl
    Dim oRng As Word.Range
    Dim strPW As String
      strPW = "topics"
      Select Case ContentControl.Title
        Case "Subject" 'The "Subject" Dropdown CC in document.
          If Not ActiveDocument.ProtectionType = wdNoProtection Then
            ActiveDocument.Unprotect Password:=strPW
          End If
          Set oCC = ActiveDocument.SelectContentControlsByTitle("HF Subject").Item(1) 'Richtext CC in Header
          Select Case True
            Case ContentControl.ShowingPlaceholderText:
              oCC.Range.Text = vbNullString
            Case ContentControl.Range.Text = "MATH"
              oCC.Range.Text = "Subject: MATH"
              Set oRng = oCC.Range
              oRng.Start = oRng.Start + 9
              oRng.Font.ColorIndex = wdRed
            Case ContentControl.Range.Text = "ENGLISH"
              oCC.Range.Text = "Subject: ENGLISH"
              Set oRng = oCC.Range
              oRng.Start = oRng.Start + 9
              oRng.Font.ColorIndex = wdBlue
            Case ContentControl.Range.Text = "SOCIAL STUDIES"
              oCC.Range.Text = "Subject: SOCIAL STUDIES"
              Set oRng = oCC.Range
              oRng.Start = oRng.Start + 9
              oRng.Font.ColorIndex = wdGreen
          End Select
          'Richtext CC in Footer
          ActiveDocument.SelectContentControlsByTitle("HF Subject").Item(2).Range.FormattedText = oCC.Range.FormattedText
          ActiveDocument.Protect wdAllowOnlyReading, , strPW
      End Select
    lbl_Exit:
      Exit Sub
    End Sub
    Attached Files Attached Files
    Greg

    Visit my website: http://gregmaxey.com

Posting Permissions

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