使用自动化在 Word 中设置样式
我有以下 VB 脚本,应该按以下方式打印:123456789(前 3 位数字 - 正常样式,接下来 3 位 - 强,最后 3 - 正常):
Set word = CreateObject("word.application")
word.visible = true
Set docs = word.documents
Set doc = docs.add()
Set sel = word.selection
Set oldStyle = sel.style
Set newStyle = doc.Styles("Strong")
sel.typeText("123")
sel.Style = newStyle
sel.typeText("456")
sel.style = oldStyle
sel.typeText("789")
但是,结果是:前 3 位数字正常,其余的,大胆的。我做错了什么?
I have the following VB script that is supposed to print: 123456789 in the following way (first 3 digits - normal style, next 3 - strong, last 3 - normal):
Set word = CreateObject("word.application")
word.visible = true
Set docs = word.documents
Set doc = docs.add()
Set sel = word.selection
Set oldStyle = sel.style
Set newStyle = doc.Styles("Strong")
sel.typeText("123")
sel.Style = newStyle
sel.typeText("456")
sel.style = oldStyle
sel.typeText("789")
However, the result is: first 3 digits normal and the rest, bold. What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在此部分:
Set oldStyle = sel.style
您创建了样式的引用(指针)。随后,您将样式更改为新样式。引用旧样式将返回指针,新样式也是如此。At this part:
Set oldStyle = sel.style
you create a Reference (pointer) to the style. Later on, you change style to the new style. Referencing oldstyle will return the pointer, so also the new style.