简体   繁体   中英

Delete contents of bookmark without deleting bookmark in ms word using c#

In ms word2010 I have a bookmark with bookmark name: nameOfBookmark

Now the bookmark content could be anything from just text to a mix of pictures, tables and whatever you could think of putting in a word document.

The problem is as following: I've got my bookmark with some contents I want to delete. However each time I try to delete the contents it also deletes my bookmark which I want to keep.

I've tried this, which simply deletes the whole thing:

    public void cleanBookmark(string bookmark)
    {
        var start = currentDocument.Bookmarks[nameOfBookmark].Start;
        var end = currentDocument.Bookmarks[nameOfBookmark].End;
        Word.Range range = currentDocument.Range(start, end);
        range.Delete();
    }

I've also tried to set the range to this:

Word.Range range = currentDocument.Range(start +1, end -1);

But then I end up with a bookmark that still contains the first and the last character of the content I wanted to delete.

Well I wonder why I have to keep answering my own questions, please notify me if you think it could be something about the way I ask questions.

Anyway I found a solution after a bit more research and it seems like the thing I want simply can't be done or at least not the way I thought it could be done.

If you delete the contents of a bookmark it also deletes the bookmark. So what you have to do is to store the name and range of the bookmark in a local variable and then add the bookmark again after deleting it.

    public void cleanBookmark(string bookmark)
    {
        var start = currentDocument.Bookmarks[bookmark].Start;
        var end = currentDocument.Bookmarks[bookmark].End;
        Word.Range range = currentDocument.Range(start, end);
        range.Delete(); 
        //The Delete() only deletes text so if you got tables in the doc it leaves the tables empty. 
        //The following removes the tables in the current range.
        if (range.Tables.Count != 0)
        {
            for (int i = 1; i <= range.Tables.Count; i++)
            {
                range.Tables[i].Delete();
            }
        }
        currentDocument.Bookmarks.Add(bookmark, range);
    }

If you want to read more about this topic see this question.

I have found that if you convert your text into a table and insert the single I beam bookmark in the cell where you need it to go, the table protects the bookmark even if you delete the content. Then once you have worked out all of your vba for the form, if you convert the form into a dotm, the users shouldn't have any issue with removing the bookmarks by accident and you having to troubleshoot the form to fix their mistake.

You can try this:

ActiveDocument.Bookmarks("name").Range.Characters.First.Delete

PS
Still trying to solve for multiple bookmarks.

For sure many people will encounter the same problem — as I did, too. The difference is that I am programming in VBA ( not C# ). Some search led me to the ultimate solution: see The Anchorange (by Gregory K. Maxey)

Thus I designed the following, quite simple procedure:

Sub PasteIntoBookmark_Test()
Dim BookRange As Word.Range, NewText As String, BookName As String
NewText = ""
BookName = "Name"
If ActiveDocument.Bookmarks.Exists(BookName) Then
    Set BookRange = ActiveDocument.Bookmarks(BookName).Range
    Debug.Print BookRange.Text
    BookRange.Text = NewText
    ActiveDocument.Bookmarks.Add BookName, BookRange
    Debug.Print BookRange.Text
    If ActiveDocument.Bookmarks.Exists(BookName) Then
        Debug.Print "Bookmark " & BookName & " still exists"
    Else    ' Just in case the above wouldn't work (but it does):
        With ActiveDocument.Bookmarks
            .Add Range:=BookRange, Name:=BookName
            .DefaultSorting = wdSortByName
            .ShowHidden = False
        End With
    End If
Else
    Stop
End If
End Sub

In this way one can easily clear a bookmark without deleting it ! Of course one can later on write new text into this bookmark.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM