如何使用 Microsoft Word API 和书签功能以编程方式将 Word 文档打开到特定位置?
我正在 Windows 窗体应用程序中尝试以下代码。我不确定我做错了什么(而且我很容易做错,因为我对 Word API 没有太多经验),但 GoTo 命令就是找不到书签。我总是在最后一行收到 COMException“此书签不存在”。
但是 wordDoc.Bookmarks.get_Item(ref name)
方法确实找到了书签! 什么给出了?
Object fileName = System.Windows.Forms.Application.StartupPath + "\\Bookmarks.docx";
Object readOnly = false;
Object isVisible = true;
Object missing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document wordDoc = new Microsoft.Office.Interop.Word.Document();
wordApp.Visible = true;
wordDoc = wordApp.Documents.Open(ref fileName, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing);
Object item = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToBookmark;
Object whichitem = Microsoft.Office.Interop.Word.WdGoToDirection.wdGoToFirst;
Object count = 1;
Object name = "Location3";
Bookmark bMark = wordDoc.Bookmarks.get_Item(ref name);
wordDoc.GoTo(ref item, ref whichitem, ref count, ref name);
I'm trying the following code in a Windows Form application. I'm not sure what I'm doing wrong (and I could easily be doing it wrong because I don't have a lot of experience with the Word API) but the GoTo command just cannot find the bookmark. I always get a COMException on the last line, "This bookmark does not exist."
But the wordDoc.Bookmarks.get_Item(ref name)
method does find the bookmark! What gives?
Object fileName = System.Windows.Forms.Application.StartupPath + "\\Bookmarks.docx";
Object readOnly = false;
Object isVisible = true;
Object missing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document wordDoc = new Microsoft.Office.Interop.Word.Document();
wordApp.Visible = true;
wordDoc = wordApp.Documents.Open(ref fileName, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing);
Object item = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToBookmark;
Object whichitem = Microsoft.Office.Interop.Word.WdGoToDirection.wdGoToFirst;
Object count = 1;
Object name = "Location3";
Bookmark bMark = wordDoc.Bookmarks.get_Item(ref name);
wordDoc.GoTo(ref item, ref whichitem, ref count, ref name);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 Bookmark 对象怎么样?
我没有检查窗口是否滚动到那里...但这应该可以帮助您开始。
编辑:这是我执行 Goto->Bookmark 宏时记录的 VB 代码:
您是否尝试为 WhichItem 和 Count 传递 Type.Missing 以便它复制 VB 调用?
How about using the Bookmark object?
I didn't check if the window scrolls there... but this should get you started.
EDIT: This is the VB code that is recorded when I do a Goto->Bookmark macro:
Did you try passing Type.Missing for WhichItem and Count so it replicates the VB call?
菲利普·华莱士的回答非常有效。
要使用 GoTo 命令,请使用
Word.Application
对象Philip Wallace's answer works great.
To use the GoTo Command, Use the
Word.Application
object