我在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新手,没有接受过正规的编程教育。
诚挚的问候,
Tyro
发布于 2016-07-06 13:23:11
试试这个:
Options.DefaultHighlightColorIndex = wdYellow
之前:
With .Selection.Find
您似乎没有将Find
函数应用于模板,而是将其应用于在创建模板之前激活的文档。尝试移动这些行:
the code creating the document and writing the template continues here…
在这些行之前:
.ActiveDocument.Select With .Selection.Find
使Word应用程序WdApp
可见(WdApp.Visible = True
),以查看代码正在做什么,并确保在任务完成后将其关闭( WdApp.Quit
)。
https://stackoverflow.com/questions/38189084
复制相似问题