简体   繁体   English

VBA excel - 数据文件从表格到带有超链接的word文档

[英]VBA excel - Data File from sheet to word document with hyperlink

I'm trying to create a Word file that contains excel data with hyperlink of file path data, like this:我正在尝试创建一个包含 excel 数据的 Word 文件,其中包含文件路径数据的超链接,如下所示:

在此处输入图像描述

在此处输入图像描述

But I want the result like this:但我想要这样的结果:

在此处输入图像描述

Here is the code I use:这是我使用的代码:

Sub InsertHyperLink()
    Dim aWord As Object
    Dim wDoc As Object
    Dim i&, EndR&
    Dim Rng As Range

    Set aWord = CreateObject("Word.Application")
    Set wDoc = aWord.Documents.Add
    EndR = Range("A65536").End(xlUp).Row
    For i = 2 To EndR
        
        wDoc.Range.InsertAfter Cells(i, 1) & vbCrLf
        wDoc.Range.InsertAfter vbCrLf
        wDoc.Paragraphs(wDoc.Paragraphs.Count).Range.Hyperlinks.Add _
          Anchor:=wDoc.Paragraphs(wDoc.Paragraphs.Count).Range, _
          Address:=Cells(i, 3), _
          TextToDisplay:=Left(Cells(i, 2), InStr(1, Cells(i, 2), ".") - 1)
        wDoc.Range.InsertAfter vbCrLf
        wDoc.Range.InsertAfter vbCrLf
    Next
        aWord.Visible = True

    aWord.Activate   'Xem ket qua
    Set wDoc = Nothing
    Set aWord = Nothing
End Sub

Please help.请帮忙。 Thanks for reading谢谢阅读

Seems like you just want to not repeat the folders, so test for that好像你只是不想重复文件夹,所以测试一下

replace代替

wDoc.Range.InsertAfter Cells(i, 1) & vbCrLf

with

If Cells(i, 1) <> Cells(i - 1, 1) then
    wDoc.Range.InsertAfter Cells(i, 1) & vbCrLf
End If

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 VBA 将 Excel 文件中的超链接添加到 Word 文档 - Adding a Hyperlink from an Excel file to a Word document using VBA 将Excel文件中的文本插入Word文档(Word VBA) - Insert text from an excel file into a word document (Word VBA) 使用vba使用从Excel工作表导入的数据自动执行Microsoft Word文档,并使用变量过滤要实际插入的数据 - Using vba to automate a Microsoft Word document with data imported from an Excel sheet, using a variable to filter what data to actually insert 如何从 Word VBA 到 excel 表中获取记录集数据? - How to get the recordset data from Word VBA to excel sheet? 如何将excel中单个单元格的超链接复制到由VBA aleady创建和填充的word文档? - How to copy a hyperlink from a single cell in excel to a word document which is being created and populated by VBA aleady? 使用Excel VBA从Word文档复制数据 - Copying Data From Word Document Using Excel VBA 从 Word 文档表填充 Excel 文件 - VBA - Populate Excel File from Word Document Tables - VBA 如何将一张Excel工作表和Word文档保存成单个pdf和VBA - How to save an Excel sheet and Word document into single pdf with VBA 从单元格 VBA excel 获取显式工作表超链接 - Get explicit sheet hyperlink from cell VBA excel 使用VBA在Word文档中填充Excel数据 - Populate excel data in word document with VBA
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM