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;
}
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
add a comment |
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
I think you're looking forFields.Add
andwdFieldPrintDate
from the WdFieldType enumeration.
– BigBen
Jan 3 at 4:34
Hum... I triedSelection.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
add a comment |
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
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
vba ms-word
edited Jan 3 at 11:47
Cindy Meister
16k102437
16k102437
asked Jan 3 at 4:24
JChrisJChris
69641327
69641327
I think you're looking forFields.Add
andwdFieldPrintDate
from the WdFieldType enumeration.
– BigBen
Jan 3 at 4:34
Hum... I triedSelection.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
add a comment |
I think you're looking forFields.Add
andwdFieldPrintDate
from the WdFieldType enumeration.
– BigBen
Jan 3 at 4:34
Hum... I triedSelection.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
add a comment |
3 Answers
3
active
oldest
votes
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
add a comment |
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
add a comment |
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
add a comment |
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
add a comment |
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
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
answered Jan 3 at 11:55
Cindy MeisterCindy Meister
16k102437
16k102437
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Jan 3 at 7:34
VariatusVariatus
5,7351524
5,7351524
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Jan 3 at 12:42


macropodmacropod
3,0442311
3,0442311
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
I think you're looking for
Fields.Add
andwdFieldPrintDate
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