[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 811
  • Last Modified:

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

0
Wiretwisterz
Asked:
Wiretwisterz
  • 3
  • 2
1 Solution
 
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
 
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
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

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now