在Word标头中使用两种字体格式的问题 [英] Problem using two font formats in Word header
问题描述
从Excel的子例程中,我试图在Word文档中创建一个标题,其中包含两个具有不同字体格式的单词,但是最后一种字体格式会获胜.任何帮助,将不胜感激!以下是我当前的代码段.
From an subroutine in Excel, I am trying to create a header in a Word document with two words each with different font formatting however the last font formatting wins. Any help would be appreciated! Below is my current code snippet.
With myDoc.Sections(1).Headers(wdHeaderFooterPrimary).Range
.Font.Name = "Courier New"
.Font.Size = 10
.Font.Bold = True
.Font.Color = wdColorGreen
.text = "TEXT LINE 1" & vbLf
.Font.Name = "Calibri Light"
.Font.Size = 16
.Font.Bold = False
.Font.Color = wdColorBlack
.text = .text & "TEXT LINE 2"
....the rest of the code....
更新:我通过明确设置范围来解决此问题.请参见下面的代码段.
UPDATE: I solved the issue by explicitly setting the range. See code snippet below.
With myDoc.Sections(1).Headers(wdHeaderFooterPrimary).Range
.Start = 0
.text = "TEXT LINE 1" & vbLf
.End = Len(.text)
.Font.Name = "Courier New"
.Font.Size = 10
.Font.Bold = True
.Font.Color = wdColorGreen
.ParagraphFormat.Alignment = wdAlignParagraphCenter
.Start = Len(.text) + 1
.text = "TEXT LINE 2"
.End = Len(.text) + .Start
.Font.Name = "Calibri Light"
.Font.Size = 16
.Font.Bold = False
.Font.Color = wdColorBlack
推荐答案
与更新"中发布的代码相比,此方法可以更有效/更轻松地完成.依靠 Start
和 End
values 对Word总是有点无能为力,因为Word可以将隐藏"的内容粘贴到文本流中.要到达 Range
的开始或结尾,使用 Collapse
更为可靠.这也将比使用值进行计算更快.
This can be done a bit more efficiently / elegantly than the code posted in the "update". Relying on Start
and End
values is always a bit chancy with Word since Word can stick "hidden" content into the text flow. To get to the beginning or end of a Range
it's more reliable to use Collapse
. This will also be faster than doing calculations with values.
Dim rng as Word.Range
Set rng = myDoc.Sections(1).Headers(wdHeaderFooterPrimary).Range
With
'.Start = 0 'Not necessary as this will be the default position
.text = "TEXT LINE 1" & vbLf
'.End = Len(.text) 'Also not necessary, see further down...
.Font.Name = "Courier New"
.Font.Size = 10
.Font.Bold = True
.Font.Color = wdColorGreen
.ParagraphFormat.Alignment = wdAlignParagraphCenter
.Collapse wdCollapseEnd 'put focus at end of range
'.Start = Len(.text) + 1 'calculation not necessary as range has been collapsed
.text = "TEXT LINE 2"
'.End = Len(.text) + .Start 'not necessary
.Font.Name = "Calibri Light"
.Font.Size = 16
.Font.Bold = False
.Font.Color = wdColorBlack
End With
这篇关于在Word标头中使用两种字体格式的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!