none
Retrieve link to external objects in a word document RRS feed

  • Question

  • Hello Experts,

    Is it possible to retrieve the links as depicted in the image in a word document


    Regards,

    Shola

    Tuesday, September 5, 2017 9:21 AM

Answers

  • Hello Deepak,

    I was able to work out the solution using Open xml sdk using the code below

     string filepath = @"C:\Users\xxxx\Desktop\destination1.docx";
    
                using (WordprocessingDocument doc =WordprocessingDocument.Open(filepath, true))
    
                {
    
                    string hyperlinkRelationshipId;
                    foreach(ExternalRelationship hdrFtr in doc.MainDocumentPart.ExternalRelationships)
                    {
                        hyperlinkRelationshipId = hdrFtr.Id;
                       
                        var relUri = doc.MainDocumentPart.GetExternalRelationship(hyperlinkRelationshipId).Uri.AbsoluteUri;
                        Console.WriteLine(relUri);
                        Console.ReadLine();
                    }
    
                    
    Regards


    Thursday, September 7, 2017 8:43 AM

All replies

  • Hi ghostme,

    I can see that you had posted the issue in Open XML Forum.

    so it looks like you want solution using Open XML.

    I try to find the solution using Open XML but did not get anything useful in Open XML to do  the same.

    then I try to find solution using VBA.

    with the code below you can get list of linked documents.

    Sub demo2()
        Dim msWord As Object
        Dim msDoc As Object
        Dim msHlink As Object
    
        Set msWord = GetObject(, "Word.Application")
        Set msDoc = ActiveDocument
      
    
        For Each msHlink In msDoc.Hyperlinks
            Debug.Print msHlink.Address
        Next
    End Sub

    Output:

    with the code below you can get the list of linked images in word document.

    Sub demo()
    
    Dim oShape As InlineShape
    On Error Resume Next
        For Each oShape In ActiveDocument.InlineShapes
                oShape.Select
                Debug.Print oShape.LinkFormat.SourceFullName
                oShape.Select
             
        Next
    
    End Sub
    

    Output:

    if you want the solution using VB or C# then also you can easily convert above code to desired language.

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, September 6, 2017 3:25 AM
    Moderator
  • Hello Deepak,

    Thanks for this...so if I convert the code to C#, would I need to still reference the openxml dll to get this to work?

    Wednesday, September 6, 2017 7:34 AM
  • Hi ghostme,

    if you convert this code in C# then you not need to reference Open XML.

    but you need to add reference to Microsoft Office Interop Word.

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, September 6, 2017 7:39 AM
    Moderator
  • Hello Deepak,

    I was able to work out the solution using Open xml sdk using the code below

     string filepath = @"C:\Users\xxxx\Desktop\destination1.docx";
    
                using (WordprocessingDocument doc =WordprocessingDocument.Open(filepath, true))
    
                {
    
                    string hyperlinkRelationshipId;
                    foreach(ExternalRelationship hdrFtr in doc.MainDocumentPart.ExternalRelationships)
                    {
                        hyperlinkRelationshipId = hdrFtr.Id;
                       
                        var relUri = doc.MainDocumentPart.GetExternalRelationship(hyperlinkRelationshipId).Uri.AbsoluteUri;
                        Console.WriteLine(relUri);
                        Console.ReadLine();
                    }
    
                    
    Regards


    Thursday, September 7, 2017 8:43 AM
  • Hi ghostme,

    I can see that you had found the solution for your issue.

    this thread is still open.

    I suggest you to mark your last post as an answer.

    so that we can close this thread.

    it will also help other community member in future who have same issue like yours.

    until you mark the answer , this thread will remain open.

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, September 7, 2017 9:35 AM
    Moderator