【问题标题】:executing a find and replace in a word doc from Excel VBA在 Excel VBA 的 word 文档中执行查找和替换
【发布时间】:2018-11-11 16:09:21
【问题描述】:

我正在尝试根据 Excel 中的数据打开特定的 Word 模板(该部分正在运行)。然后,一旦打开模板,我就会尝试根据 Excel 文档中的标签进行查找并替换为同一列中的相应数据。当我运行宏时,它会打开模板并且只是旋转和旋转而不给我输出。代码如下:

` Sub New_Purification_SOP()
    '
    ' New_Purification_SOP Macro
    ''Open an existing Word Document from Excel
      Dim objWord As Object
      Dim myValue As Variant
      Dim PurCol As Variant
     'open input box requesting line of the material to be made
      myValue = InputBox("Select Row to create SOP")

      Set objWord = CreateObject("Word.Application")
      objWord.Visible = True
      'Change the directory path and file name to the location
      'of the document you want to open from Excel
       If ActiveSheet.Cells(myValue, 10) = "Supe" And _
          ActiveSheet.Cells(myValue, 12) = "IgG1" Then
         objWord.Documents.Open "S:\generic filename"
         With objWord
            For PurCol = 3 To 13 'move through columns left to right
               TagName = .Cells(10, PurCol).Value 'get tag name from row
               TagValue = .Cells(myValue, PurCol).Value 'get tag name from row
               With objWord.Content.Find
                     .Text = TagName
                     .Replacement.Text = TagValue
                     .Wrap = wdFindContinue
                     .Execute Replace:=wdReplaceAll  'Forward = True, Wrap = _ 
                        wdFindContinue
                End With
            Next PurCol
       End With`
...

我对 VBA 很陌生,所以请尽可能多地批评。

【问题讨论】:

  • 我假设 objWord 是一个 Word 对象?你如何使用.Cells?如果你用F8 单步执行你的代码,你能看到循环从哪里开始吗?
  • 首先从您的代码中删除wdFindContinue。如果有的话,使用 wdFindStop 并且只使用一次(Execute 命令看起来很奇怪 - 也应该注释掉那个断线)。
  • 如果这没有帮助,请包括一个将运行的代码 sn-p,就目前而言。我们无法复制您拥有的内容,因为我们没有您的电子表格并且您没有包含代码的结尾 - 该过程不完整。
  • 感谢大家的帮助。删除 wdFindContinue 是正确的。 BruceWayne 发现 .Cells 无法正常工作。
  • 感谢您的确认 - 我已将其写为答案,因为在使用 Word 的查找/替换时这可能是一个令人讨厌的陷阱 :-)

标签: vba excel replace ms-word


【解决方案1】:

您可能希望在打开/创建单词对象时包含错误处理。我发现如果我已经有一个 word up 的实例,它实际上可能会使应用程序崩溃。这可能会导致您的程序出现问题。

source: https://stackoverflow.com/questions/17177412/close-release-word-object-in-vba
On Error Resume Next
Set objWord = GetObject(, "Word.Application")

'We've tried to get Word but if it's nothing then it isn't open
If objWord Is Nothing Then
    Set objWord = CreateObject("Word.Application")
End If

'It's good practice to reset error warnings
On Error GoTo 0

之后,查找/替换故障排除可能是一项痛苦的任务。请参阅这篇文章了解原因:https://gregmaxey.com/word_tip_pages/words_fickle_vba_find_property.html。您可能想要做的是使用宏记录器检查查找/替换代码,然后将其提升到您的循环中。它可以节省大量的调试时间。

【讨论】:

    【解决方案2】:

    问题来自在您的代码中使用Find.Wrap = wdFindContinue

    此属性指示 Word 继续执行查找 - 有点像用户在对话框中连续按“查找下一个”。在编码时应该很少(更像是从不)使用它,因为它会导致一个无休止的循环,就像在这个例子中一样。

    在这种情况下,由于正在使用 wdReplaceAll - 意味着代码一步完成 - Wrap 不一定需要指定。但通常好的做法是使用Find.Wrap = wdFindStop

    【讨论】:

      猜你喜欢
      • 2021-10-13
      • 2014-10-12
      • 2016-11-05
      • 2018-01-18
      • 2017-08-06
      • 2021-03-13
      • 2019-05-20
      • 2020-12-26
      • 1970-01-01
      相关资源
      最近更新 更多