【问题标题】:Excel to Word Macro resulting in Run-time error 462Excel 到 Word 宏导致运行时错误 462
【发布时间】:2023-01-29 00:40:59
【问题描述】:

我写了一个驻留在 Excel 工作簿中的 VBA 宏。运行时,会打开一个已有的Word文档(与Excel工作簿在同一目录下),将Excel工作簿单元格中的部分内容复制到Word文档中,将Word文档另存为新名称(与Excel工作簿同目录)目录)并杀死原始的 Word 文档。此过程在首次运行时按预期工作。但是在第二次运行时,我得到了运行时错误 462。我确信这是由于我对在 VBA 代码中创建和使用应用程序实例一无所知(我刚刚开始学习)。我正在使用适用于企业的 Microsoft 365 应用。

Sub ExcelToWord()

    Dim wordApp As Word.Application
    Dim wDoc As Word.Document
    Dim strFile As String

'Open Word file
    strFile = ("G:\HOME\Word File.docx")
    Set wordApp = CreateObject("word.Application")
    Set wDoc = wordApp.Documents.Open("G:\HOME\Word File.docx")
    wordApp.Visible = True

'Copy data from Excel to Word
    wDoc.ContentControls(1).Range.Text = Sheets("Model").Cells(4, 2)
    wDoc.ContentControls(2).Range.Text = Format(Date, "mm/dd/yyyy")
    wDoc.ContentControls(3).Range.Text = Sheets("Model").Range("X4")
    
    Word.Application.Activate

'Save Word Document with new name
    ActiveDocument.SaveAs Filename:=ActiveDocument.Path & "\" & Format(Sheets("Model").Range("B14"), "YYYY") & " " & ThisWorkbook.Sheets("Model").Range("B4") & " " & Format(Date, "YYYY-mm-dd") & ".docx"

'Delete original Word document
    Kill strFile

End Sub

我已经为此研究了几个小时并尝试了多种解决方案,包括注释掉所有复制数据块以尝试将错误归零。但没有运气。我希望我已经正确地发布了这个请求。预先感谢您的任何帮助。

【问题讨论】:

  • 这很简单:如果您在例程结束时终止该文件,您将在第二次运行时收到错误消息,因为该文件不再存在。

标签: excel vba


【解决方案1】:

这是你正在尝试的吗(未经测试)?我已经对代码进行了评论,但如果您遇到任何问题,只需询问即可。你所拥有的是早期绑定.我用过了后期绑定这样您就不需要添加任何对 MS Word 应用程序的引用。

Option Explicit

Private Const wdFormatXMLDocument As Integer = 12

Sub ExcelToWord()
    Dim oWordApp As Object, oWordDoc As Object
    Dim FlName As String
    Dim FilePath As String
    Dim NewFileName As String
    Dim IOpenedWord As Boolean
        
    '~~> This is the original word file
    FlName = "G:HOMEWord File.docx"
    
    '~~> Establish an Word application object if open
    On Error Resume Next
    Set oWordApp = GetObject(, "Word.Application")
    '~~> If not open then create a new word application instance 
    If Err.Number <> 0 Then
        Set oWordApp = CreateObject("Word.Application")
        '~~> I created Word instance so I have to close it as well
        IOpenedWord = True
    End If
    Err.Clear
    On Error GoTo 0
        
    oWordApp.Visible = True
        
    Set oWordDoc = oWordApp.Documents.Open(FlName)

    With oWordDoc
        '~~> File path
        FilePath = .Path & ""
        
        '~~> New File name
        NewFileName = FilePath & _
                      Format(ThisWorkbook.Sheets("Model").Range("B14").Value, "YYYY") & _
                      " " & _
                      ThisWorkbook.Sheets("Model").Range("B4").Value & _
                      " " & _
                      Format(Date, "YYYY-mm-dd") & ".docx"
                      
        '~~> Copy data from Excel to Word
        .ContentControls(1).Range.Text = Sheets("Model").Cells(4, 2).Value2
        .ContentControls(2).Range.Text = Format(Date, "mm/dd/yyyy")
        .ContentControls(3).Range.Text = Sheets("Model").Range("X4").Value2
        
        '~~> Save the word document
        .SaveAs Filename:=NewFileName, FileFormat:=wdFormatXMLDocument
        
        DoEvents
        
        '~~> Close the file
        .Close (False)
    End With
    
    '~~> If I opened word then quit word
    If IOpenedWord = True Then oWordApp.Quit
    
    '~~> Delete original Word document
    Kill FlName
End Sub

【讨论】:

    猜你喜欢
    • 2015-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-08
    相关资源
    最近更新 更多