How to locate hyperlink position in document

Posted by Million under C# on 4/14/2015 | Points: 10 | Views : 515 | Status : [Member] | Replies : 1
How can the insertion point be relocated in a Word document to where it finds a hyperlink? E.g. I would like to add some text after each hyperlink in a document. So I traverse the hyperlinks using

For each hLink in Selection.Hyperlinks

But when I try to reference hLink.Range it says that property is not defined.

Any ideas?




Responses

Posted by: Yesi on: 4/14/2015 [Member] Starter | Points: 25

Up
0
Down
Not a hard thing, refer this article http://www.e-iceblue.com/Tutorials/Spire.Doc/Spire.Doc-Program-Guide/Hyperlink/NET-Finding-Hyperlinks-in-a-word-document.html . Download this DOC library http://www.e-iceblue.com/Introduce/word-for-net-introduce.html then add reference to your project, then use the following code:

static void Main(string[] args)
{
Document doc = new Document();
doc.LoadFromFile("Spire.docx");
List hyperlinks = new List();
foreach (Section section in doc.Sections)
{
foreach (DocumentObject sec in section.Body.ChildObjects)
{
if (sec.DocumentObjectType == DocumentObjectType.Paragraph)
{
foreach (DocumentObject para in (sec as Paragraph).ChildObjects)
{
if (para.DocumentObjectType == DocumentObjectType.Field)
{
Field field = para as Field;

if (field.Type == FieldType.FieldHyperlink)
{
hyperlinks.Add(field);
}
}
}
}
}
}


Million, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response