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





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















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?










share|improve this question

























  • I think you're looking for Fields.Add and wdFieldPrintDate from the WdFieldType enumeration.

    – BigBen
    Jan 3 at 4:34











  • Hum... 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?

    – JChris
    Jan 3 at 4:47




















0















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?










share|improve this question

























  • I think you're looking for Fields.Add and wdFieldPrintDate from the WdFieldType enumeration.

    – BigBen
    Jan 3 at 4:34











  • Hum... 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?

    – JChris
    Jan 3 at 4:47
















0












0








0








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?










share|improve this question
















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?







vba ms-word






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 11:47









Cindy Meister

16k102437




16k102437










asked Jan 3 at 4:24









JChrisJChris

69641327




69641327













  • I think you're looking for Fields.Add and wdFieldPrintDate from the WdFieldType enumeration.

    – BigBen
    Jan 3 at 4:34











  • Hum... 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?

    – JChris
    Jan 3 at 4:47





















  • I think you're looking for Fields.Add and wdFieldPrintDate from the WdFieldType enumeration.

    – BigBen
    Jan 3 at 4:34











  • Hum... 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?

    – JChris
    Jan 3 at 4:47



















I think you're looking for Fields.Add and wdFieldPrintDate from the WdFieldType enumeration.

– BigBen
Jan 3 at 4:34





I think you're looking for Fields.Add and wdFieldPrintDate from the WdFieldType enumeration.

– BigBen
Jan 3 at 4:34













Hum... 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?

– JChris
Jan 3 at 4:47







Hum... 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?

– JChris
Jan 3 at 4:47














3 Answers
3






active

oldest

votes


















1














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





share|improve this answer































    0














    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





    share|improve this answer































      0














      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





      share|improve this answer
























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54016322%2fhow-to-insert-printdate-field-code-in-a-sentence-using-word-vba%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        1














        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





        share|improve this answer




























          1














          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





          share|improve this answer


























            1












            1








            1







            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





            share|improve this answer













            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






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 3 at 11:55









            Cindy MeisterCindy Meister

            16k102437




            16k102437

























                0














                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





                share|improve this answer




























                  0














                  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





                  share|improve this answer


























                    0












                    0








                    0







                    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





                    share|improve this answer













                    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






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jan 3 at 7:34









                    VariatusVariatus

                    5,7351524




                    5,7351524























                        0














                        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





                        share|improve this answer




























                          0














                          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





                          share|improve this answer


























                            0












                            0








                            0







                            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





                            share|improve this answer













                            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






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Jan 3 at 12:42









                            macropodmacropod

                            3,0442311




                            3,0442311






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54016322%2fhow-to-insert-printdate-field-code-in-a-sentence-using-word-vba%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                MongoDB - Not Authorized To Execute Command

                                How to fix TextFormField cause rebuild widget in Flutter

                                Npm cannot find a required file even through it is in the searched directory