MS Word 文档中书签的超链接

发布于 2024-10-31 16:15:55 字数 294 浏览 2 评论 0原文

是否可以从 WPF 文本块链接到 Word 文档中的书签?

到目前为止,我已经:

<TextBlock TextWrapping="Wrap" FontFamily="Courier New">
    <Hyperlink NavigateUri="..\\..\\..\\MyDoc.doc"> My Word Document </Hyperlink>
</TextBlock>

我假设相对路径来自 exe 位置。我根本无法打开该文档。

Is it possible to link to a bookmark within a word document from a WPF text block?

So far I have:

<TextBlock TextWrapping="Wrap" FontFamily="Courier New">
    <Hyperlink NavigateUri="..\\..\\..\\MyDoc.doc"> My Word Document </Hyperlink>
</TextBlock>

I am assuming the relative path is from the exe location. I can't get the document to open at all.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我早已燃尽 2024-11-07 16:15:55

作为我之前答案的补充,有一种编程方式可以打开本地 Word 文件、搜索书签并将光标放置在那里。我改编自这个优秀的答案。如果您有这种设计:

<TextBlock>           
    <Hyperlink NavigateUri="..\\..\\MyDoc.doc#BookmarkName"
               RequestNavigate="Hyperlink_RequestNavigate">
        Open the Word file
    </Hyperlink>            
</TextBlock>

请使用以下代码:

//Be sure to add this reference:
//Project>Add Reference>.NET tab>Microsoft.Office.Interop.Word

private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e) {
    // split the given URI on the hash sign
    string[] arguments = e.Uri.AbsoluteUri.Split('#');

    //open Word App
    Microsoft.Office.Interop.Word.Application msWord = new Microsoft.Office.Interop.Word.Application();

    //make it visible or it'll stay running in the background
    msWord.Visible = true;

    //open the document 
    Microsoft.Office.Interop.Word.Document wordDoc = msWord.Documents.Open(arguments[0]);

    //find the bookmark
    string bookmarkName = arguments[1];

    if (wordDoc.Bookmarks.Exists(bookmarkName))
    {
        Microsoft.Office.Interop.Word.Bookmark bk = wordDoc.Bookmarks[bookmarkName];

        //set the document's range to immediately after the bookmark.
        Microsoft.Office.Interop.Word.Range rng = wordDoc.Range(bk.Range.End, bk.Range.End);

        // place the cursor there
        rng.Select();
    }
    e.Handled = true;
}

As an addition to my previous answer, there is a programmatic way of opening a local Word file, searching for a bookmark and placing the cursor there. I adapted it from this excellent answer. If you have this design:

<TextBlock>           
    <Hyperlink NavigateUri="..\\..\\MyDoc.doc#BookmarkName"
               RequestNavigate="Hyperlink_RequestNavigate">
        Open the Word file
    </Hyperlink>            
</TextBlock>

use this code:

//Be sure to add this reference:
//Project>Add Reference>.NET tab>Microsoft.Office.Interop.Word

private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e) {
    // split the given URI on the hash sign
    string[] arguments = e.Uri.AbsoluteUri.Split('#');

    //open Word App
    Microsoft.Office.Interop.Word.Application msWord = new Microsoft.Office.Interop.Word.Application();

    //make it visible or it'll stay running in the background
    msWord.Visible = true;

    //open the document 
    Microsoft.Office.Interop.Word.Document wordDoc = msWord.Documents.Open(arguments[0]);

    //find the bookmark
    string bookmarkName = arguments[1];

    if (wordDoc.Bookmarks.Exists(bookmarkName))
    {
        Microsoft.Office.Interop.Word.Bookmark bk = wordDoc.Bookmarks[bookmarkName];

        //set the document's range to immediately after the bookmark.
        Microsoft.Office.Interop.Word.Range rng = wordDoc.Range(bk.Range.End, bk.Range.End);

        // place the cursor there
        rng.Select();
    }
    e.Handled = true;
}
ゝ杯具 2024-11-07 16:15:55

在 WPF 应用程序中而不是在网页上使用超链接需要您自己处理 RequestNavigate 事件。

此处有一个很好的示例。

Using Hyperlink in a WPF application rather than on a web page requires you to handle the RequestNavigate event yourself.

There is a nice example here.

奈何桥上唱咆哮 2024-11-07 16:15:55

根据官方文档,它应该非常简单:

<TextBlock>           
<Hyperlink NavigateUri="..\\..\\MyDoc.doc#BookmarkName"
    RequestNavigate=”Hyperlink_RequestNavigate”>
    Open the Word file
</Hyperlink>            
</TextBlock>

private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)
  {
            Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));
            e.Handled = true;
  }

然而,有一个共识很多 非官方页面表明这只适用

  • .doc 文件(没有 Office 2007 .docx 文件),不幸的是,
  • 仅适用于 Office 2003

尝试将其与 .docx 文件一起使用将产生错误。在 Office 2007 及更高版本上将其与 .doc 文件一起使用将打开文档,但在第一页上。

您可以使用 AutoOpen 宏来解决 Office 2007 及更高版本的限制,请参阅此处了解如何将宏参数传递给 Word。这将需要更改该系统使用的所有文档(并提出有关宏使用的其他问题)。

According to the official documentation, it should be surprisingly simple:

<TextBlock>           
<Hyperlink NavigateUri="..\\..\\MyDoc.doc#BookmarkName"
    RequestNavigate=”Hyperlink_RequestNavigate”>
    Open the Word file
</Hyperlink>            
</TextBlock>

private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)
  {
            Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));
            e.Handled = true;
  }

However, there is a consensus on a lot of inofficial pages that this only works

  • with .doc files (no Office 2007 .docx files), and unfortunately
  • only with Office 2003

Trying to use this with .docx files will yield an error. Using this with .doc files on Office 2007 and above will open the document, but on the first page.

You may be able to work around the limitations of Office 2007 and above by using AutoOpen macros, see here for how to pass a Macro argument to Word. That would require changing all the documents to be used with that system (and raise additional questions about the use of macros).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文