vba - 在word中添加超链接后如何在VBA中重置样式
问题描述
我正在使用从这里https://wordribbon.tips.net/T000672_Making_Hyperlinks_from_Coded_Text.html改编的代码, 以根据匹配的文本自动生成 word 中的超链接。
我有一个问题,我想保留选择的原始格式(样式)。我试图开始工作的代码的关键部分是:
Dim my_hyperlink As Hyperlink
Dim curStyle As Object
...
... ' find the text I want to match now apply hyperlink
With Selection
curStyle = .Style
Set my_hyperlink =ActiveDocument.Hyperlinks.Add(Anchor:=Selection.Range,_
Address:="https://example.com",SubAddress:="")
my_hyperlink.Range.Style = curStyle
End With
我正在尝试将当前样式存储在 curStyle 中,应用超链接,然后重置样式。
我得到了错误虽然“对象变量或块变量不是”
是否有一些技巧来存储样式应用超链接然后恢复原始样式?
解决方案
我可以提供两种选择
- 修改超链接样式(一次)
- 重置字体样式(之后)
Sub OldStyleHyperlinkStyleChange()
Dim my_hyperlink As Hyperlink
'...
'... ' find the text I want to match now apply hyperlink
With ActiveDocument.Styles(wdStyleHyperlink).Font
.ColorIndex = wdAuto
.Underline = wdUnderlineNone
End With
With Selection
Set my_hyperlink = ActiveDocument.Hyperlinks.Add(Anchor:=.Range, _
Address:="https://example.com", SubAddress:="")
End With
End Sub
Sub OldStyleReset()
Dim my_hyperlink As Hyperlink
'...
'... ' find the text I want to match now apply hyperlink
With Selection
Set my_hyperlink = ActiveDocument.Hyperlinks.Add(Anchor:=.Range, _
Address:="https://example.com", SubAddress:="")
my_hyperlink.Range.Font.Reset
End With
End Sub
推荐阅读
- node.js - 如何修复:react-admin 中未定义响应
- linux - 用于将输出重定向到具有日期文件名的日志文件的 Linux 脚本
- robotframework - 机器人框架:如何从 __init__ 文件访问全局变量
- python - 在熊猫数据框中查找到下一个更高值的距离
- r - 将列表列表转换为小标题
- excel - 如何确定变量的先前值?
- javascript - 重命名 webpack/gatsby 块映射字符串
- python - 在 Pandas Python 中折叠列中的一些行
- linq - 使用 linq 在多对多表中进行计数
- google-app-engine - 谷歌应用引擎阻止两个服务之间的 OPTIONS 请求