[英]How to find hyperlinks in a word document?
I'm working on a VB 2015 and I have a problem.我正在开发 VB 2015,但遇到了问题。 I want to find hyperlinks in a word document containing a paragraph with several words containing hyperlinks.
我想在包含一个段落的 word 文档中找到超链接,该段落包含几个包含超链接的单词。 How can I find all the hyperlinks and list them in a text file or textbox?
如何找到所有超链接并将它们列在文本文件或文本框中?
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim wa As Microsoft.Office.Interop.Word.Application
Dim wd As Microsoft.Office.Interop.Word.Document
Dim wp As Microsoft.Office.Interop.Word.Paragraph
wa = CreateObject("Word.Application")
wa.Visible = False
wd = wa.Documents.Add
wp = wd.Content.Paragraphs.Add
wp.Range.Paste()
wd.SaveAs("F:\sample.docx")
Dim colHyperlinks As String = wd.Hyperlinks.ToString
For Each objHyperlink In colHyperlinks
TextBox1.Text = objHyperlink.TextToDisplay
Next
wa.Quit()
End Sub
End Class
As mentioned in the comment's above, you are declaring a string
type on a Word.Hyperlinks
collections object.正如上面评论中提到的,您在
Word.Hyperlinks
集合对象上声明了一个string
类型。 Therefore you would only ever get one string and not any others.因此,您只会得到一个字符串,而不会得到任何其他字符串。 Please see code below, comment's on what it does...
请参阅下面的代码,评论它的作用......
Note: Code tried and tested注意:代码经过测试
'returns a collection of links, not a string
Dim colHyperlinks As Word.Hyperlinks = wd.Hyperlinks()
This code below is just a LINQ
statement to get all hyperlinks into a List(Of String)
.下面的代码只是一个
LINQ
语句,用于将所有超链接放入List(Of String)
。 You can loop through if you want and then add them to the Textbox
if you wish...如果需要,您可以循环遍历,然后将它们添加到
Textbox
如果需要)...
'get all the hyperlinks
Dim arr As List(Of String) = (From hl As Word.Hyperlink In colHyperlinks Select hl.TextToDisplay).ToList()
'show the url's
TextBox1.Text = String.Join(Environment.NewLine, arr)
Or in just one line if you wish...或者如果您愿意,只需一行...
TextBox1.Text = String.Join(Environment.NewLine, (From hl As Word.Hyperlink In colHyperlinks Select hl.TextToDisplay).ToList())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.