Marco Reus
Marco Reus

Reputation: 35

Use text as name of the bookmark

Use the text of a cell from a table and use it as a string variable then use that variable as name of the bookmark, with vba word?

I have tried the following, and it says bad bookmark name. I know the constraints of a bookmark name that's why I am deleting the extra new line which ActiveDocument.Tables(1).Cell(1,1).Range.Text gets. But still why I am getting the bad bookmark name runtime error-5828

Function RemoveLastCharacter(myString As String) As String

    
    'Remove Last Character From String
        myString = Left(myString, Len(myString) - 1)
        RemoveLastCharacter = myString
    

End Function
Sub makebookmark()
    Dim kib As String
    kib = ActiveDocument.Tables(1).Cell(1, 1).Range.Text
    Dim kib2 As String
    
    kib2 = RemoveLastCharacter(kib)
     
    ActiveDocument.Tables(1).Cell(1, 3).Range.InsertAfter kib2
    
    ActiveDocument.Bookmarks.Add Name:=kib2, _
    Range:=ActiveDocument.Tables(1).Cell(1, 2).Range
    
End Sub

Upvotes: 0

Views: 471

Answers (1)

Timothy Rylatt
Timothy Rylatt

Reputation: 7850

The end of cell marker is actually 2 characters. You can test this by checking the length of an empty cell.

So you could do either:

kib2 = RemoveLastCharacter(RemoveLastCharacter(kib))

or create a new function

Function RemoveEndOfCellMarker(cellText As String) As String
  'Remove Last 2 Characters From String
  RemoveEndOfCellMarker = Left(cellText, Len(cellText) - 2)
End Function

Upvotes: 1

Related Questions