【问题标题】:Combine word documents in hyperlink list在超链接列表中合并word文档
【发布时间】:2020-07-28 10:10:29
【问题描述】:

我是 VBA 领域的新手。我会为我的问题寻求帮助。

我有 B3:40 范围内的文件列表。在每个单元格中,超链接到公共服务器中的特定 Word 文档 (.docx) 文件。

现在,我想将该范围内的超链接列表中的所有文档组合成一个新的word文档,其中包含使用VBA excel列表中的所有文档。

下面是我的代码。我只是可以打开它,但不知道如何组合它。

提前感谢您的帮助。

Dim xhyperlink as Hyperlink
Dim Openfile as Range
On Error Resume Next

Set Openfile = Thisworkbook.Sheets("name of sheet")
Set Openfile = Range ("B3:B40")

For Each xhyperlink In Openfile.Hyperlinks
xhyperlink.Follow
Next

【问题讨论】:

    标签: excel vba merge hyperlink word


    【解决方案1】:

    为了在未来获得更快的响应,请询问有关您的代码的具体问题,“这可能吗?”不是一个好问题。如果您不确定如何在 Word/Excel 中编写代码,请先尝试使用宏记录器。然后发布您在代码中遇到的具体问题。话虽如此,欢迎来到 Stack Overflow。这是一个新的帐户免费赠品答案...

    通过工具添加Microsoft Word 14.0 Object Library引用 --> 引用。

    Sub MergeDocs()
        Dim oLink As Hyperlink
        Dim oLinkRange As Range
        
        ' Set Range of Hyperlinks
        Set oLinkRange = Sheet1.Range("A1:A10")
        
        'Setup "Merge" Document
        Dim oWord As New Word.Application
        Dim oMergeDoc As Word.Document
        Set oMergeDoc = oWord.Documents.Add
        
        'Insert Files into Master
        For Each oLink In oLinkRange.Hyperlinks
            oWord.Selection.InsertFile (oLink.Address)
        Next
        
        'Save Merged Document & Close
        oMergeDoc.SaveAs2 ("C:\Temp\MergeTest\Merged.docx")
        oMergeDoc.Close
        oWord.Quit
    End Sub
    

    注意:您需要在其中添加一些错误检查。

    【讨论】:

    • 感谢您的帮助和建议@JosephC,下次我会做更好的问题。对于您的代码,我已经尝试过了,发现有关文件地址的错误。它说找不到文件(运行时错误5174)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-17
    • 2010-11-08
    • 2013-10-02
    相关资源
    最近更新 更多