不能 select 超链接范围的一部分
Can't select part of hyperlink range
我的插件中的超链接根据某些条件包含 3 种颜色,在使用超链接之前我使用 documents.Range(start, end);
更改颜色和字体。
但是一旦添加到超链接颜色就消失了,实际上我不能select超链接范围的一部分,documents.Range(start, end);
returns所有范围都不是它的一部分。
问题是超链接从文本更改为对象模型对象,即 'Hyperlink' 对象。此对象具有 Hyperlink 样式,这是将其更改为 Hyperlink 对象后所看到的。
如果您想以不同的方式显示超链接,您可以更改超链接样式。
格式化部分超链接是可行的。然而,这并不简单。超链接实现为Word字段,即由字段代码和显示值组成(实际上是特殊类字段,并不是显示等所有相关信息文本存储在字段代码中)。
您可以通过按 Alt+F9 显示域代码(并通过再次按相同的快捷键再次隐藏它)。
当您设置范围的开始和结束以应用格式时,您需要考虑将字段代码添加到显示文本之前(尽管默认情况下您在查看 Range.Text
).例如,如果您的超链接从偏移量 100 开始,那么显示文本实际上将从偏移量 100 + 字段代码长度.
开始
此示例 VBA 宏解释了如何检索字段代码的长度并计算正确的偏移量:
Sub FormatHyperlink()
' set up a sample document
Dim doc As Document
Set doc = Application.Documents.Add
Selection.TypeText "This is a hyperlink to the "
Selection.Collapse wdCollapseEnd
Selection.Hyperlinks.Add Selection.Range, _
"https://whosebug.com/", , , "greatest webpage"
Selection.TypeText " ever."
' retrieve the hyperlink
Dim lnk As Hyperlink
Set lnk = ActiveDocument.Hyperlinks(1)
' retrieve the field code of the hyperlink
Dim rng As Range
Set rng = lnk.Range.Duplicate
rng.TextRetrievalMode.IncludeFieldCodes = True
Dim fieldCodeLength As Integer
fieldCodeLength = Len(rng.Text)
rng.TextRetrievalMode.IncludeFieldCodes = False
' format the first word of the display text with a different color
rng.Start = rng.Start + fieldCodeLength + 1
rng.Collapse
rng.MoveEnd wdWord, 1
rng.Font.ColorIndex = wdRed
' format the rest of the hyperlink with another color
Set rng = lnk.Range.Duplicate
rng.Start = rng.Start + fieldCodeLength + 1
rng.MoveStart wdWord, 1
rng.Font.ColorIndex = wdDarkBlue ' or use rng.Font.TextColor = RGB(x,x,x)
End Sub
我的插件中的超链接根据某些条件包含 3 种颜色,在使用超链接之前我使用 documents.Range(start, end);
更改颜色和字体。
但是一旦添加到超链接颜色就消失了,实际上我不能select超链接范围的一部分,documents.Range(start, end);
returns所有范围都不是它的一部分。
问题是超链接从文本更改为对象模型对象,即 'Hyperlink' 对象。此对象具有 Hyperlink 样式,这是将其更改为 Hyperlink 对象后所看到的。
如果您想以不同的方式显示超链接,您可以更改超链接样式。
格式化部分超链接是可行的。然而,这并不简单。超链接实现为Word字段,即由字段代码和显示值组成(实际上是特殊类字段,并不是显示等所有相关信息文本存储在字段代码中)。
您可以通过按 Alt+F9 显示域代码(并通过再次按相同的快捷键再次隐藏它)。
当您设置范围的开始和结束以应用格式时,您需要考虑将字段代码添加到显示文本之前(尽管默认情况下您在查看 Range.Text
).例如,如果您的超链接从偏移量 100 开始,那么显示文本实际上将从偏移量 100 + 字段代码长度.
此示例 VBA 宏解释了如何检索字段代码的长度并计算正确的偏移量:
Sub FormatHyperlink()
' set up a sample document
Dim doc As Document
Set doc = Application.Documents.Add
Selection.TypeText "This is a hyperlink to the "
Selection.Collapse wdCollapseEnd
Selection.Hyperlinks.Add Selection.Range, _
"https://whosebug.com/", , , "greatest webpage"
Selection.TypeText " ever."
' retrieve the hyperlink
Dim lnk As Hyperlink
Set lnk = ActiveDocument.Hyperlinks(1)
' retrieve the field code of the hyperlink
Dim rng As Range
Set rng = lnk.Range.Duplicate
rng.TextRetrievalMode.IncludeFieldCodes = True
Dim fieldCodeLength As Integer
fieldCodeLength = Len(rng.Text)
rng.TextRetrievalMode.IncludeFieldCodes = False
' format the first word of the display text with a different color
rng.Start = rng.Start + fieldCodeLength + 1
rng.Collapse
rng.MoveEnd wdWord, 1
rng.Font.ColorIndex = wdRed
' format the rest of the hyperlink with another color
Set rng = lnk.Range.Duplicate
rng.Start = rng.Start + fieldCodeLength + 1
rng.MoveStart wdWord, 1
rng.Font.ColorIndex = wdDarkBlue ' or use rng.Font.TextColor = RGB(x,x,x)
End Sub