none
Close Header Section editing RRS feed

  • Question

  •         

     I am trying to remove paragraph marks from the Header/Footer.

    The Header / Footer section remains in Edit mode after following code is executed. How can we close the Header / Footer sections programmatically

    foreach (Word.Paragraph ap in Range.Paragraphs)
                {
                    object missing = System.Reflection.Missing.Value;

                    if (ap.Range.Text == "\r" || ap.Range.Text == " \r" || ap.Range.Text == "  \r" || ap.Range.Text == "\t\r")
                    {
                        ap.Range.Select();
                        ap.Range.Delete(ref missing, ref missing);
                    }
                }

    Tuesday, October 2, 2012 9:09 PM

Answers

  • You can do the processing without switching to Header/Footer view, in which case, there's nothing to close. It is also far more efficient this way. For example (vba):

    Sub Demo1()
    Dim HdFt As HeaderFooter
    With ActiveDocument.Sections(1)
      For Each HdFt In .Footers
        HdFt.Range.Delete
      Next
    End With
    End Sub

    or:

    Sub Demo2()
    Dim HdFt As HeaderFooter, Para As Paragraph
    With ActiveDocument.Sections(1)
      For Each HdFt In .Footers
        For Each Para In HdFt.Range.Paragraphs
          If Para.Range.Text <> "Text to find" Then
            Para.Range.Delete
          End If
        Next
      Next
    End With
    End Sub

    or:

    Sub Demo3()
    Dim Para As Paragraph
    With ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary)
      For Each Para In .Range.Paragraphs
        If Para.Range.Text <> "Text to find" Then
          Para.Range.Delete
        End If
      Next
    End With
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Tuesday, October 2, 2012 10:59 PM
  • Thanks Macropad.

    foreach (Word.Paragraph ap in Range.Paragraphs)
                {
                    object missing = System.Reflection.Missing.Value;

                    if (ap.Range.Text == "\r" || ap.Range.Text == " \r" || ap.Range.Text == "  \r" || ap.Range.Text == "\t\r")
                    {
                        ap.Range.Delete(ref missing, ref missing);
                    }
                }

    the above code works for me. I removed ap.Range.Select(); statement.

    Wednesday, October 3, 2012 12:57 PM

All replies

  • You can do the processing without switching to Header/Footer view, in which case, there's nothing to close. It is also far more efficient this way. For example (vba):

    Sub Demo1()
    Dim HdFt As HeaderFooter
    With ActiveDocument.Sections(1)
      For Each HdFt In .Footers
        HdFt.Range.Delete
      Next
    End With
    End Sub

    or:

    Sub Demo2()
    Dim HdFt As HeaderFooter, Para As Paragraph
    With ActiveDocument.Sections(1)
      For Each HdFt In .Footers
        For Each Para In HdFt.Range.Paragraphs
          If Para.Range.Text <> "Text to find" Then
            Para.Range.Delete
          End If
        Next
      Next
    End With
    End Sub

    or:

    Sub Demo3()
    Dim Para As Paragraph
    With ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary)
      For Each Para In .Range.Paragraphs
        If Para.Range.Text <> "Text to find" Then
          Para.Range.Delete
        End If
      Next
    End With
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Tuesday, October 2, 2012 10:59 PM
  • Thanks Macropad.

    foreach (Word.Paragraph ap in Range.Paragraphs)
                {
                    object missing = System.Reflection.Missing.Value;

                    if (ap.Range.Text == "\r" || ap.Range.Text == " \r" || ap.Range.Text == "  \r" || ap.Range.Text == "\t\r")
                    {
                        ap.Range.Delete(ref missing, ref missing);
                    }
                }

    the above code works for me. I removed ap.Range.Select(); statement.

    Wednesday, October 3, 2012 12:57 PM