我正在
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 isVisible,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);
解决方法
如何使用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"
您是否尝试将Type.Missing传递给WhichItem和Count以便复制VB调用?