无法在其他位置另存为word文档

Unable to save as word document in another location

Public Sub WordFindAndReplace()
    Dim wrdApp, wrdDoc
    Dim ws As Worksheet, msWord As Object, itm As Range
    Dim sFolder As String
    Dim filename As String
    Dim Path As String
   
       
    Path = "C:\Rohan\Automations\CM Plan Automation\Upload\"
    filename = Range("C4").Text
    Set ws = ActiveSheet
    Set msWord = CreateObject("Word.Application")

    With msWord
        .Visible = True
        .documents.Open "C:\Rohan\Automations\CM Plan Automation\Engagement Name_Configuration Management Plan.docx"
        .Activate

        With .ActiveDocument.Content.Find
            .ClearFormatting
            .Replacement.ClearFormatting

            For Each itm In ws.UsedRange.Columns("B").Cells

                .Text = itm.Value2                          'Find all strings in col A

                .Replacement.Text = itm.Offset(, 1).Value2  'Replacements from col B

                .MatchCase = False
                .MatchWholeWord = False

                .Execute Replace:=2     'wdReplaceAll (WdReplace Enumeration)
            Next
        
        End With
        
        
        .SaveAs2 filename:=filename & _
FileFormat:=wdFormatDocumentDefault, AddtoRecentFiles:=False
        
        '.Quit SaveChanges:=True
         
        '.Quit
        
        '.SaveAs2 Filename:=("file name goes here"), _
    FileFormat:=wdFormatXMLDocument, AddtoRecentFiles:=False
        
        
       '.SaveAs filename:=filename & ".docx"
        
       '.SaveAs2 filename:="C:\Rohan\Automations\CM Plan Automation\Upload\" & ".docx",
        'FileFormat:=wdFormatDocumentDefault
        
        
        
    End With
    
    MsgBox "Done"
    
    
End Sub

问题是您的 SaveAs 代码出现在 With msWord 块中并且缺少文档对象。

您的问题与您使用后期绑定(将 Word 声明为对象)有关。在这种情况下使用后期绑定没有什么价值。使用它意味着编译器不知道您的代码是错误的。它还剥夺了您在对象浏览器中使用 IntelliSense 和访问 Word 对象库的权利。

后期绑定的唯一优势是版本独立性,即您可以在较新的版本中编写代码,理论上它也可以在较旧的版本中运行。但是,除非您还对主机应用程序代码使用后期绑定,否则这种优势就会消失,因为 Office 通常作为一个包安装。在使用其他 Office 应用程序时最好使用早期绑定,并为其他常用库保留后期绑定,例如ADO 或 XML.

此外,当您的代码打开特定文档时,您应该避免使用 ActiveDocument。改用变量,如下所示。

Public Sub WordFindAndReplace()
    Dim msWord As Word.Application, wrdDoc As Word.Document
    Dim ws As Worksheet, itm As Range
    Dim sFolder As String
    Dim filename As String
    Dim Path As String
   
       
    Path = "C:\Rohan\Automations\CM Plan Automation\Upload\"
    filename = Range("C4").Text
    Set ws = ActiveSheet
    Set msWord = CreateObject("Word.Application")

    With msWord
        .Visible = True
        Set wrdDoc = .documents.Open("C:\Rohan\Automations\CM Plan Automation\Engagement Name_Configuration Management Plan.docx")

        With wrdDoc.Content.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .MatchCase = False
            .MatchWholeWord = False

            For Each itm In ws.UsedRange.Columns("B").Cells

                .Text = itm.Value2                          'Find all strings in col A

                .Replacement.Text = itm.Offset(, 1).Value2  'Replacements from col B

                .Execute Replace:=2     'wdReplaceAll (WdReplace Enumeration)
            Next
        
        End With
        
        wrdDoc.SaveAs2 filename:=filename, FileFormat:=wdFormatDocumentDefault, AddtoRecentFiles:=False
        
    End With
    
    MsgBox "Done"
    
End Sub