简体   繁体   中英

How to insert PRINTDATE field code in a sentence using Word VBA?

I'd like to get the following sentence inside my doc: "This document was last printed on 01/01/2019 2:10 AM". The date and time must be dynamic. Right now I only have this part:

Element.Range.Text = "This document was last printed on "

It works, but it's missing the date part. What should I do to get the PRINTDATE field concatenated into this sentence?

I tried

Selection.Fields.Add Range:=.Footers(wdHeaderFooterPrimary).Range, Type:=wdFieldPrintDate, Text:="\@""DD MMM YYYY""", preserveformatting:=True 

and it works, but it's overwriting all the footer, how can I append it to the text to make it similar to my example?

To access the Footer associated with a Selection:

Dim rng as Word.Range
Set rng = Selection.Sections(1).Footers(wdHeaderFooterPrimary).Range

To then append to that Range:

rng.InsertAfter " This document was last printed on " 'don't forget a space at the beginning
rng.Collapse wdCollapseEnd 'so the inserted field comes AFTER
rng.Fields.Add Range:=rng,  Type:=wdFieldPrintDate, _
    Text:="\@""DD MMM YYYY""", preserveformatting:=False

Note: I highly recommend using PreserveFormatting:=False as the field is more likely to pick up the formatting of the surrounding text if other formatting is applied. Setting this to True will retain the originally applied formatting for the number of characters originally in the field . If the field is updated and the number of characters changes, some of the characters may be formatted differently from the rest.

Indeed, I prefer using the method more as follows, with all the field content in the Text parameter, including the CharFormat switch. CharFormat will force the entire field to use the character formatting applied to the first character in the field code - much more reliable:

rng.Fields.Add Range:=rng,  Type:=wdFieldEmpty, _
    Text:="PrintDate \@""DD MMM YYYY"" \* CharFormat", preserveformatting:=False

The code below will change all footers in a document to show "The document was last printed on {dd.mm.yyyy}. It might be modified not to replace all of the footer.

Sub ModifyFooter()
    ' 03 Jan 2019

    Dim Doc As Document
    Dim Txt As String
    Dim Foot As HeaderFooter
    Dim Para As Paragraph
    Dim Rng As Range
    Dim i As WdHeaderFooterIndex

    Set Doc = ActiveDocument
    For i = wdHeaderFooterPrimary To wdHeaderFooterFirstPage
        Set Foot = Doc.Sections(1).Footers(i)
        Txt = "This document was last printed on "
        Set Para = Foot.Range.Paragraphs(1)
        Set Rng = Para.Range
        With Rng
            .Text = Txt
            .Collapse wdCollapseEnd
        End With
        Txt = "\@ ""dd.MM.yyyy"""
        Doc.Fields.Add Rng, wdFieldPrintDate, Text:=Txt, PreserveFormatting:=True
    Next i
End Sub

Try:

With ActiveDocument
  .Fields.Add Range:=Selection.Sections.First.Footers(wdHeaderFooterPrimary).Range.Characters.Last, _
    Type:=wdFieldEmpty, Text:="PRINTDATE \@""'This document was last printed on 'DD MMM YYYY""", PreserveFormatting:=False
End With

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM