Consulting

Results 1 to 9 of 9

Thread: Solved: Deleting headers and footers throughout document

  1. #1

    Solved: Deleting headers and footers throughout document

    Hi, im trying to create a VBA macro for word 2003 that will delete all of the header and footer information throughout a document.

    This is what ive come up with so far:

    [VBA]
    Sub PullHeadFoot()
    ActiveWindow.View.Type = wdPrintView
    ActiveWindow.ActivePane.View.Zoom.PageFit = wdPageFitFullPage
    Selection.HomeKey Unit:=wdStory
    cursec = 1
    curpg = 1
    Do
    ActiveDocument.ActiveWindow.View.SeekView = wdSeekCurrentPageHeader
    Selection.WholeStory
    Selection.Delete
    ActiveDocument.ActiveWindow.View.SeekView = wdSeekCurrentPageFooter
    Selection.WholeStory
    Selection.Delete
    Selection.MoveDown Unit:=wdScreen, Count:=1
    Selection.MoveDown Unit:=wdWindow, Count:=-1
    curpg = curpg + 1
    Loop Until curpg = ActiveDocument.ActiveWindow.Panes(1).Pages.Count + 1

    Do
    ActiveDocument.Sections(cursec).headers(wdHeaderFooterPrimary).Range.Delete
    ActiveDocument.Sections(cursec).Footers(wdHeaderFooterPrimary).Range.Delete
    ActiveDocument.Sections(cursec).headers(wdHeaderFooterEvenPages).Range.Dele te
    ActiveDocument.Sections(cursec).Footers(wdHeaderFooterEvenPages).Range.Dele te
    ActiveDocument.Sections(cursec).headers(wdHeaderFooterFirstPage).Range.Dele te
    ActiveDocument.Sections(cursec).Footers(wdHeaderFooterFirstPage).Range.Dele te
    cursec = cursec + 1
    Loop Until cursec = ActiveDocument.Sections.Last.Index + 1

    End Sub

    [/VBA]



    However, this macro sometimes deletes all of the headers and footers, but also sometimes leaves the headers and footers existing in the document but with 1 blank line. Ive also seen it miss a header/footer, but that was a one-off occassion.

    Can anyone help me? Im sure theres got to be a simpler and more consistent way but i have no idea how to do it.

  2. #2
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Use objects.[vba]Sub PullHeadFoot()
    Dim oHF As HeaderFooter
    Dim oSection As Section
    For Each oSection in ActiveDocument.Sections
    For Each oFF In oSection.Headers
    oFF.Range.Delete
    Next
    For Each oFF In oSection.Footers
    oFF.Range.Delete
    Next
    Next
    End Sub
    [/vba]

    Note: "but also sometimes leaves the headers and footers existing in the document"

    Headers and Footers always exist in documents. Always. Whether you have put anything in them, or taken anything out, they always exist. A brand new blank document has headers and footers.

  3. #3
    That worked brilliantly. It doesnt get stuck inside the header and footer sections when the macro is finished now.

    Also i wasnt aware that headers and footers always existed. I was under the impression they could be removed completely.

    Thanks for your help!

  4. #4
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Hey Bob,
    You really should use Option Explicit at the top of each module, form, etc.

    If you had you would have received an error stating that a variable (oFF) was not defined.

    Gerry made a little mistake in his post by diming oHF as HeaderFooter and then using oFF in the code.

    I would suggest that this is what Gerry was intending to offer:
    [VBA]
    Option Explicit
    Sub DeleteHeadFoot()
    Dim oHF As HeaderFooter
    Dim oSection As Section

    For Each oSection In ActiveDocument.Sections
    For Each oHF In oSection.Headers
    oHF.Range.Delete
    Next
    For Each oHF In oSection.Footers
    oHF.Range.Delete
    Next
    Next
    End Sub
    [/VBA]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  5. #5
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Oooooops! That is what you get for typing code in directly.

    Steve is quite correct. Both in noting my error, and suggesting you use Option Explicit.

  6. #6
    Yeah i caught that and fixed it I knew what he was trying to do. It worked really well and is less lines of code than my version, and it runs faster as well.

    I do use option explicit though in my own code. I can never get over not having to define my variables up front, it makes reading other peoples code so difficult, especially since im relatively new to VBA. It takes while if someones using a parameter ive never heard of or just calling out a variable in the middle of the code

  7. #7
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    "it runs faster as well"

    Yes indeed. That is because there is no use of Selection. In particular doing any actual changing to visually see into the header uses GUI resources, and takes time.

    Serious VBA coding dealing with headers and footer should NEVER use ActiveWindow.View. From a coding point of view (pun intended) there absolutely no need. The only reason people have View when doing astuff with headers is that they have used the macro recorder.

    Macro recording uses Selection. Only Selection.

  8. #8
    Hi good morning, i am new user and I'm starting in the world of VBA programming, I am currently trying to insert a watermark with the current date, my question is: how do I delete the watermark and date without erasing all the header? because when I use the Luke code headers of the document is deleted.




    Thanks in advance for your help and discuple the google translator xD


    Attachment 14631


    this is the date code that I want to delete

    Sub Fecha()
    ActiveDocument.Sections(1).Range.Select
     ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
     
     Selection.HeaderFooter.Shapes.AddTextEffect( _
     PowerPlusWaterMarkObject244368608, "              " & _
     Format(Now(), "dd/mm/yyyy"), "Arial", _
     1, False, False, 0, 0).Select
     Selection.ShapeRange.TextEffect.NormalizedHeight = False
     Selection.ShapeRange.Line.Visible = False
     Selection.ShapeRange.Fill.Visible = True
     Selection.ShapeRange.Fill.Solid
     Selection.ShapeRange.Fill.ForeColor.RGB = RGB(192, 192, 192)
    
    
      
     Selection.ShapeRange.Fill.Transparency = 0.4
     Selection.ShapeRange.Rotation = 315
     Selection.ShapeRange.LockAspectRatio = True
     Selection.ShapeRange.Height = CentimetersToPoints(6.1)
     Selection.ShapeRange.Width = CentimetersToPoints(4.34)
    
    
     Selection.ShapeRange.WrapFormat.AllowOverlap = True
     Selection.ShapeRange.WrapFormat.Side = wdWrapNone
    Selection.ShapeRange.WrapFormat.Type = 6
     Selection.ShapeRange.RelativeHorizontalPosition = _
    wdRelativeVerticalPositionMargin
     Selection.ShapeRange.RelativeVerticalPosition = _
     wdRelativeVerticalPositionMargin
    Selection.ShapeRange.Left = wdShapeCenter
     Selection.ShapeRange.Top = wdShapeCenter
    
    
    Selection.ShapeRange.IncrementTop 55
     ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
    End Sub
    In short I want to delete the date and watermark without deleting the logo that is in the header, there is a parameter?

  9. #9

Posting Permissions

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