tables.cell string used as bookmark name

F

Fernando Peralta

OS: Mac OS 10.3.5
Word X for Mac

I have a big Word table for which each row in column 2 is going to be
marked with a bookmark. The idea is to automate the creation of the
bookmarks. The name of the bookmark is being read from column 1.
Following is the code to automate the process of creating the
bookmarks. The problem resides with using the string read in column 1
as the bookmark name. When running the sub I always get "Bad Bookmark
Name". When using the commented out expression (marked (1) at the
left) I realized that there are extra spaces which have to do with
the length of the string being defined when declaring the string
variable. I have tried to use (* SIZE) when declaring the string to
make its length known (though not the desired solution... just used as
test to understand a bit more), yet the "bad bookmark name" problem
persists.

Sub CreateBookmarks()
Dim rows As Integer
Dim bookmarkName As String
rows = ThisDocument.Tables(1).rows.Count
For Count = 2 To rows
bookmarkName = ThisDocument.Tables(1).Cell(Count,
1).Range.Text
'(1) bookmarkName = Chr$(34) & bookmarkName & Chr$(34)
ThisDocument.Bookmarks.Add Name:=bookmarkName, _
Range:=ThisDocument.Tables(1).Cell(Count, 2).Range
MsgBox bookmarkName
Next Count
End Sub

Thanks in advance,

Fernando
 
D

Doug Robbins

The .Range of a table cell contains the end of cell character. Dim a
variable as a Range, and Set it to the .Range of the Cell and then use

[rangevariable].End = [rangevariable].End - 1

to strip off the end of cell marker.

--
Please respond to the Newsgroup for the benefit of others who may be
interested. Questions sent directly to me will only be answered on a paid
consulting basis.

Hope this helps,
Doug Robbins - Word MVP
 
P

Peter Hewett

Hi Fernando Peralta

I don't have a Mac, so I'm making this comment based on my experiences with Word running
under Windows OS's.

You don't need the double quote characters around the bookmark name, so remove the
chr$(34) your prefixing and suffixing to the name you want to use. Also, when you use:
bookmarkName = ThisDocument.Tables(1).Cell(Count,1).Range.Text

It normally includes the end of paragraph and end of cell characters as well! So you may
need to do the following to get a valid name:
bookmarkName = ThisDocument.Tables(1).Cell(Count,1).Range.Text
bookmarkName = Left$(bookmarkName, Len(bookmarkName)-2)

Add some debug code, or try out the above in the VBA IDE's Immediate window.

HTH + Cheers - Peter


(e-mail address removed) (Fernando Peralta), said:
 
F

Fernando Peralta

Thanks for your help..... I implemented both solutions and they both
worked really well.

Thanks again,

Fernando
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top