none
Add text to top right corner of a document? RRS feed

  • Question

  • I am trying to create a macro that adds the date to the top right corner of a document.  I figured that the best way to achieve this was to write code that adds a header to the document and aligns the text to the right corner, but this is turning out to be more difficult than I expected.

    Does anyone have any VBA code that easily adds a header to a document and then aligns the text to the right?  Should I even use a header for this task?

    Robert Crandal

     


    jmmc
    Tuesday, March 8, 2011 8:16 AM

Answers

  • In order to answer this it will be necessary to know which header (there are
    potentially four header ranges for each section of the document), whether
    there is already content in the header(s) and where in relation to that
    header content you want the date.
     
    e.g. the following will insert the date in all the headers of all sections
    after any existing header text, and aligned to the right.
     
    Sub InsertDateInHeader()
    Dim oSection As Section
    Dim oHeader As HeaderFooter
    Dim oRng As Range
    For Each oSection In ActiveDocument.Sections
        For Each oHeader In oSection.Headers
            Set oRng = oHeader.Range
            With oRng
                If Len(oRng) > 1 Then
                    .InsertAfter vbCr
                End If
                .Start = oHeader.Range.End
                .Text = Format(Date, "dd/mm/yyyy")
                .ParagraphFormat.Alignment = wdAlignParagraphRight
            End With
        Next oHeader
    Next oSection
    End Sub
    --
     
     
     
     

    Graham Mayor - Word MVP
    www.gmayor.com
    Posted via the Communities Bridge
    http://communitybridge.codeplex.com/
    • Marked as answer by Bessie Zhao Friday, April 8, 2011 9:25 AM
    Tuesday, March 8, 2011 10:06 AM
  • The following code will add the date to primary header of a document:

    Dim rng As Range
    Set rng = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range
    rng.ParagraphFormat.Alignment = wdAlignParagraphRight
    rng.Text = Format(Date, "dd MMMM yyyy")

    Probably better however to create a template that has a { CREATEDATE } field in the desired place in the header.  Then when a new document is created from that template, the date on which the document is created will appear when the fields in the document are updated (by printing the document)


    Hope this helps.

    Doug Robbins - Word MVP,
    dkr[atsymbol]mvps[dot]org
    Posted via the Community Bridge

    "Robert Crandal" wrote in message news:6db91d07-f02f-4b09-a9eb-5b393f5fef16@communitybridge.codeplex.com...

    I am trying to create a macro that adds the date to the top right corner of a document.  I figured that the best way to achieve this was to write code that adds a_header_ to the document and aligns the text to the right corner, but this is turning out to be more difficult than I expected.

    Does anyone have any VBA code that easily adds a header to a document and then aligns the text to the right?  Should I even use a header for this task?

    Robert Crandal


    jmmc


    Doug Robbins - Word MVP dkr[atsymbol]mvps[dot]org
    • Marked as answer by Bessie Zhao Friday, April 8, 2011 9:25 AM
    Tuesday, March 8, 2011 10:08 AM

All replies

  • In order to answer this it will be necessary to know which header (there are
    potentially four header ranges for each section of the document), whether
    there is already content in the header(s) and where in relation to that
    header content you want the date.
     
    e.g. the following will insert the date in all the headers of all sections
    after any existing header text, and aligned to the right.
     
    Sub InsertDateInHeader()
    Dim oSection As Section
    Dim oHeader As HeaderFooter
    Dim oRng As Range
    For Each oSection In ActiveDocument.Sections
        For Each oHeader In oSection.Headers
            Set oRng = oHeader.Range
            With oRng
                If Len(oRng) > 1 Then
                    .InsertAfter vbCr
                End If
                .Start = oHeader.Range.End
                .Text = Format(Date, "dd/mm/yyyy")
                .ParagraphFormat.Alignment = wdAlignParagraphRight
            End With
        Next oHeader
    Next oSection
    End Sub
    --
     
     
     
     

    Graham Mayor - Word MVP
    www.gmayor.com
    Posted via the Communities Bridge
    http://communitybridge.codeplex.com/
    • Marked as answer by Bessie Zhao Friday, April 8, 2011 9:25 AM
    Tuesday, March 8, 2011 10:06 AM
  • The following code will add the date to primary header of a document:

    Dim rng As Range
    Set rng = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range
    rng.ParagraphFormat.Alignment = wdAlignParagraphRight
    rng.Text = Format(Date, "dd MMMM yyyy")

    Probably better however to create a template that has a { CREATEDATE } field in the desired place in the header.  Then when a new document is created from that template, the date on which the document is created will appear when the fields in the document are updated (by printing the document)


    Hope this helps.

    Doug Robbins - Word MVP,
    dkr[atsymbol]mvps[dot]org
    Posted via the Community Bridge

    "Robert Crandal" wrote in message news:6db91d07-f02f-4b09-a9eb-5b393f5fef16@communitybridge.codeplex.com...

    I am trying to create a macro that adds the date to the top right corner of a document.  I figured that the best way to achieve this was to write code that adds a_header_ to the document and aligns the text to the right corner, but this is turning out to be more difficult than I expected.

    Does anyone have any VBA code that easily adds a header to a document and then aligns the text to the right?  Should I even use a header for this task?

    Robert Crandal


    jmmc


    Doug Robbins - Word MVP dkr[atsymbol]mvps[dot]org
    • Marked as answer by Bessie Zhao Friday, April 8, 2011 9:25 AM
    Tuesday, March 8, 2011 10:08 AM