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




Reply With Quote