John Baum
John Baum

Reputation: 3331

Word automation adding/deleting text

I am working on a word automation project in c# and am using the interop word library to read/write to word. I am currently using bookmarks in a word template doc to find where to write info to in the word doc from c#. One of my bookmarks consists of two highlighted lines in the doc. Based on a boolean value, i have to decide whether to leave that text there and add a new line of text right after it, or delete those existing two lines from the doc.

So here is my pseudo for it:

if (writeToDoc)
{
// leave selected bookmark text intact and press enter to write another line right after
}
else
{
//delete the selected bookmark text
}

Can anyone please show me how to delete existing text as well as do the equivalent of pressing enter and writing another line from c#?

Thanks

EDIT: Here is the code i have (roughly)

    foreach (var bookmark in wordDoc.Bookmarks)
    {
        var bookMarkNameExistsInCode = listOfBookmarks.Contains(wordDoc.Bookmarks[bookmark].Name);
        if (bookMarkNameExistsInCode )
        {
            object oBookMarkName = wordDoc.Bookmarks[bookmark].Name;
            rng = wordDoc.Bookmarks.get_Item(ref oBookMarkName).Range;
            // at this point i am pointing to the two selected lines labelled as a bookmark in word. How can i deselect and add a new line?
        }
        }

Upvotes: 1

Views: 1256

Answers (1)

MadBoy
MadBoy

Reputation: 11104

If the word manipulation is done on DocX files you could use DocX library and use some very simple commands like text.ReplaceText(); and other very easy/intuitive commands. Replacing Interop with DocX if possible should get you up and running in no time :)

Upvotes: 0

Related Questions