How do you clear a table or bookmark in MS Word?

I have a MS Word Document with a Command button, and a table 2 columns by 4 rows.  Only the second column changes, as the 1st column is used for labeling.  I have a bookmark in each of the rows in the second column and the command button retrieves information from an outside program, assigns the information to variables and then inserts it into the table where the bookmarks are at.

The problem is, the next time you hit the command button, it doesn't simply overwrite the information in the table/bookmark, it appends the information to it.  How can I get it to replace the information in the cell with the new information, each time the command button is clicked?

Here is the code:

The code where I am trying to re-add the bookmark and delete isn't working, but the code below where I use the Selection.TypeText does work by itself.
'FILL IN THE FORM
Application.ScreenUpdating = True
    Selection.GoTo What:=wdGoToBookmark, Name:="HUSBANDNAME"
    Selection.Delete Unit:=wdCharacter, Count:=25
    'Selection.InsertAfter "This is the new text"
    ActiveDocument.Bookmarks.Add Range:=Selection.Range, _
    Name:="HUSBANDNAME"
 
ActiveDocument.Bookmarks("HUSBANDNAME").Select
Selection.TypeText HUSBANDNAME
 
    Selection.GoTo What:=wdGoToBookmark, Name:="WIFENAME"
    Selection.Delete Unit:=wdCharacter, Count:=25
    'Selection.InsertAfter "This is the new text"
    ActiveDocument.Bookmarks.Add Range:=Selection.Range, _
    Name:="WIFENAME"
 
ActiveDocument.Bookmarks("WIFENAME").Select
Selection.TypeText WIFENAME
 
    Selection.GoTo What:=wdGoToBookmark, Name:="TELE_NUM"
    Selection.Delete Unit:=wdCharacter, Count:=25
    'Selection.InsertAfter "This is the new text"
    ActiveDocument.Bookmarks.Add Range:=Selection.Range, _
    Name:="TELE_NUM"
 
ActiveDocument.Bookmarks("TELE_NUM").Select
Selection.TypeText TELE_NUM
 
    Selection.GoTo What:=wdGoToBookmark, Name:="CELL_NUM"
    Selection.Delete Unit:=wdCharacter, Count:=25
    'Selection.InsertAfter "This is the new text"
    ActiveDocument.Bookmarks.Add Range:=Selection.Range, _
    Name:="CELL_NUM"
 
ActiveDocument.Bookmarks("CELL_NUM").Select
Selection.TypeText CELL_NUM

Open in new window

LVL 1
WiretwisterzAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

cquinnCommented:
I use the following functions to find and set bookmark values:


Public Sub SetBookmarkContent(sBookmark As String, sVarname As String)
Dim bkRange As Word.Range
On Error GoTo ErrTrap
 
    If ActiveDocument.Bookmarks.Exists(sBookmark) Then
        Set bkRange = ActiveDocument.Bookmarks(sBookmark).Range
        bkRange.Text = sVarname
        ActiveDocument.Bookmarks.Add Name:=sBookmark, Range:=bkRange
    End If
 
    Exit Sub
    
ErrTrap:
    End
End Sub
 
Public Function GoToBookmark(strBookmark As String) As Integer
On Error GoTo ErrTrap
 
    GoToBookmark = True
    ActiveDocument.Bookmarks(strBookmark).Select
    
    Exit Function
    
ErrTrap:
    GoToBookmark = False
    
End Function

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
wobbledCommented:
This is a common problem when going to bookmarks and trying to change the text

The way around your problem is to go to the bookmark, select it, then delete the selection (with this it will delete the bookmark.  Add text and then add the bookmark back to the selection.

This is exactly what cquinn is suggesting

If ActiveDocument.Bookmarks.Exists(sBookmark) Then
        Set bkRange = ActiveDocument.Bookmarks(sBookmark).Range    'selects the bookmarked range
        bkRange.Text = sVarname                                                             'overwrites it with new text
        ActiveDocument.Bookmarks.Add Name:=sBookmark, Range:=bkRange       're-adds the bookmark
    End If

Hope that helps clarify it a little
0
WiretwisterzAuthor Commented:
That did clarify it Wobbled, I appreciate it,  Please excuse me for being so dense, but I can't quite get it to work right.

I've added the two functions suggested, and then tried to call them from the OnClick code of the command button like this:


But that code produces an, "Expected =" error.  What exactly am I supposed to set the function call equal to?
    GoToBookmark (CELL_NUM)
    SetBookmarkContent(CELL_NUM, CELL_NUM)

Open in new window

0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

WiretwisterzAuthor Commented:
Does anyone know what mistake I am making?
0
cquinnCommented:
They are functions so must be called in a different way, and if the bookmark is called Cell_num it must be enclosed in quotes in the call


Dim iReturn as integer
 
iReturn = SetBookmarkContent("CELL_NUM", CELL_NUM)

Open in new window

0
cquinnCommented:
Sorry - ignore the first bit and do it this way:

SetBookmarkContent "CELL_NUM", CELL_NUM

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Word

From novice to tech pro — start learning today.