Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Editing Header in Section 2 Affects Section 1

Status
Not open for further replies.

duGly

Technical User
Nov 13, 2006
52
US
I am using VBA in Word 2003 to programmatically edit the headers of my document. My header paragraph style includes a bottom border.

Page one (section 1) does not use a header. However, when I programmatically edit the header of section 2, the bottom border shows up in the header of section 1 (even though there is no text). The following attempts have not fixed the problem:
Code:
With ThisDocument
    .Sections(1).Headers(wdHeaderFooterPrimary).Range.Text = ""
    .Sections(1).Headers(wdHeaderFooterEvenPages).Range.Text = ""
    .Sections(1).Headers(wdHeaderFooterFirstPage).Range.Text = ""
    .Sections(1).Headers(wdHeaderFooterPrimary).Range.Delete
    .Sections(1).Headers(wdHeaderFooterEvenPages).Range.Delete
    .Sections(1).Headers(wdHeaderFooterFirstPage).Range.Delete
End With    ' ThisDocument
The only workaround I have found is to view the headers/footers (change the SeekView property), and then return to the print view. The problem is that, even with ScreenUpdating disable, the screen briefly flashes the headers/footers editing window. Here is my code:
Code:
Public Sub EditHeaders(strNameProject$, strNameChapter$, intChapter%)
'
' Object variables:
'   sct = Used to iterate through sections.
    Dim sct As Section
    
    Application.ScreenUpdating = False
    For Each sct In ThisDocument.Sections
        If Not sct.Index = 1 Then _
            sct.Headers(wdHeaderFooterPrimary).Range.Text = strNameProject & Chr(12) & _
            intChapter & ".0 " & strNameChapter
    Next    ' sct
    
    If ActiveWindow.View.SplitSpecial <> wdPaneNone Then ActiveWindow.Panes(2).Close
    ActiveWindow.ActivePane.View.Type = wdPrintView
    ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
    ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
    Application.ScreenUpdating = True
    
End Sub
Can anyone suggest a way to fix this problem without having to alter the Seek View? I would really appreciate your help.
 



Hi,

Check out the Same as previous toggle in the Header.

Skip,

[glasses] [red][/red]
[tongue]
 
1. Make sure that Section 2 is set Same as previous = False. Then if you have to you can remove any text from Section 1.

2. You do not need to use View at all. You can declare a HeaderFooter object and action it directly. This also solves any screen flashing/updating.
Code:
Public Sub EditHeaders(strNameProject$, strNameChapter$, intChapter%)
Dim sct As Section
Dim oHF As Word.HeaderFooter
Dim var, var2
For Each sct In ThisDocument.Sections
[COLOR=red]' if Section 2[/color red]
  If sct.Index = 2 Then
[COLOR=red]  ' loop through all three headers[/color red]
    For var = 1 To 3
[COLOR=red]    ' setting header object for each[/color red]
    Set oHF = sct.Headers(var)
[COLOR=red]    ' explicitly making them NOT Same as previous[/color red]
    oHF.LinkToPrevious = False
[COLOR=red]    ' if header object is 1 (Primary)
    ' put in your content[/color red]
       If var = 1 Then
          oHF.Range.Text = strNameProject & Chr(12) & _
             intChapter & ".0 " & strNameChapter
[COLOR=red]    ' otherwise explicitly make other headers
    ' for that Section blank[/color red]
       Else
         oHF.Range.Text = ""
       End If
    Next
  Else
[COLOR=red]  ' for all other Sections, and all other headers
  ' make blank[/color red]
    For var2 = 1 To 3
      Set oHF = sct.Headers(var2)
      With oHF
        .LinkToPrevious = False
        .Range.Text = ""
      End With
    Next
  End If
Next
End Sub
You do not clearly state what the situation is with your other headers, or other possible Sections.

What the code above does is:

Explicitly makes Section 2 Header (Primary) the contents you want;

Explicitly makes ALL Section 2 headers NOT Same as previous;

Explicitly makes the other two headers in Section 2 blank;

Explicitly makes all other Sections, and all other headers for those Sections, blank.

The code does not use View.

Hope this helps.

Gerry
My paintings and sculpture
 
Thanks, Skip and Gerry, for your suggestions. I should have noted in my post that the LinkToPrevious was already set to FALSE. Nevertheless, I tried used your suggestion to explicitly set it as such. Here is my code, modified from Gerry's example:
Code:
Public Sub EditHeaders(strNameProject$, strNameChapter$, intChapter%)[green]
'
' Variables:
'   intHeader = Used to iterate through headers of each respective section.[/green]
    Dim intHeader%[green]
'
' Object variables:
'   sct = Used to iterate through sections.[/green]
    Dim sct As Section
    
    Application.ScreenUpdating = False
    For Each sct In ThisDocument.Sections
        Select Case sct.Index[green]
        
            ' Section 1's headers:[/green]
            Case 1[green]
                
                ' Clear all headers.[/green]
                For intHeader = 1 To 3
                    sct.Headers(intHeader).Range.Text = ""
                Next[green]    ' intHeader
        
            ' Section 2's headers:[/green]
            Case 2[green]
                
                ' Unlink each header to previous section. Set text for header 1. Clear headers 2 and 3.[/green]
                For intHeader = 1 To 3
                    With sct.Headers(intHeader)
                        .LinkToPrevious = False
                        If intHeader = 1 Then _
                            .Range.Text = strNameProject & Chr(12) & intChapter & ".0 " & strNameChapter Else _
                            .Range.Text = ""
                    End With    ' Header
                Next[green]    ' intHeader
        
            ' All other sections' headers:[/green]
            Case Else[green]
                
                ' Link each header to previous section.[/green]
                For intHeader = 1 To 3
                    sct.Headers(intHeader).LinkToPrevious = True
                Next[green]    ' intHeader[/green]
        End Select[green]  ' sct.Index[/green]
    Next[green]    ' sct[/green]
    Application.ScreenUpdating = True
    
End Sub
Unfortunately, the problem persists. Even though Section 1's headers are blank and Section 2's headers are NOT LinkedToPrevious, editing Section 2's headers causes Section 1's header to display with a bottom border (an attribute of my Header paragraph style).

I also tried this variation for section 1's headers:
Code:
            [green]' Section 1's headers:[/green]
            Case 1[green]
                
                ' Clear all headers.[/green]
                For intHeader = 1 To 3
                    sct.Headers(intHeader).Range.Style = "Normal"
                    sct.Headers(intHeader).Range.Text = ""
                Next[green]    ' intHeader[/green]
Although this prevented the bottom border from showing up (since the bottom border is NOT an attribute of the Normal style), running the code still causes Section 1's header to "appear" (ie. the first line of text on the page is lowered by about 1" to make room for the non-existent header).

And again, the only way I have found to fix it is to temporarily switch views, either manually or programmatically. I can switch to any of the following views to clear the problem: Normal, Web Layout, Reading Layout, Outline, Header and Footer, or Print Preview.

Any other suggestions? The screen flicker caused by switching views is unacceptable to me.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top