如何使用office互操作单词在.docx文件中放置形状内的超链接

时间:2015-02-18 20:02:10

标签: c# .net hyperlink ms-word office-interop

我正在寻找一种以编程方式替换word文档内部的超链接的方法。我已经有代码处理超链接替换,只要它们放在文档的主文本中。不幸的是,如果这些代码放在形状内部,则此代码不会看到超链接。我处理的文档经常包含由形状(通常是分组的形状)组成的图表(流程图),我找不到任何方法从形状内部检索和修改超链接。

我到目前为止的代码允许我从文档文本中检索所有超链接:

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;
        }

不幸的是,从shape对象检索超链接的所有尝试都会导致异常:

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;
        }

这会在分配了地址变量的行上引发异常。我正在使用非英语版的visual studio,所以我得到的例外也不是英文版,但它说的是:操作无法执行

任何人都知道如何从形状内部检索超链接?

1 个答案:

答案 0 :(得分:0)

我做了一个小例子,它从word文件中的形状中检索超链接。我正在使用Word 2010.您无法通过Shape.Hyperlink访问形状内的文本超链接,因为这样可以获得

  

返回表示相关联的超链接的超链接对象   具有指定的形状。

请参阅MSDN - Shape文档。

而不是这个,您必须访问 Shape - TextFrame - TextRange 属性。这将允许您访问任何形状的文本中的超链接。

源代码

/// 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);
        }
    }
}

<强>字文件

sample file with a hyperlink in text and one in a shape

结果输出

result for the given word file and provided source

修改

在MSDN中搜索后,我摔倒了accros Type Property。通过使用这个,我能够在形状和组之间有所不同(一个组也是一个形状!)。

我希望这对你有任何帮助!