如何使用 Microsoft Word API 和书签功能以编程方式将 Word 文档打开到特定位置?

发布于 2024-08-09 13:44:47 字数 1210 浏览 1 评论 0原文

我正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

短暂陪伴 2024-08-16 13:44:47

使用 Bookmark 对象怎么样?

    object bookmarkName = "Location3";
    if (wordDoc.Bookmarks.Exists(bookmarkName.ToString()))
    {
        Bookmark bookmark = wordDoc.Bookmarks.get_Item(ref bookmarkName);
        bookmark.Select();
    }

我没有检查窗口是否滚动到那里...但这应该可以帮助您开始。


编辑:这是我执行 Goto->Bookmark 宏时记录的 VB 代码:

Selection.GoTo What:=wdGoToBookmark, Name:="Location3"

您是否尝试为 WhichItem 和 Count 传递 Type.Missing 以便它复制 VB 调用?

How about using the Bookmark object?

    object bookmarkName = "Location3";
    if (wordDoc.Bookmarks.Exists(bookmarkName.ToString()))
    {
        Bookmark bookmark = wordDoc.Bookmarks.get_Item(ref bookmarkName);
        bookmark.Select();
    }

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:

Selection.GoTo What:=wdGoToBookmark, Name:="Location3"

Did you try passing Type.Missing for WhichItem and Count so it replicates the VB call?

清晨说晚安 2024-08-16 13:44:47

菲利普·华莱士的回答非常有效。

要使用 GoTo 命令,请使用 Word.Application 对象

object What = Word.WdGoToItem.wdGoToBookmark;
object oMissing = System.Reflection.Missing.Value;    
wordApp.Selection.GoTo(What, oMissing, oMissing, "bookMarkName");

Philip Wallace's answer works great.

To use the GoTo Command, Use the Word.Application object

object What = Word.WdGoToItem.wdGoToBookmark;
object oMissing = System.Reflection.Missing.Value;    
wordApp.Selection.GoTo(What, oMissing, oMissing, "bookMarkName");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文