locked
Removing Headers/Footers (and their paragraph marks!) RRS feed

  • السؤال

  • I'm about at wits end on this thing.  I've got a macro written in VBA for removing headers/footers from a word document:

     

    Public Function ClearHeaderFooters()
      Dim osection As Section
      For Each osection In ActiveDocument.Sections
        For Each ohead In osection.Headers
          If ohead.Exists Then ohead.Range.Delete
        Next
        For Each ofoot In osection.Footers
          If ofoot.Exists Then ofoot.Range.Delete
        Next
      Next osection
    End Function
    

     

    This does seem to work; it removes all the headers and footers.  Except of course for one character :

     

    ? Len(ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Text) <br/>
    1<br/>
    <br/>
    ? asc(ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Text)<br/>
    13<br/>
    
    

     

    If I open up the header/footer view by double clicking the paragraph mark or through View -> Header/Footer -- and go back to the body of the page (via the exit button or by double clicking on the body) the paragraph mark, and the space allocated for it, seems to disapear. 

    But after I run the ClearHeaderFooters sub again the paragraph marker shows up again....  Is there a way to remove this character using macro code?

    15/شوال/1431 02:04 ص

الإجابات

  • Hi ts

    No, I don't think so, although different versions of Word behave differently in this respect. As I recall, older versions didn't remove the paragraph mark from view at all, once a header/footer had been initialized. Some newer versions can do that (as you describe), in the UI.

    Best I can suggest would be to use SeekView and mimic the user action of going into the header/footer, then go back out...

    The other possibility would be inserting the "dirty" file into a new document.


    Cindy Meister, VSTO/Word MVP
    • تم الاقتراح كإجابة بواسطة Amy Li 20/شوال/1431 03:07 ص
    • تم وضع علامة كإجابة بواسطة Amy Li 21/شوال/1431 02:07 ص
    15/شوال/1431 04:54 م

جميع الردود

  • Hi ts

    No, I don't think so, although different versions of Word behave differently in this respect. As I recall, older versions didn't remove the paragraph mark from view at all, once a header/footer had been initialized. Some newer versions can do that (as you describe), in the UI.

    Best I can suggest would be to use SeekView and mimic the user action of going into the header/footer, then go back out...

    The other possibility would be inserting the "dirty" file into a new document.


    Cindy Meister, VSTO/Word MVP
    • تم الاقتراح كإجابة بواسطة Amy Li 20/شوال/1431 03:07 ص
    • تم وضع علامة كإجابة بواسطة Amy Li 21/شوال/1431 02:07 ص
    15/شوال/1431 04:54 م
  • Hi tswaters,

    I'm writing to check the status of the thread. Could you please let me know if you have solved the problem?

    Best Regards,
    Amy Li
    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    20/شوال/1431 03:07 ص
  • Hi Amy and tswaters

    Have you solve the problem? 

    I need the similar.

    I need to remove the header & footer and their paragraph marks else my paragraph.range call using paragraph.count got misplaced. 

    don

    23/شعبان/1433 07:38 م
  • On 2012-07-13 21:38, willhokwatchdog wrote:

    Hi Amy and tswaters

    Have you solve the problem? 

    I need the similar.

    I need to remove the header & footer and their paragraph marks else my paragraph.range call using paragraph.count got misplaced. 




    You can certainly remove the contents of headers and footers, for example using the following macro:

    Sub ClearHeaderFooterContent()
    Dim s As Section
    For Each s In ActiveDocument.Sections
    s.Headers(wdHeaderFooterEvenPages).Range.Text = ""
    s.Headers(wdHeaderFooterFirstPage).Range.Text = ""
    s.Headers(wdHeaderFooterPrimary).Range.Text = ""

    s.Footers(wdHeaderFooterEvenPages).Range.Text = ""
    s.Footers(wdHeaderFooterFirstPage).Range.Text = ""
    s.Footers(wdHeaderFooterPrimary).Range.Text = ""
    Next s
    End Sub

    However, it is still a fact that some Word versions leave a paragraph mark (¶) behind after you have run the macro. (The same thing happens when you delete the content manually, in the user interface.)

    Stefan Blom, Microsoft Word MVP
    24/شعبان/1433 02:39 م
  • Dear Stern,

    thanks for your answering and apologize a belated reply here cos work busy these days.

    I shall try on that and come back with feedback.

    Yes, I aware the para-mark may still appear after header/footer content removal. In my ideal world, suppose these can be removed, I shall have no worry on using range.paragraphs.count then paragraphs.range.text to extract the "target" paragraphs. Due to unknown reason, sometimes the result would extracted paragraphs upper in the page, especially when working with documents which are more lengthy/pages.

    kind regards,

    willhok  

    26/شعبان/1433 06:20 م
  • On 2012-07-16 20:20, willhokwatchdog wrote:

    Dear Stern,

    thanks for your answering and apologize a belated reply here cos work busy these days.

    I shall try on that and come back with feedback.

    Yes, I aware the para-mark may still appear after header/footer content removal. In my ideal world, suppose these can be removed, I shall have no worry on using range.paragraphs.count then paragraphs.range.text to extract the "target" paragraphs. Due to unknown reason, sometimes the result would extracted paragraphs upper in the page, especially when working with documents which are more lengthy/pages.

    kind regards,

    willhok 





    Do post back with your findings. In the meantime, may I ask why it is a problem that the paragraph mark remains?

    Stefan Blom, Microsoft Word MVP
    26/شعبان/1433 07:42 م
  • Hi there, sorry it's been so long. 

    Once again ran into this problem, searched google and it came up with my post!

    When I wrote the above, it was with Word 2007 -- now I note that in Word 2010 the paragraph marker cannot be removed. I tried the code that Stefan provides, but the paragraph marker remains, as he noted it would in certain versions. After playing with this a bit I'm getting some inconsistent results.  I'm not sure how I did it, but I managed to, at some point, remove the header... but after trying it again, now can't remove it.

    to answer Stefan's question :

    My underlying problem here is that this paragraph marker takes up space in the header -- with it gone, there is more room to work with. This can be important when you need to place elements in certain locations without relying on textboxes to position them absolutly. 

    The problem that brought this post was in using the "Insert Envelope" function, replacing the return address with a custom building block -- this BB takes up quite a bit of space and needs to be very close to the top of the envelope -- with the paragraph marker taking up space, it isn't postitioned properly. 

    As a workaround, I reduce the font size to 1 -- updated code :

    Public Function ClearHeaderFooters()
      Dim osection As Section
      For Each osection In ActiveDocument.Sections
        For Each ohead In osection.Headers
          If ohead.Exists Then
            ohead.Range.Delete
            If ohead.Range.text = Chr(13) Then
              ohead.Range.Font.Size="1"
            End If
        Next
        For Each ofoot In osection.Footers
          If ofoot.Exists Then
            ofoot.Range.Delete
            If ofoot.Range.text = Chr(13) Then
              ofoot.Range.Font.Size="1"
            End If
          End If
        Next
      Next osection
    End Function

    ...and that seems to do the trick. 

    Other weirdness :

    If you set the '.Range.Text=chr(13)' it will add another paragraph marker, totalling two.

    If you set '.Range.Text=""' it will clear out everything, but the first paragraph marker stays

    If you call ".Range.Delete" it will remove any font size changes & the paragraph marker returns at full size

    Highlighting the text, and calling "Selection.Range.Delete" has no effect (calling Selection.Range.Font.Size="1" will reduce its size.)


    • تم التحرير بواسطة tswaters 03/ذو القعدة/1433 11:24 م I accidently a few words
    03/ذو القعدة/1433 11:21 م
  • Maybe I’m missing something, but it seems to me you could just set the font size to 1 point and the spacing before/after to zero for the Range object in the header.

    Stefan Blom, Microsoft Word MVP
    04/ذو القعدة/1433 06:50 م
  • Hi.

    I was reading this article having the same problem, but finally I found a solution that I want to share.

    First you should remove headers and footers not with any macro or VBA, but going to: File - Info - Inspect document, and leave a check mark only at "Headers footers, and watermarks", then inspect and "Remove all", as I read somewhere, But then:

    Second go to: Layout - Margins - Custom margins - Layout - choose Apply to Whole document - Uncheck two boxes under Headers and footers - and add two zeros on header and footer - Ok.

    This should do the trick.


    Paul Vouros

    13/محرم/1442 10:38 ص