如何使用单词VBA在句子中插入PRINTDATE字段代码?
我想在我的文档中看到以下句子:“这份文档最后一次打印是在2019年1月1日凌晨2:10”。日期和时间必须是动态的。现在我只有这一部分:如何使用单词VBA在句子中插入PRINTDATE字段代码?,vba,ms-word,Vba,Ms Word,我想在我的文档中看到以下句子:“这份文档最后一次打印是在2019年1月1日凌晨2:10”。日期和时间必须是动态的。现在我只有这一部分: Element.Range.Text = "This document was last printed on " 它工作正常,但缺少日期部分。如何将PRINTDATE字段连接到此句子中 我试过了 Selection.Fields.Add Range:=.Footers(wdHeaderFooterPrimary).Range, Type:=wdFieldPr
Element.Range.Text = "This document was last printed on "
它工作正常,但缺少日期部分。如何将PRINTDATE
字段连接到此句子中
我试过了
Selection.Fields.Add Range:=.Footers(wdHeaderFooterPrimary).Range, Type:=wdFieldPrintDate, Text:="\@""DD MMM YYYY""", preserveformatting:=True
它是有效的,但是它覆盖了所有的页脚,我如何将它附加到文本中,使其与我的示例类似?下面的代码将更改文档中的所有页脚,以显示“文档上次打印于{dd.mm.yyyy}。可能会对其进行修改,以不替换所有的页脚
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
要访问与所选内容关联的页脚,请执行以下操作:
Dim rng as Word.Range
Set rng = Selection.Sections(1).Footers(wdHeaderFooterPrimary).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
注意:我强烈建议使用PreserveFormatting:=False
,因为如果应用了其他格式,则字段更有可能选择周围文本的格式。将此设置为True
将保留字段中原始字符数的原始应用格式。如果字段已更新且字符数更改时,某些字符的格式可能与其他字符不同
事实上,我更喜欢使用如下方法,所有字段内容都在Text
参数中,包括CharFormat
开关。CharFormat
将强制整个字段使用应用于字段代码中第一个字符的字符格式-更可靠:
rng.Fields.Add Range:=rng, Type:=wdFieldEmpty, _
Text:="PrintDate \@""DD MMM YYYY"" \* CharFormat", preserveformatting:=False
尝试:
我想您正在从.Hum中查找和
wdFieldPrintDate
。我尝试了Selection.Fields.Add Range:=.Footers(wdheaderfootperprimary)。Range,Type:=wdFieldPrintDate,Text:=“\@”“DD MMM YYYY”,preserveformatting:=True
并且它可以工作,但是它会覆盖所有的页脚,我如何将其附加到文本中以使其与我的示例类似?
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