我正在处理Excel VBA中的大型项目并遇到以下问题。我使用VBA使Excel在MS Word中编译模板。我想在模板中突出显示某些短语黄色。它曾经在某个时刻工作,但之后停止工作,原因不明。当我现在运行代码时,该部分无错地执行,但这些单词不会突出显示,即使我确信它们在模板中。我使用debug.print来查找.replacement.highlight值的值,并在立即窗口中显示9999999,如果将鼠标光标悬停在中断模式下的表达式上,则显示-1。.replacement.highlight = true在MS Word中VBA不起作用
代码的相关片段引述如下:
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 [email protected]@@>>>"
.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的新手,没有正式的编程教育。
最好的问候,
TYRO
我还没有尝试过你的代码,但是在你设置.Replacement.Highlight之前,你是否尝试过使用“.Replacement.ClearFormatting”以及.ClearFormatting? (以防万一某些东西在它不应该“粘住”的时候) – 2016-07-05 09:41:31