【问题标题】:Copy multiple word documents into one new word document将多个word文档复制到一个新的word文档中
【发布时间】:2020-08-24 09:47:04
【问题描述】:

我是 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

 

【问题讨论】:

  • 恕我直言,("B3:B40")) 后面的括号不平衡。那么,为什么要使用“范围”?文件的值应该在单个单元格 (B3) 中而不是范围内(即使存在合并的单元格:选择该单元格并查看左上角的名称)。

标签: excel vba copy-paste word


【解决方案1】:

我认为这对你有用。缺少的是为每个文件(范围内的单元格)工作的循环。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多