[英]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.