【问题标题】:word replacement macro in excel does not workexcel中的单词替换宏不起作用
【发布时间】:2014-09-20 19:52:09
【问题描述】:

我正在尝试将我的 Excel 结果输入到预先制作的 Word 文档中。 我想为此使用宏。我对代码编写游戏相当陌生,但我了解基础知识。 我试图从不同的课程中复制,但它们都不起作用。

这个简单的宏应该把'x1'变成'anything', 但它对我所做的只是打开文档并选择文档中的 x1。

有人知道我哪里出错了吗?

Private Sub CommandButton1_Click()
    Set wrdApp = CreateObject("Word.Application")
    wrdApp.Visible = True
    Set wrdDoc = wrdApp.Documents.Open("C:\Users\mycomputer\Documents\PremadeDocument.docx")

        wrdApp.Selection.Find.ClearFormatting
        wrdApp.Selection.Find.Replacement.ClearFormatting
    With wrdApp.Selection.Find
        .Text = "x1"
        .Replacement.Text = "anything"
    '    .Forward = True
        .Wrap = wdFindContinue
    '    .Format = False
    '    .MatchCase = False
    '    .MatchWholeWord = False
    '    .MatchWildcards = False
    '    .MatchSoundsLike = False
    '    .MatchAllWordForms = False
    End With
        wrdApp.Selection.Find.Execute Replace:=wdReplaceAll

    Set wrdDoc = Nothing
    Set wrdApp = Nothing
End Sub

【问题讨论】:

  • 不确定,但会是 wrdDoc.selection 等而不是 wrdApp?

标签: excel vba replace ms-word


【解决方案1】:

为 wdReplaceAll 添加 Word Enumerated Const

Const wdReplaceAll = 2 

宏定义之后

Private Sub CommandButton1_Click()
Const wdReplaceAll = 2

【讨论】:

    猜你喜欢
    • 2019-10-11
    • 1970-01-01
    • 2017-02-14
    • 2018-04-08
    • 2013-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多