vba 不使用 .Select 在 Word 2010 标题中插入文本和字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14747261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Inserting text and fields in Word 2010 header without using .Select
提问by Fredrik P
I am trying to fix up a Word 2010 page header containing fields for filename, save date and page number as well as some text between each, like so: filename+" "+save date+tab+page number. However, I can't seem to get the strings in their right places. What I have so far is this:
我正在尝试修复包含文件名、保存日期和页码字段以及每个字段之间的一些文本的 Word 2010 页眉,如下所示:文件名+""+保存日期+标签+页码。但是,我似乎无法将字符串放在正确的位置。到目前为止,我所拥有的是:
Sub CreateHeader()
Dim myRange As Range
With ActiveDocument
Set myRange = .Sections(1).Headers(wdHeaderFooterPrimary).Range
.Fields.Add Range:=myRange, Type:=wdFieldFileName, PreserveFormatting:=True
myRange.Collapse wdCollapseEnd
myRange.InsertAfter (" ")
myRange.Collapse wdCollapseEnd
.Fields.Add Range:=myRange, Type:=wdFieldSaveDate, Text:="\@ YYYY-MM-DD", PreserveFormatting:=True
myRange.InsertAfter (Chr(9))
myRange.Collapse wdCollapseEnd
.Fields.Add Range:=myRange, Type:=wdFieldPage, PreserveFormatting:=True
End With
End Sub
However, after executing the sub, the different parts are not were I intend. Instead, they show up as filename+" "+tab+page number+save date. What am I doing wrong here? If it is at all possible, I would prefer not resorting to .Select
.
但是,在执行 sub 之后,不同的部分并不是我想要的。相反,它们显示为文件名+" "+标签+页码+保存日期。我在这里做错了什么?如果可能的话,我宁愿不诉诸于.Select
.
(Please note that I recently asked a similar question.)
(请注意,我最近问了一个类似的问题。)
采纳答案by CuberChase
I believe your problem here stems from the fact that you set the myRange
variable to the header text which, when empty, is simply the first (empty) character. After you add the Savedate it seems to go out of this original range. You need to add two things to make your code work.
我相信您的问题源于您将myRange
变量设置为标题文本,当为空时,它只是第一个(空)字符。添加 Savedate 后,它似乎超出了这个原始范围。您需要添加两件事才能使您的代码工作。
Firstly, you want to collapse to the end after each insertion but you also need to redefine the header range (myRange
variable) to the Header after inserting the SaveDate.
首先,您希望在每次插入后折叠到最后,但您还需要myRange
在插入 SaveDate 后将标题范围(变量)重新定义为标题。
It's a bit ugly but the following code seems to do what you desire. Note that if I don't put the last wdCollapseEnd
in the code it doesn't work.
这有点难看,但以下代码似乎可以满足您的需求。请注意,如果我不将最后一个wdCollapseEnd
放在代码中,则它不起作用。
Finally, I'd update your fields at the end just so you don't have to manually do it yourself.
最后,我会在最后更新您的字段,这样您就不必自己手动完成。
Sub CreateHeader()
Dim myRange As Range
With ActiveDocument
Set myRange = .Sections(1).Headers(wdHeaderFooterPrimary).Range
.Fields.Add Range:=myRange, Type:=wdFieldFileName, PreserveFormatting:=True
myRange.Collapse wdCollapseEnd
myRange.InsertAfter (" ")
myRange.Collapse wdCollapseEnd
.Fields.Add Range:=myRange, Type:=wdFieldSaveDate, Text:="\@ YYYY-MM-DD", PreserveFormatting:=True
Set myRange = .Sections(1).Headers(wdHeaderFooterPrimary).Range
myRange.Collapse wdCollapseEnd
myRange.InsertAfter (Chr(9))
myRange.Collapse wdCollapseEnd
.Fields.Add Range:=myRange, Type:=wdFieldPage, PreserveFormatting:=True
myRange.Fields.Update
End With
End Sub
结束子