gpt4 book ai didi

c# - 如何使用 Word API 以编程方式打开页面顶部带有书签的文档?

转载 作者:行者123 更新时间:2023-11-30 18:25:44 25 4
gpt4 key购买 nike

使用以下代码在书签处打开Word文档

 Object readOnly = true;
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 bookmarkName = BookMarkName;
if (wordDoc.Bookmarks.Exists(bookmarkName.ToString()))
{
Microsoft.Office.Interop.Word.Bookmark bookmark = wordDoc.Bookmarks.get_Item(ref bookmarkName);
bookmark.Select();
}

Word 文档显示书签,但不在页面顶部。有没有办法像使用 Goto-> Bookmark 那样在页面顶部显示书签?

StackOverflow 问题中建议的代码

How do I use the Microsoft Word API and Bookmarks feature to programmatically open a Word document to a specific location?

这是

Object item = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToBookmark;
Object whichitem = Microsoft.Office.Interop.Word.WdGoToDirection.wdGoToFirst;
Object count = 1;
Object name = BookMarkName;
wordDoc.GoTo(ref item, ref missing, ref missing, ref name);

不起作用并给出帖子中描述的所有错误。

最佳答案

在 MS Word 中转到书签

在 Pascal 中运行这段代码:

 procedure GotoBookMark(name:string);
var d1,d2,d7,d8:olevariant;
begin
d1:=name;
d2:= wdGoToBookmark;
d7:=unAssigned;
d8:=unAssigned;
try
WA.selection.GoTo_(d2, d7,d8,d1);
except
end;
end;

name书签名称;

waMicrosoft.Office.Interop.Word.Application wa= new Microsoft.Office.Interop.Word.Application();

打开文档 MS Word

procedure OpenWordDoc(Name:string);
var FileName,Visible,newtemplate,documenttype:olevariant;
begin
FileName:=Name;
newtemplate:=false;
documenttype:=0;
visible:=true;
WA.Documents.Add( FileName,newtemplate,documenttype,visible);
end;

关于c# - 如何使用 Word API 以编程方式打开页面顶部带有书签的文档?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29690574/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com