Eam*_*voy 8 c# wpf bookmarks ms-word hyperlink
是否可以从WPF文本块链接到word文档中的书签?
到目前为止,我有:
<TextBlock TextWrapping="Wrap" FontFamily="Courier New">
<Hyperlink NavigateUri="..\\..\\..\\MyDoc.doc"> My Word Document </Hyperlink>
</TextBlock>
Run Code Online (Sandbox Code Playgroud)
我假设相对路径来自exe位置.我根本无法打开文件.
作为我之前回答的补充,有一种编程方式可以打开本地Word文件,搜索书签并将光标放在那里.我从这个优秀的答案改编而来.如果你有这个设计:
<TextBlock>
<Hyperlink NavigateUri="..\\..\\MyDoc.doc#BookmarkName"
RequestNavigate="Hyperlink_RequestNavigate">
Open the Word file
</Hyperlink>
</TextBlock>
Run Code Online (Sandbox Code Playgroud)
使用此代码:
//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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2804 次 |
| 最近记录: |