繁体   English   中英

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

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

我正在尝试创建一个包含 excel 数据的 Word 文件,其中包含文件路径数据的超链接,如下所示:

在此处输入图像描述

在此处输入图像描述

但我想要这样的结果:

在此处输入图像描述

这是我使用的代码:

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

请帮忙。 谢谢阅读

好像你只是不想重复文件夹,所以测试一下

代替

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

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM