【问题标题】:Open a Word Doc from Excel using VBA to find a Date text and replace it with a date text from a cell in Excel使用 VBA 从 Excel 中打开 Word Doc 以查找日期文本并将其替换为 Excel 中单元格中的日期文本
【发布时间】:2020-01-23 23:37:12
【问题描述】:

我有一个带有文本的 word doc,其中还有一个显示这种格式的日期文本:

text text text 17.01.2020 text text text.

我想用 Excel 中的一个单元格替换上述日期文本,该单元格也是一个类似于 18.01.2020 的日期。

所以 VBA 代码应该执行以下操作:

1.打开word文档

2.查找文本并将其替换为 Excel 中的文本

3.保存Word文档。

Sub DocSearchandReplace()
Dim wdApp As Object, wdDoc As Object
Set wdApp = CreateObject("word.application")
wdApp.Visible = True
Set wdDoc = wdApp.Documents.Open("C:\sampletest.docx")
With wdDoc.Content.Find
  .Date = "???"
  .Replacement.Cell = "referencing a cell from Excel??"
  .Wrap = wdFindContinue
  .Execute Replace:=wdReplaceAll
End With

wdDoc.Save 'it however always prompt a word warning, how to surpress it?
wdDoc.Close 'it only closes the doc inside Word without closing the whole program.

Set wdApp = Nothing: Set wdDoc = Nothing
End Sub

我不确定在 .Date 和 .Replacement.Cell 部分应该写什么。

【问题讨论】:

  • “有人可以帮助使用 VBA 实现上述步骤吗?” 实际上正是这种情况,这不是一个真正的问题(请参阅我上面评论中的链接)。也只是说 “它不工作” 不是错误描述。 • 请描述您的代码有什么问题、什么已经在工作以及您在哪里遇到错误?您的代码与您的预期有何不同?你到底卡在了哪一部分?您的代码中的哪些问题阻止您实施它? • 另外我认为.Date = "???" 中不存在.Content.Find
  • 这正是我不知道怎么写的。我用了 ???表明我不知道该写什么。我真的不知道你想让我表现出我在那个部分中迷失了多少。希望我的编辑这次有效吗?我很欣赏你的建议,但我问过这样的问题没有任何问题。我想我已经足够精确了。如果你不知道答案,那很好。请让其他人回答我的问题。谢谢。

标签: excel vba ms-word


【解决方案1】:

问题是您的语法错误。 .Date.Replacement.Cell 属性在 Find 对象中不存在。请务必阅读find object 的手册,不要发明属性。

根据Finding and Replacing Text or Formatting 的正确语法类似于:

With wdDoc.Content.Find 
    .ClearFormatting 
    .Text = "17.01.2020" 'your date to replace
    .Replacement.ClearFormatting 
    .Replacement.Text = ThisWorkbook.Worksheets("Sheet1").Range("A1").Text 'read value from sheet1 cell A1
    .Execute Replace:=wdReplaceAll, Forward:=True, Wrap:=wdFindContinue 
End With

根据 Cindys 的评论,Word 中存在通配符匹配,类似于使用正则表达式。

With wdDoc.Content.Find 
    .ClearFormatting 
    .Text = "[0-9]{2}.[0-9]{2}.[0-9]{4}"  'will match ##.##.####
    .MatchWildcards = True 'makes sure the above wildcards are recognized
    .Replacement.ClearFormatting 
    .Replacement.Text = ThisWorkbook.Worksheets("Sheet1").Range("A1").Text 'read value from sheet1 cell A1
    .Execute Replace:=wdReplaceAll, Forward:=True, Wrap:=wdFindContinue 
End With

由于您使用的是后期绑定,因此您必须在使用之前为 WdFindWrapWdReplace 枚举定义您的 Word 常量

Const wdReplaceAll As Long = 2
Const wdFindContinue As Long = 1

或用它们的值替换它们

.Execute Replace:=2, Forward:=True, Wrap:=1

或设置对 Word 的引用并使用预先绑定,以便自动定义它们。

【讨论】:

  • 谢谢@Peh .Text = "17.01.2020" 日期不是硬编码的。它是动态的。代码应该找到该日期使用该格式的任何内容并将其替换。这就是我没有使用 .TEXT 的原因。
  • 嗯,你显然只能替换你所知道的。如果您不知道要替换的日期,则无法搜索。然后你需要找到一个不同的模式,你必须为此使用正则表达式(Word 不支持开箱即用)。见Is there a way to search for a pattern in a MS Word document?
  • 所以您是说 Word VBA 找不到使用这种格式 dd.mm.yyyy 的文本字符串?好的,那么这段代码不适合这个目的。
  • @Pᴇʜ 非常感谢。我没有忘记你。一旦我检查了代码,我会告诉你的。谢谢。
  • @purpleblau 是的,请打开一个新问题来描述您的问题,并显示您尝试过的确切代码和收到的错误消息。如果是新问题,我们无法处理 cmets 中的后续问题,问题必须作为问题提出。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-09-20
  • 1970-01-01
  • 2017-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多