Reading bookmarks in word
-
How to open a document using word automation and read the bookmark values in it?This has to be done as a function...input of the function will be the full path of the file to be opened and a list of bookmark names.
-
There is similar question and solution too given here: how to retrive bookmarks from word document in c#.net[^]
// ♫ 99 little bugs in the code, // 99 bugs in the code // We fix a bug, compile it again // 101 little bugs in the code ♫
Thank you :) but i am not able to goto the bookmark and bookmark names are not displayed(dont know whether i had inserted bookmarks correctly,please check).Following is the code: Word._Application oWord; Word._Document oDoc; oWord = new Word.Application(); oWord.Visible = true; object fileName = strFileName; object readOnly = false; object isVisible = true; object missing = System.Reflection.Missing.Value; object oEndOfDoc = "\\endofdoc"; oDoc = oWord.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); oDoc.Activate(); Word.Paragraph oPara1; oPara1 = oDoc.Content.Paragraphs.Add(ref missing); oPara1.Range.Text = "Heading 1"; oPara1.Range.Font.Bold = 1; oPara1.Format.SpaceAfter = 24; oPara1.Range.InsertParagraphAfter(); Word.Paragraph oPara2; object oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range; oPara2 = oDoc.Content.Paragraphs.Add(ref oRng); oPara2.Range.Text = "Heading 2"; oPara2.Format.SpaceAfter = 6; oPara2.Range.InsertParagraphAfter(); Word.Paragraph oPara3; oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range; oPara3 = oDoc.Content.Paragraphs.Add(ref oRng); oPara3.Range.Text = "This is a sentence of normal text.Now here is a text"; oPara3.Range.Font.Bold = 0; oPara3.Format.SpaceAfter = 24; oPara3.Range.InsertParagraphAfter(); oDoc.Paragraphs[1].Range.InsertParagraphBefore(); object bm1 = oDoc.Paragraphs[1].Range; Microsoft.Office.Interop.Word.Bookmark bookmark1 = oDoc.Bookmarks.Add("bookmark1", ref bm1); bookmark1.Range.Text = "bm1"; oDoc.Paragraphs[2].Range.InsertParagraphBefore(); object bm2 = oDoc.Paragraphs[2].Range; Microsoft.Office.Interop.Word.Bookmark bookmark2 = oDoc.Bookmarks.Add("bookmark2", ref bm2); bookmark1.Range.Text = "bm2"; int nCount; nCount = oDoc.Bookmarks.Count; for (int i = 0; i < nCount; i++) { object objI = i; MessageBox.Show(oDoc.Bookmarks.get_Item(ref objI).Name); }