首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >.replacement.highlight = true在MS Word VBA中不起作用

.replacement.highlight = true在MS Word VBA中不起作用
EN

Stack Overflow用户
提问于 2016-07-05 00:31:26
回答 1查看 519关注 0票数 0

我在Excel VBA中处理一个较大的项目,遇到了以下问题。我正在使用VBA让Excel在MS Word中编译一个模板。我想突出显示模板黄色中的某些短语。它曾经在某个时候起作用,但后来它停止了,没有明显的原因。当我现在运行代码时,该部分执行时没有错误,但是单词没有突出显示,即使我确定它们在模板中。我使用debug.print来查找.replacement.highlight值的值,它在即时窗口中显示9999999,如果在中断模式下将鼠标光标悬停在表达式上,则显示-1。

下面引用了代码的相关片段:

代码语言:javascript
运行
复制
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

EN

回答 1

Stack Overflow用户

发布于 2016-07-06 13:23:11

试试这个:

代码语言:javascript
运行
复制
Options.DefaultHighlightColorIndex = wdYellow

之前:

代码语言:javascript
运行
复制
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)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38189084

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档