开发者

Hyperlink to bookmark in a MS Word document

开发者 https://www.devze.com 2023-02-25 18:26 出处:网络
Is it possible to link to 开发者_StackOverflow中文版a bookmark within a word document from a WPF text block?

Is it possible to link to 开发者_StackOverflow中文版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.


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


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.


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消