如何解决超链接到MS Word文档中的书签
| 是否可以从WPF文本块链接到Word文档中的书签? 到目前为止,我有:<TextBlock textwrapping=\"Wrap\" FontFamily=\"Courier New\">
<Hyperlink NavigateUri=\"..\\\\..\\\\..\\\\MyDoc.doc\"> My Word Document </Hyperlink>
</TextBlock>
我假设相对路径来自exe位置。我根本无法打开该文档。
解决方法
除了我以前的回答外,还有一种编程的方式来打开本地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;
}
,在WPF应用程序而不是网页中使用超链接需要您自己处理RequestNavigate事件。
这里有一个很好的例子。
,根据官方文档,它应该非常简单:
<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;
}
但是,在许多非正式页面上都达成共识,认为这仅适用于
带有ѭ4(文件(没有Office 2007.docx
文件),并且不幸的是
仅在Office 2003中
尝试对.docx
个文件使用此方法会产生错误。将其与Office 2007及更高版本上的“ 4”个文件一起使用将打开文档,但位于第一页。
您可以通过使用AutoOpen宏来解决Office 2007及更高版本的限制,有关如何将Macro参数传递给Word的信息,请参见此处。这将需要更改要与该系统一起使用的所有文档(并引发有关使用宏的其他问题)。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。