Consulting

Results 1 to 6 of 6

Thread: how to put a date on the first page of the document ?

  1. #1

    Unhappy how to put a date on the first page of the document ?

    good afternoon, someone knows how to put a date on the first page of the document using Word VBA macros (the date has to go polygonal position, centered and background)

    I also need to know how to remove that date using VBA macros



    Thanks in advance for your valuable help

  2. #2
    Quote Originally Posted by newcodeboy View Post
    the date has to go polygonal position, centered and background
    What does this mean?
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3

    Post

    I mean this (image attached)

    Capture.JPG

    I want to do it through VBA macros.
    only on the first page and I need to know how to remove it by VBA macros


    Quote Originally Posted by gmayor View Post
    What does this mean?

  4. #4
    The following method re-purposes a built-in building block to add the data in the first page header:

    Sub InsertDateWatermark()
    Dim oRng As Range
    Dim oHeader As HeaderFooter
    Dim oTemplate As Template
        ActiveDocument.PageSetup.DifferentFirstPageHeaderFooter = True
        Set oHeader = ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage)
        Set oRng = oHeader.Range
        On Error Resume Next
        For Each oTemplate In Application.Templates
            If oTemplate.name Like "Built-In Building Blocks*" Then
                oTemplate.BuildingBlockEntries("CONFIDENTIAL 1").Insert Where:=oRng, _
                                                                        RichText:=True
            End If
        Next oTemplate
        With oRng.ShapeRange.Item(1)
            .TextEffect.Text = Format(Date, "Short Date")
            .TextEffect.FontSize = 24
            .Height = 29.25
            .Width = 102#
        End With
    lbl_Exit:
        Exit Sub
    End Sub
    To remove it
    Sub RemoveDateWatermark()
    Dim oRng As Range
    Dim oHeader As HeaderFooter
        On Error Resume Next
        ActiveDocument.PageSetup.DifferentFirstPageHeaderFooter = True
        Set oHeader = ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage)
        Set oRng = oHeader.Range
        oRng.ShapeRange.Item(1).Delete
    lbl_Exit:
        Exit Sub
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  5. #5

    Red face

    brother thanks for the help, I place the code "Format (Now," dd / mm / yyyy ") but not displayed in the document. Annex picture

    I know why this happens


    word.JPG

    Quote Originally Posted by gmayor View Post
    The following method re-purposes a built-in building block to add the data in the first page header:

    Sub InsertDateWatermark()
    Dim oRng As Range
    Dim oHeader As HeaderFooter
    Dim oTemplate As Template
        ActiveDocument.PageSetup.DifferentFirstPageHeaderFooter = True
        Set oHeader = ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage)
        Set oRng = oHeader.Range
        On Error Resume Next
        For Each oTemplate In Application.Templates
            If oTemplate.name Like "Built-In Building Blocks*" Then
                oTemplate.BuildingBlockEntries("CONFIDENTIAL 1").Insert Where:=oRng, _
                                                                        RichText:=True
            End If
        Next oTemplate
        With oRng.ShapeRange.Item(1)
            .TextEffect.Text = Format(Date, "Short Date")
            .TextEffect.FontSize = 24
            .Height = 29.25
            .Width = 102#
        End With
    lbl_Exit:
        Exit Sub
    End Sub
    To remove it
    Sub RemoveDateWatermark()
    Dim oRng As Range
    Dim oHeader As HeaderFooter
        On Error Resume Next
        ActiveDocument.PageSetup.DifferentFirstPageHeaderFooter = True
        Set oHeader = ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage)
        Set oRng = oHeader.Range
        oRng.ShapeRange.Item(1).Delete
    lbl_Exit:
        Exit Sub
    End Sub

  6. #6
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    I would do that a bit differently using either a Date (as shown below) or CreateDate field:

    Sub InsertDateWatermark()
    Dim oHeader As HeaderFooter
    Dim oShp As Shape
    Dim oFld As Field
    Dim oShpRng As ShapeRange
      ActiveDocument.PageSetup.DifferentFirstPageHeaderFooter = True
      Set oHeader = ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage)
      For Each oShp In oHeader.Shapes
        If oShp.Name = "Psuedo Date Watermark" Then
          'Watermark already exists.
          oShp.TextFrame.TextRange.Fields(1).Update
          GoTo lbl_Exit
          Exit For
        End If
      Next oShp
      'Insert an appropriately sized basic shape (rectangle)
      Set oShp = oHeader.Shapes.AddShape(msoShapeRectangle, 1, 1, 350, 100)
      With oShp
        'Name and format the shape appearance.
        .Name = "Psuedo Date Watermark"
        .IncrementRotation -45
        .Line.Visible = msoFalse
        .Fill.Visible = msoFalse
      End With
      'Add a DATE field to the shape text frame.
      Set oFld = oShp.TextFrame.TextRange.Fields.Add(oShp.TextFrame.TextRange, wdFieldEmpty, "DATE \@ ""MMMM d, yyyy""")
      'Format the text range.
      With oShp.TextFrame.TextRange
        .Font.Size = 24
        .Font.ColorIndex = wdGray25
      End With
      'Position the shape independent of any other shape in the header.
      Set oShpRng = oHeader.Shapes.Range("Psuedo Date Watermark")
      With oShpRng
        .Align msoAlignCenters, True 'relative to document edges.
        .Align msoAlignMiddles, True
      End With
    lbl_Exit:
      Exit Sub
    End Sub
    Sub RemoveDateWatermark()
    Dim oRng As Range
    Dim oHeader As HeaderFooter
    Dim oShp As Shape
      Set oHeader = ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage)
      For Each oShp In oHeader.Shapes
        If oShp.Name = "Psuedo Date Watermark" Then
          oShp.Delete
          Exit For
        End If
      Next oShp
    lbl_Exit:
      Exit Sub
    End Sub
    Last edited by gmaxey; 11-05-2015 at 04:05 AM.
    Greg

    Visit my website: http://gregmaxey.com

Posting Permissions

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