簡體   English   中英

Excel 到 Word 宏導致運行時錯誤 462

[英]Excel to Word Macro resulting in Run-time error 462

我寫了一個 VBA 宏,它位於 Excel 工作簿中。 運行時會打開一個已有的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

我已經為此研究了幾個小時並嘗試了多種解決方案,包括注釋掉所有復制數據塊以嘗試將錯誤歸零。 但沒有運氣。 我希望我已經正確地發布了這個請求。 預先感謝您的任何幫助。

這是你正在嘗試的嗎? 我已經對代碼進行了評論,但如果您遇到任何問題,只需詢問即可。 你擁有的是Early Binding 我使用了后期綁定,這樣您就不需要添加任何對 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
        
    '~~> This is the original word file. Change as applicable
    FlName = "G:\HOME\Word File.docx"
    
    '~~> Check if word file exists
    If Dir(FlName) = "" Then
        MsgBox "Word File Not Found"
        Exit Sub
    End If
    
    '~~> 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")
    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
    End With
    
    '~~> Delete original Word document
    Kill FlName
End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM