c# - Bookmarks Inconsistencies -


i have tried figure out 30 minutes no avail.

i got code:

class bookmarktest {     application word;     document doc;      public bookmarktest()     {         word = new application();         doc = word.documents.open("c:\\users\\vipar\\desktop\\testskabelon.docx");         console.writeline("before:\n");         foreach (bookmark b in doc.bookmarks)         {             console.writeline("name: {0}\n", b.name);         }         console.writeline("name: {0}, text: {1}\n", doc.bookmarks[2].name, doc.bookmarks[2].range.text);         test(ref doc);         console.writeline("------------------\n");         console.writeline("after:\n");         foreach (bookmark b in doc.bookmarks)         {             console.writeline("name: {0}\n", b.name);         }         console.writeline("name: {0}, text: {1}\n", doc.bookmarks[2].name, doc.bookmarks[2].range.text);         doc = null;         word = null;     }      // fixed code replaces bookmarks correctly rather removing them.     public void test(ref document doc)     {         dictionary<string, bookmark> bookmarks = new dictionary<string, bookmark>();         foreach(bookmark b in doc.bookmarks) {             bookmarks.add(b.name, b);         }         bookmarkreplacenative(bookmarks["titel"], "min nye titel");     }      internal void bookmarkreplacenative(bookmark bookmark, string newtext)     {         object rng = bookmark.range;         string name = bookmark.name;          bookmark.range.text = newtext;         doc.bookmarks.add(name, rng);     } } 

first check if bookmarks there. there 3 (i print them out check before else) array should 0 = kontraktstart, 1 = signatur, 2 = titel , when call doc.bookmarks[2].name signatur rather titel. can't figure out why is. have tried doc.bookmarks[3].name tells me element doesn't exist. reason can't call doc.bookmarks[0].name name though. it's element disappears , replaced nothing.

also test() method removes title bookmarks array entirely. knew happen, how go replacing bookmark rather remove it? when in document can see text on specific bookmark changes, bookmark removed not desired. fixed this. added code snippet

so question two-fold:

  1. how come elements disappearing bookmarks collection before manipulate collection?
  2. how replace bookmark rather remove it? fixed this. added code snippet

thanks in advance!


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -