仔細再看了一遍,應該是這一行的問題
ActiveDocument.Content.Delete
你前面加個.應該就可以了
.ActiveDocument.Content.Delete
Sub CopyDataToWord()
Dim wdApp As Word.Application
Dim myRange As Range
Dim i As Long
Set wdApp = New Word.Application
With wdApp
.DisplayAlerts = False
.Documents.Open Filename:=ThisWorkbook.Path & "\123.docx"
.ActiveDocument.Content.Delete
For i = 1 To 1
Set myRange = Worksheets("合併").UsedRange
myRange.Copy
With .Selection
.TypeParagraph
.Paste
End With
Next i
.ActiveDocument.Save
.ActiveDocument.Close
.Quit
End With
Application.CutCopyMode = False
Set wdApp = Nothing
Set myRange = Nothing
Set docRange = Nothing
Set docApp = Nothing
End Sub
※ 引述《rock04rock (CarlosChiang)》之銘言:
: 軟體:Microsoft 365 企業應用版
: 我編寫了一個駐留在 Excel 工作簿中的 VBA 巨集。執行時,它將開啟一個現有的Word文
: 件(與Excel工作簿儲存在同一目錄中),將Excel工作簿中的儲存格中的一些內容複製到
: Word文件中,以新名稱儲存Word文件(在相同的目錄中)目錄)並刪除原始 Word 文件。
: 此過程在第一次運行時按預期工作。但在第二次運行時,我收到運行時誤 462。
: (都卡在ActiveDocument.Content.Delete)。因為我還是新手,想請教怎麼修改會比較好。
: Sub CopyDataToWord()
: Dim wdApp As Word.Application
: Dim myRange As Range
: Dim i As Long
: Set wdApp = New Word.Application
: With wdApp
: .Documents.Open Filename:=ThisWorkbook.Path & "\123.docx"
: ActiveDocument.Content.Delete
: For i = 1 To 1
: Set myRange = Worksheets("合併").UsedRange
: myRange.Copy
: With .Selection
: .TypeParagraph
: .Paste
: End With
: Next i
: .ActiveDocument.Save
: wdApp.Visible = True
: End With
: Application.CutCopyMode = False
: Set wdApp = Nothing
: Set myRange = Nothing
: Set docRange = Nothing
: Set docApp = Nothing
: End Sub
: 第一個提供解決方法成功,會提供500P做感謝。
: 謝謝大家