仔细再看了一遍,应该是这一行的问题
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做感谢。
: 谢谢大家