2011-04-13 51 views
8

是否可以鏈接到WPF文本塊的Word文檔中的書籤?超鏈接到MS Word文檔中的書籤

到目前爲止,我有:

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

我假設的相對路徑是exe文件的位置。我無法讓文檔打開。

回答

5

作爲我以前的答案的補充,有一種編程方式打開本地Word文件,搜索書籤並將光標放置在那裏。我改編自this excellent answer。如果你有這樣的設計:

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

在WPF應用程序而不是網頁中使用超鏈接需要您自己處理RequestNavigate事件。

有一個很好的例子here

+0

有處理的超級鏈接本身,但似乎並沒有處理書籤的例子。 – 2013-04-30 23:50:26

3

按照official documentation,它應該是出奇的簡單:

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

然而,在a lotof inofficial頁面,這隻能

  • .doc文件(沒有Office 2007 .docx共識文件),不幸的是
  • 只與Office 2003

試圖將此與.docx文件一起使用會產生錯誤。在Office 2007及更高版本上使用.doc文件將打開該文檔,但會打開第一頁。

通過使用AutoOpen宏,您可能能夠解決Office 2007及更高版本的侷限性,請參閱此處瞭解如何將Word。這將需要更改與該系統一起使用的所有文檔(並提出有關使用宏的其他問題)。

+0

贊成票,你有一個美好的繼續和權力活動的堆棧。 – 2013-05-07 12:31:09