Select Text and create Bookmark

S

Sajid

Help MVPs.

I am trying to automate word in C#. I will explain what I am trying to
do. The text in my document is "...[[[[some text]]]]..." (This text is
coming from HTML pages that are imported in via Word automation). What
I want to do is find first set of [[[[ and create a bookmark. Then I
want to find ]]]] and create second bookmark. Then I want to start my
selection from first bookmark and end at second bookmark (inclusive)
and then delete the selection. For some reason my code is not working.
Here is the sample:

Please Help!!!

private void DeleteBookMarks()
{
object unit = MSWord.WdUnits.wdSection;
object extend = MSWord.WdMovementType.wdMove;
object Count = 1;
MSWord.Bookmark bm1;
MSWord.Bookmark bm2;
Object findText = "";
Object matchCase = false;
Object matchWholeWord = false;
Object matchWildcards = false;
Object matchSoundsLike = false;
Object matchAllWordForms = false;
Object forward = Type.Missing;
Object wrap = Type.Missing;
Object format = Type.Missing;
Object replaceWith = "";

aWord.Selection.Find.ClearFormatting() ;
aWord.Selection.Find.Replacement.ClearFormatting() ;


// move to the top of current section
aWord.Selection.GoTo(ref GoToSection,ref GoToFirst, ref Count,ref
oMissing);


findText = "[[[[";
aWord.Selection.Find.Execute(ref findText, ref matchCase,
ref matchWholeWord, ref matchWildcards, ref matchSoundsLike,
ref matchAllWordForms, ref forward, ref oMissing, ref oMissing,
ref replaceWith, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing);

bm1 = aWord.Selection.Bookmarks.Add("Start", ref oMissing);

aWord.Selection.Find.ClearFormatting() ;
aWord.Selection.Find.Replacement.ClearFormatting() ;

findText = "]]]]";
aWord.Selection.Find.Execute(ref findText, ref matchCase,
ref matchWholeWord, ref matchWildcards, ref matchSoundsLike,
ref matchAllWordForms, ref forward, ref oMissing, ref oMissing,
ref replaceWith, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing);

aWord.Selection.Start = bm1.Start;
aWord.Selection.End = bm2.End;
aWord.Selection.Delete(ref oMissing, ref oMissing);


}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top