locked
Help Needed : How to format header in MS Word 2003 using vba RRS feed

  • Question

  • Hi,

    I am developing an word automation using vba. The aim of this automation is to create a word 2003 document using VBA. The Word document consist of a header in all the pages. I have created a code for creating the header in all the pages but experiencing some issues in it. The code creates header in all the pages from page 1. But the code does not creates the header in the last page of the document. The header in the last page is empty and its not displaying the text.

    The code that i use to create the header is as follows

    Sub GenerateHeader()
      
    Dim oWord As Word.Application  
    Dim oDoc As Word.Document
    Dim ProtocolText As String, ProjCodeText As String
    Dim i As Integer, j As Integer
    
    ProtocolText = txtProtocol.Text
    ProjCodeText = txtProjCode.Text
    
      For i = 1 To oDoc.Sections.Count
        With oDoc.Sections(i)
          .Headers(wdHeaderFooterPrimary).Range.Paragraphs.Alignment = wdAlignParagraphCenter
          .Headers(wdHeaderFooterPrimary).Range.Font.Name = "Arial"
          .Headers(wdHeaderFooterPrimary).Range.Font.Size = 12
          .Headers(wdHeaderFooterPrimary).LinkToPrevious = True
          .Headers(wdHeaderFooterPrimary).Range.Text = "Management Report" + vbCrLf + "Protocol: " + ProtocolText + vbCrLf
          '.Headers(wdHeaderFooterPrimary).Range.Borders(wdBorderDiagonalUp).Visible = True
        End With
      Next i
          
      oDoc.Sections(1).PageSetup.DifferentFirstPageHeaderFooter = False
    
      GenerateFooter
     
    End Sub
    
    

    The Header consist of the text "Management Report, Protocol, Title, Document" which are displayed in each line. I need a line to be displayed above the text "Document" that is displayed in the header. I have attached the screenshot of what i need as the output below.

    Can anyone please help me how to solve my isues i am facing in the header. Any help regarding these two things are highly appreciated.

    Regards,

    Karthik V

     

    Thursday, August 25, 2011 8:52 AM

All replies

  • Maybe the last page of the document is the only page in a Section and that Section is formatted to have a Different First Page Header.  I note that you are setting the DifferentFirstPageHeaderFooter option to False for Section 1 of the document.  Do the other sections have Different First Page Headers/Footers?
    Doug Robbins - Word MVP dkr[atsymbol]mvps[dot]org
    Friday, August 26, 2011 2:23 AM
  • Hi,

    I am able to have the header in all the pages now. can you please help me how to have a line before the text "Document" in the header.

    The Header consist of the text "Management Report, Protocol, Title, Document" which are displayed in each line. I need a line to be displayed above the text "Document" that is displayed in the header. I have attached the screenshot of what i need as the output below.

    Any help regarding these two things are highly appreciated.


    Thanks you and Kind Regards, Karthik Venkatraman
    Friday, August 26, 2011 8:17 AM
  • Hi Karthik,

    Please take a look at this code snippet which I recorded, it can insert the horizontal line in the header of the ActiveDocument:

    Sub Macro3()
    '
    ' Macro3 Macro
    '
    '
        If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
            ActiveWindow.Panes(2).Close
        End If
        If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
            ActivePane.View.Type = wdOutlineView Then
            ActiveWindow.ActivePane.View.Type = wdPrintView
        End If
        ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
        Application.Templates( _
            "C:\Users\v-bpeng\AppData\Roaming\Microsoft\Document Building Blocks\1033\14\Built-In Building Blocks.dotx" _
            ).BuildingBlockEntries("Conservative").Insert Where:=Selection.Range, _
            RichText:=True
        Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
        Selection.TypeText Text:="Managed Report"
        Selection.Delete Unit:=wdCharacter, Count:=1
        Selection.TypeText Text:="Hello Test"
    End Sub

    Hope this can give you some hint. 

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Monday, August 29, 2011 2:34 AM
  • I do this for lots of documents.  What I do is manually create a template with the header the way I want it.  When I create the document I base it on the template.  Pay careful attention to styles.  If you ever need to merge documents inconsistent styles can make it a nightmare.
    Monday, August 29, 2011 2:46 AM
  • Hi,

    the document must not be created using a temple. I cannot use templates for creating the document. I have managed somehow and able to create a line in the all the headers of the document. below is the code of that.

    Sub GenerateHeader()
    
        With oDoc.Sections(1)
          .Headers(wdHeaderFooterPrimary).LinkToPrevious = True
          .Headers(wdHeaderFooterPrimary).Range.Paragraphs.Alignment = wdAlignParagraphCenter
          .Headers(wdHeaderFooterPrimary).Range.Font.Name = "Arial"
          .Headers(wdHeaderFooterPrimary).Range.Font.Size = 12
          .Headers(wdHeaderFooterPrimary).Range.Text = "Quality Management Review Form" + vbCrLf + "Protocol: " + vbCrLf + "ProjCodeText"
        End With
          
      oDoc.Sections(1).Headers(wdHeaderFooterPrimary).Range.Borders(wdBorderBottom).Visible = True
            
      oDoc.Sections(1).PageSetup.DifferentFirstPageHeaderFooter = False
    
    End Sub


    Currently i am able to have the line below the text ProjCodeText. I need to have an empty line below this text so that i can show the line with some space after it.

     


    Thanks you and Kind Regards, Karthik Venkatraman
    Monday, August 29, 2011 9:28 AM
  • I am curious why you cannot create a document based on a template.  You are using normal.dot today.  Also,  if you ever need to merge documents you need setup the styles properly.
    Monday, August 29, 2011 2:50 PM