Link to home
Start Free TrialLog in
Avatar of Wiretwisterz
WiretwisterzFlag for United States of America

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of cquinn
cquinn
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Avatar of Wiretwisterz

ASKER

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

Does anyone know what mistake I am making?
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

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

SetBookmarkContent "CELL_NUM", CELL_NUM

Open in new window