【问题标题】:How to trigger the find replace sequence in a Word document using Excel VBA?如何使用 Excel VBA 在 Word 文档中触发查找替换序列?
【发布时间】:2021-04-04 12:50:54
【问题描述】:

我正在寻找一种方法:

  1. 在 Excel 工作表中基于单元格 XX 打开 Word 文档(现在,我在单元格 XX 中列出了文档的完整路径。有没有办法可以根据 Word 文档文件名中的标识符打开文档?)
  2. 使用查找和替换编辑 Word 文档中的文本(Excel 和 Word doc 之间的链接,我正在更新这些链接的路径。旧路径是静态的,新路径会根据用户而变化,并将在单元格 XXX 中找到)李>
  3. 查找替换后触发更新 word 中的所有链接
  4. 断开这些链接
  5. 重命名word文档并将其保存在客户端文件夹中
Sub openfile()
    'opening word file based on cell value in excel, this part works
    Dim File As String

    File = Worksheets("HOME").Range("A54").Value
    Set wordapp = CreateObject("word.Application")
    wordapp.documents.Open File
    wordapp.Visible = True
    
      
   'finding and replacing text in word file that was opened with text in specific cell from the excel file, not working
    objSelection = wordapp.Selection
    objSelection.Selection.Find.ClearFormatting
    objSelection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "old pathway" 'this will be static text to always find
        .Replacement.Text = Worksheets("HOME").Range("A53").Value 'value in the cell changes depending on user
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute replace:=wdReplaceAll
    
    'would like to update all links in the word doc
    'would like to break specific links, only the excel links, in the word doc
    
    'would like to rename file and save into a different folder at this point, lost on how to code this

End Sub

我的主要问题是如何在我刚刚打开并“激活”的 Word 文档中触发查找替换序列。

当文档打开时,我收到一条错误消息

运行时错误 450
参数数量错误或属性分配无效

【问题讨论】:

  • 我怀疑您是否能够使用Selection 对象完成您的任务,因为Selection 与用户看到的内容相关联。因此 Word 的 Selection 与 Excel 的 Selection 不同,两者都取决于屏幕上的内容。相反,创建一个名为 Doc 的 Word 文档和一个名为 Ws 的 Excel 工作表,并将 Word 和 Excel 范围定义为您可以操作的 DocRngWsRng。考虑使用FileOpen 对话框来指定要打开的word 文档。
  • 您列出了您的要求并转储了一些代码,这不是本网站的工作方式。我们不是免费的代码编写服务。您需要清楚地解释您遇到的实际问题,并提出与您发布的代码相关的一个具体的问题。如果您花一些时间在tour 和阅读help center 页面以了解该网站的工作原理,然后再开始发布,您会发现您在这里的体验会好很多。
  • @KenWhite谢谢,我明白了。我的主要问题是如何在我刚刚打开并“激活”的 word 文档中触发查找替换序列。当文档打开时,我收到一条错误消息“运行时错误 450”错误数量的参数或无效的属性分配“。我的其他笔记是占位符,我更专注于“不工作”部分。为令人困惑的部分道歉格式化。

标签: excel vba ms-word


【解决方案1】:

查找/替换是否会真正完成这项工作取决于您是否将 Word 的域代码显示切换为“打开”(您没有相应的代码),链接是否仅在文档正文中,或者在页眉、页脚中,等等,以及这些对象的包装格式。

另一种方法是显式更改所有 StoryRanges 等中的链接对象源路径和/或文件名,为此尝试:

Sub ReplaceLinksInWordFile()
'Note: A Reference to the Word Object model is required, via Tools|References in the VBE
Dim wdApp As Word.Application, wdDoc As Word.Document, wdRng As Word.Range
Dim wdShp As Word.Shape, wdiShp As Word.InlineShape, wdFld As Word.Field
Dim StrOldPath As String, StrNewPath As String, bStart As Boolean
StrOldPath = "Old Path"
StrNewPath = Worksheets("HOME").Range("A53").Value
bStart = False: On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then 'Word isn't already running
  Set wdApp = CreateObject("Word.Application")
  bStart = True
End If
On Error GoTo 0
With wdApp
  .Visible = Not bStart
  Set wdDoc = .Documents.Open(Worksheets("HOME").Range("A54").Value, AddToRecentFiles:=False)
  With wdDoc
    For Each wdRng In .StoryRanges
      ' Go through the shapes in the story range.
      For Each wdShp In wdRng.ShapeRange
        With wdShp
          ' Skip over shapes that don't have links to external files.
          If Not .LinkFormat Is Nothing Then
            With .LinkFormat
              If .Type = wdLinkTypeOLE Then
                ' Replace the link to the external file.
                .SourceFullName = Replace(.SourceFullName, StrOldPath, StrNewPath)
                .Update
                .BreakLink
              End If
            End With
          End If
        End With
      Next wdShp
      ' Go through the inlineshapes in the story range.
      For Each wdiShp In wdRng.InlineShapes
        With wdiShp
          ' Skip over inlineshapes that don't have links to external files.
          If Not .LinkFormat Is Nothing Then
            With .LinkFormat
              If .Type = wdLinkTypeOLE Then
                ' Replace the link to the external file.
                .SourceFullName = Replace(.SourceFullName, StrOldPath, StrNewPath)
                .Update
                .BreakLink
              End If
            End With
          End If
        End With
      Next wdiShp
      ' Go through the fields in the story range.
      For Each wdFld In wdRng.Fields
        With wdFld
          ' Skip over fields that don't have links to external files.
          If Not .LinkFormat Is Nothing Then
            With .LinkFormat
              If .Type = wdLinkTypeOLE Then
                ' Replace the link to the external file.
                .SourceFullName = Replace(.SourceFullName, StrOldPath, StrNewPath)
                .Update
                .BreakLink
              End If
            End With
          End If
        End With
      Next wdFld
    Next wdRng
    .SaveAs2 Filename:=StrNewPath & .Name, FileFormat:=.SaveFormat, AddToRecentFiles:=False
    .Close False
  End With
  If bStart = True Then .Quit
End With
MsgBox "Done"
End Sub

【讨论】:

    猜你喜欢
    • 2019-05-20
    • 2021-10-13
    • 2018-11-11
    • 1970-01-01
    • 2021-05-29
    • 2014-10-12
    • 2021-04-27
    • 1970-01-01
    • 2017-08-06
    相关资源
    最近更新 更多