繁体   English   中英

如何使用 Excel VBA 在 Word 文档中触发查找替换序列?

[英]How to trigger the find replace sequence in a Word document using Excel VBA?

我正在寻找一种方法:

  1. 基于 Excel 工作表中的单元格 XX 打开 Word 文档(现在,我在单元格 XX 中列出了文档的完整路径。有没有办法可以根据 Word 文档文件名中的标识符打开文档?)
  2. 使用查找和替换编辑 Word 文档中的文本(Excel 和 Word doc 之间的链接,我正在更新这些链接的路径。旧路径是 static,新路径会根据用户而变化,并将在单元格 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
arguments 编号错误或属性分配无效

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM