[英]Word Interop Bookmarks Dont Exist
我目前正在創建一個 c# 程序,該程序使用包含預定義構建塊(帶書簽)的 template.dotx 將內容頁面插入新文檔,所有工作如下
Word._Application oWord;
Word._Document oDoc;
object oMissing = System.Reflection.Missing.Value;
object bAddress = "bAddress";
oWord = new Word.Application();
object oTemplate = _template;
oDoc = oWord.Documents.Add(ref oTemplate, ref oMissing,
ref oMissing, ref oMissing);
Word.Template objTmpl = (Word.Template)oDoc.get_AttachedTemplate();
Word.BuildingBlock objBB = objTmpl.BuildingBlockEntries.Item("PageBB");
Range where = oDoc.Range(oDoc.Content.End - 1, oDoc.Content.End - 1);
var orng = objBB.Insert(where, true);
orng.Bookmarks[bAddress].Range.Text = "Address";
我想反過來執行此操作,以便在以后打開創建的文件,並通讀每個書簽以獲取值。
問題是當我打開生成的 DocXCreatedFromTemplate.docx 時,書簽消失了。 以編程方式,我收到書簽不在收藏錯誤中,而且通過允許打開單詞並手動檢查書簽被替換為文本,但書簽引用已經消失。
有沒有辦法解決這個問題?
我已經解決了這個問題,以下文本替換並刪除了書簽
orng.Bookmarks[bAddress].Range.Text = "Address";
解決方案是之后使用以下方法重新添加書簽
private void UpdateBookmarkWithoutDeleting(object bookmark, string text, _Document document)
{
var bookmarkRange = document.Bookmarks[bookmark].Range;
bookmarkRange.Text = text;
document.Bookmarks.Add(bookmark.ToString(), bookmarkRange);
}
我在以下鏈接(在 VB 中)找到了信息: https://wordmvp.com/FAQs/MacrosVBA/InsertingTextAtBookmark.htm
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.