Free Support Forum - aspose.com

Managing Hyperlinks

I am looking for a way to manage hyperlinks in a document. In your documentation I found the following example : http://www.aspose.com/documentation/file-format-components/aspose.words-for-.net-and-java/aspose.words.fields.fieldstart.html

However, I am unable to follow this example because the class Hyperlink can not be found. Am I missing something? (I tried version 4.1.1 and 5.0.2)

Kind regards,
Mariëlla

Hi

Thanks for your inquiry. You can do the same using the following code.

string NewUrl = @"http://www.aspose.com";

string NewName = "Aspose - The .NET & Java Component Publisher";

//Open document

Document doc = new Document(@"Test034\in.doc");

//Get field starts

NodeCollection starts = doc.GetChildNodes(NodeType.FieldStart, true);

foreach (FieldStart start in starts)

{

//If field is HyperLink

if (start.FieldType == FieldType.FieldHyperlink)

{

//Create new field code

string fieldCode = string.Format(" HYPERLINK \"{0}\" ", NewUrl);

Run fieldCodeRun = (Run)start.NextSibling.Clone(true);

fieldCodeRun.Text = fieldCode;

//Remove old field code

while (start.NextSibling.NodeType != NodeType.FieldSeparator)

{

start.NextSibling.Remove();

}

FieldSeparator separator = (FieldSeparator)start.NextSibling;

//Create new field value

Run fieldValueRun = (Run)separator.NextSibling.Clone(true);

fieldValueRun.Text = NewName;

//Remove old field value

while (separator.NextSibling.NodeType != NodeType.FieldEnd)

{

separator.NextSibling.Remove();

}

//Insert new field code

start.ParentNode.InsertAfter(fieldCodeRun, start);

//Insert new field value

separator.ParentNode.InsertAfter(fieldValueRun, separator);

}

}

//Save document

doc.Save(@"Test034\out.doc");

I hope this could help you.

Best regards.

Thank you for your response! What I was trying to do (and stil am :slight_smile: ) is merging hyperlink fields. In Word this code is shown as { HYPERLINK “{MERGEFIELD “Fieldname”}” }. The merge of the merge field will be fine. However, the hyperlink will only be displayed when in Word all fields are refreshed.

Since we also create documents which are not to be opened in Word, it is not possible to do that for all documents we create. I was hoping to find a way to update merged hyperlink fields using Aspose components so they are displayed correctly when opened from Word.

When I saw the example using a Hyperlink class I hoped to be able to update it that way. Using your example I am not really sure if I can do what I want. Do you have any suggestions for me?

Thanks in advance,
Mariëlla

Hi

Thanks for your inquiry. I think that you can use a macro to automatically update the field. See the following link for more information.

http://support.microsoft.com/kb/832897

Also see attached document, you can use this document as template.

Best regards.

Thanks for your response again. As not all documents will be opened in Word (Some will be converted to plain text or HTML using Aspose components), that solution will not cover the entire problem.

Isn’t there a way to update fields from Aspose and/or manually update the display part of a hyperlink field to be the same as the link target part of the hyperlink?

Kind regards,
Mariëlla

Hi

At the moment Aspose.Words can update the results of DOCPROPERTY and DOCVARIABLE fields only. See the following link for more information.

http://www.aspose.com/documentation/file-format-components/aspose.words-for-.net-and-java/aspose.words.range.updatefields.html

Best regards.