简体   繁体   中英

How to get hyperlinks placed inside of shapes in .docx file using office interop word

I'm looking for a way to programmatically replace hyperlinks from inside of word documents. I already have code which handles hyperlinks replacement as long as those're placed along document's main text. Unfortunately this code doesn't see hyperlinks if those're placed inside of shapes. The documents I deal with quite often contains diagrams (flow-charts) composed of shapes (often grouped shapes) and I can't find any way to retrieve and modify hyperlinks from inside of the shapes.

The code that I've so far allows me to retrieve all hyperlinks from documents text:

Microsoft.Office.Interop.Word.Application applicationObject = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document aDDoc = applicationObject.Documents.Open(FileName: @"c:\Temp\a.docx");
Microsoft.Office.Interop.Word.Hyperlinks links = aDDoc.Hyperlinks;

foreach (var hyperlink in links)
        {
            string adress = ((Microsoft.Office.Interop.Word.Hyperlink) hyperlink).Address;
        }

Unfortunately all the attempts to retrieve hyperlinks from shape object cause an exception:

Microsoft.Office.Interop.Word.Shapes shapes = aDDoc.Shapes;


        foreach (var shape in shapes)
        {

                Microsoft.Office.Interop.Word.Hyperlink hyperlink = ((Microsoft.Office.Interop.Word.Shape)shape).Hyperlink;

                string adress = ((Microsoft.Office.Interop.Word.Hyperlink)hyperlink).Address;
        }

This throws an exception on line where address variable is assigned. I'm working in non-English version of visual studio so the exception I get is also not in English but it says something like: operation could not be performed

Anyone have an idea how to retrieve hyperlinks from inside of shapes?

I did a small example which retrieves hyperLinks from shapes within a word file. I am working with Word 2010. You cannot access a text hyperlink from within a shape by Shape.Hyperlink as this will get you

Returns a Hyperlink object that represents the hyperlink associated with the specified shape.

see MSDN - Shape documentation.

Instead of this you have to access the Shape - TextFrame - TextRange property. This will allow you access to hyperlinks in the text of any shape.

Source Code

/// entry point - will look for hyperlinks in text
/// and afterwards in shapes within this document
private static void replaceHyperlinksInShapes()
{
    Word.Application app = new Word.Application();
    Word.Document doc = app.Documents.Open(FileName: @"e:\Temp\demoFile.docx");
    Word.Hyperlinks links = doc.Hyperlinks;

    Debug.WriteLine("Text Hyperlinks");
    foreach (var hyperlink in links)
    {
        string address = ((Microsoft.Office.Interop.Word.Hyperlink)hyperlink).Address;
        Debug.WriteLine("\t" + address);
    }

    Debug.WriteLine("Shape Hyperlinks");
    foreach (Word.Shape shape in doc.Shapes)
    {
        searchForHyperLink(shape);
    }
}

/// will search for hyperlinks in given shape
/// if this shape is a group, call recursivly
private static void searchForHyperLink(Word.Shape shape)
{
    /// check if this shape is a group or not
    /// CRUCIAL!!! There are way more types which may contain hyperlinks
    /// check if necessary
    if (shape.Type == Office.MsoShapeType.msoAutoShape)
    {
        Word.TextFrame frame = shape.TextFrame;
        Word.Range range = frame.TextRange;
        if (range != null)
        {
            foreach (var hyperlink in range.Hyperlinks)
            {
                string address = ((Word.Hyperlink)hyperlink).Address;
                Debug.WriteLine("\t" + address);
            }
        }
    }
    else if (shape.Type == Office.MsoShapeType.msoGroup)
    {
        for (int i = 1; i <= shape.GroupItems.Count; i++)
        {
            Word.Shape childShape = shape.GroupItems[i];
            searchForHyperLink(childShape);
        }
    }
}

Word-File

示例文件,文本中带有超链接,形状中带有一个超链接

Result output

给定单词文件和提供的源的结果

Edit

After searching around in MSDN I tumbled accros Type Property . By using this one I was able to differ between shapes and groups ( a group is a shape too! ).

I hope this will be of any help for you!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM