I'm currently trying to add a hyperlink to a web url in word through a VB program. I'm stumbling around to try and find the proper syntax and what I need to accomplish this, because I've been getting a lot of unhelpful VBA examples, which is not what I need at all.
My code looks like this:
sPara2 = oDoc.Content.Paragraphs.Add
sPara2.Range.Text = attachmentRdr("attachmentName")
sPara2.Range.Hyperlinks.Add(attachmentRdr("attachmentPath"))
sPara2.Format.SpaceAfter = 24 '24 pt spacing after paragraph.
sPara2.Range.InsertParagraphAfter()
where attachmentRdr
is a sqlDatareader
reading strings of text (attachment name, and path) from a database. If I run this, I get an error for bad parameters (which gets' proced off of the hyperlinks.add()
).
Pass range through as the first parameter to the Add function, followed by your URL:
Dim range As Microsoft.Office.Interop.Word.Range
range = Me.Application.Selection.Range
range.Hyperlinks.Add(range, "http://www.microsoft.com")
来源:https://stackoverflow.com/questions/14389464/adding-a-hyperlink-in-word-with-vb-net