excel 将多个Word文档复制到一个新的Word文档中

wyyhbhjk  于 2022-11-18  发布在  其他
关注(0)|答案(1)|浏览(308)

我是VBA新手,想寻求一些帮助。
我在Excel中有一个范围B3:B40中的Word文档列表。我想复制列表中的文档并粘贴到新文档中,而不更改页面格式。
我已经试过下面的代码,它给予我“运行时错误13”。有人能帮助这个情况吗?提前感谢任何帮助。

Application.ScreenUpdating=false

set objword = createobject("Word.Application")
set objdoc = objword.Documents.Add
objword.visible = true

set objselection = objword.Selection
Folderpath = "C:\desktop"  'where I save the word document that would be combined

set objtempword = createobject("Word.Application")
set tempdoc = objword.documents.open (Folderpath & "\" & Sheet1.Range ("B3:B40")
set objtempselection = objtempword.selection
tempdoc.range.select
tempdoc.range.copy
objselection.typeparagraph
objselection.paste
tempdoc.close
cdmah0mi

cdmah0mi1#

我想这可能对你有用。缺少的是为每个文件(范围中的单元格)工作的周期。

Option Explicit

Sub JoinDocs()
    Application.ScreenUpdating = False
    Dim objword As Object, objdoc As Object, objselection   As Object
    Set objword = CreateObject("Word.Application")
    Set objdoc = objword.Documents.Add
    objword.Visible = True
    Dim Folderpath  As String
    Set objselection = objword.Selection
    Folderpath = "C:\desktop\"  'where I save the word document that would be combined
    Dim vDoc As Variant
    Dim objtempword As Object, tempdoc As Object, objtempselection As Object
    Set objtempword = CreateObject("Word.Application")
    For Each vDoc In Sheet1.Range("B3:B40").Value
        Set tempdoc = objword.Documents.Open(Folderpath & vDoc)
        Set objtempselection = objtempword.Selection
        tempdoc.Range.Select
        tempdoc.Range.Copy
        objselection.TypeParagraph
        objselection.Paste
        tempdoc.Close
    Next vDoc
End Sub

相关问题