【发布时间】:2016-11-06 10:38:56
【问题描述】:
我在 Excel VBA 中处理一个更大的项目,遇到了以下问题。我正在使用 VBA 使 Excel 在 MS Word 中编译模板。我想在黄色模板中突出显示某些短语。它曾经在某个时候工作,但后来它停止了,没有明显的原因。当我现在运行代码时,该部分会在没有错误的情况下执行,但单词不会突出显示,即使我确定它们在模板中也是如此。我使用 debug.print 找出 .replacement.highlight 值的值,它在立即窗口中显示 9999999,如果我将鼠标光标悬停在中断模式下的表达式上,则显示 -1。
下面引用了相关代码片段:
Dim WdApp As Word.Application
Set WdApp = New Word.Application
With WdApp
.ActiveDocument.Select
With .Selection.Find
'the code creating the document and writing the template continues here
.ClearFormatting
.Text = "{Optional: Please also confirm the terms of transactions and other key information (for example: rights of return, allowances and rebates, special agreements, payment terms, incoterms, etc.) which may affect the accounting for the transactions.}"
.Replacement.Highlight = True
.Execute Replace:=wdReplaceAll, Forward:=True, Wrap:=wdFindContinue
.ClearFormatting
.Text = PlaceholderAdditionalInfoRequest
.Replacement.Text = .Text
.Replacement.Highlight = True
.Execute Replace:=wdReplaceAll, Forward:=True, Wrap:=wdFindContinue
.ClearFormatting
.Text = "<<<@@@Client's Letterhead@@@>>>"
.Replacement.Text = .Text
.Replacement.Highlight = True
.Execute Replace:=wdReplaceAll, Forward:=True, Wrap:=wdFindContinue
.ClearFormatting
.Text = "{Optional: A statement of account with the above invoices marked is attached. ALTERNATIVELY: Copies of the above invoices are attached.}"
.Replacement.Highlight = True
.Execute Replace:=wdReplaceAll, Forward:=True, Wrap:=wdFindContinue
End With
End With
有人知道发生了什么吗?我会很感激任何指点,特别是如果用简单的英语表达的话:) 我是 VBA 新手,没有接受过正规的编程教育。
最好的问候,
泰罗
【问题讨论】:
-
我没有尝试过您的代码,但您是否尝试过在设置 .Replacement.Highlight 之前使用“.Replacement.ClearFormatting”以及 .ClearFormatting? (以防万一有些东西在不应该的时候“粘住”)
标签: vba excel ms-word highlighting