Loop through Hyperlinks within document

Hi,
I’m trying to loop through all hyperlinks within a document. Within the loop i am looking to change the address, etc, of some of the hyperlinks.
How would i go about this please ?
Thanks in advance
Mark

Hi
Thanks for your inquiry. You can try looping through HIPERLINK fields in the document. Please see the following thread.
https://forum.aspose.com/t/111741
Best regards.

Hi,
Many thanks…
Do you have the code in vb ??
Mark

Hi,
What i am trying to achieve is to loop through the hyperlinks (you code supplied helps with that), but i want to replace the hyperlink address with the hyperlink screen-tip where the screen-tip is not “about:blank”…
How would i go about this ?
Thanks In advance
Mark

Hi
Thanks for your inquiry. Here is code in VB.NET.

Dim NewUrl As String = "http://www.aspose.com"
Dim NewName As String = "Aspose - The .NET & Java Component Publisher"
Dim NewScreenTip As String = "Aspose corporporation"
'Open document
Dim doc As Document = New Document("in.doc")
'Get field starts
Dim starts As NodeCollection = doc.GetChildNodes(NodeType.FieldStart, True)
Dim start As FieldStart
For Each start In starts
'If field is HyperLink
If (start.FieldType = FieldType.FieldHyperlink) Then
'Create new field code
Dim fieldCode As String = String.Format(" HYPERLINK ""{0}"" \o ""{1}"" ", NewUrl, NewScreenTip)
Dim fieldCodeRun As Run = CType(start.NextSibling.Clone(True), Run)
fieldCodeRun.Text = fieldCode
'Remove old field code
While (Not start.NextSibling.NodeType = NodeType.FieldSeparator)
start.NextSibling.Remove()
End While
Dim separator As FieldSeparator = CType(start.NextSibling, FieldSeparator)
'Create new field value
Dim fieldValueRun As Run = CType(separator.NextSibling.Clone(True), Run)
fieldValueRun.Text = NewName
'Remove old field value
While (Not separator.NextSibling.NodeType = NodeType.FieldEnd)
separator.NextSibling.Remove()
End While
'Insert new field code
start.ParentNode.InsertAfter(fieldCodeRun, start)
'Insert new field value
separator.ParentNode.InsertAfter(fieldValueRun, separator)
End If
Next
'Save document
doc.Save("out.doc")

Hope this helps.
Also see the following link.
https://forum.aspose.com/t/111741
Best regards.

Hi,
Great, thanks very much !!
1 more quicky…!.. when using this code to loop through hyperlinks, how would i change the forecolor and underline ? I want to change it to black and no-underline when there is no hyperlink …?
Reason: The hyperlink is being merged from a seperate template, so the formatting is always blue and underlined, even when the address is about:blank.
Mark

Hi
Thanks for inquiry. You can change formation of field value. For example see the following code snippet.

'Create new field value
Dim fieldValueRun As Run = CType(separator.NextSibling.Clone(True), Run)
fieldValueRun.Text = NewName
fieldValueRun.Font.Underline = Underline.None
fieldValueRun.Font.Color = Color.Red

Hope this helps.
Best regards.

Hi Alexey,
Cant believe i missed that !! lol
Thanks for your help
Mark

Hi,
When looping through the hyperlinks, how would i check the hyperlink address BEFORE i decide to change it?
eg.

If UCase(fieldCodeRun.Text) = "ABOUT:BLANK" Then
Dim fieldCode As String = String.Format(" HYPERLINK ""{0}"" \o ""{1}"" ", NewUrl, NewScreenTip)
fieldCodeRun.Text = fieldCode
End If

Where about:blank is the hyperlink address…
Thanks
Mark

Hi
Thanks fro your inquiry. You can try using the following code snippet.

If UCase(fieldCodeRun.Text).Contains("ABOUT:BLANK") Then
'Replace hyperlink
End If

Also see the following link.
https://docs.aspose.com/words/net/working-with-hyperlinks/
Best regards.

Hi,
The problem i am having is that the hyperlink address is a merge field.
So, the fieldcoderun.text = " hyperlink " and the address is not being shown ???
Mark

Hi
This occurs because Field code contains several Runs. You can try using the following code to solve this.

Dim oldFieldCode As String = String.Empty
While (Not start.NextSibling.NodeType = NodeType.FieldSeparator)
If start.NextSibling.NodeType = NodeType.Run Then
oldFieldCode = oldFieldCode + CType(start.NextSibling, Run).Text
End If
start.NextSibling.Remove()
End While
If UCase(oldFieldCode).Contains("ABOUT:BLANK") Then
'Replace hyperlink
End If

I hope this could help you.
Best regards.

ahh… i understand now…
just tried it, and it works !!! awsome !
Many Thanks for your help Alexey
Mark

A post was split to a new topic: Loop through all Hyperlinks within document