bookmark issue

  • Thread starter VinceB1 via OfficeKB.com
  • Start date
V

VinceB1 via OfficeKB.com

hello there i posted recently with a problem that was kindly looked into but
is still causing me problems.

i have done some debugging and have narrowed it down to the following line

If Len(.Bookmark(BOOKMARK_LABEL & CStr(lngIndex)).Range) = 0 Then

now ive looked into this and understand what this line does so my question is,
instead of comparing it to the length of the bookmark value which is zero. Is
there any way to make the comparison to "" instead? e.g.

if bookmark range is equal to "" with the name Bookmark_value& lngIndex then
...

Thanks for any help :)
 
J

Jezebel

If .Bookmark(BOOKMARK_LABEL & CStr(lngIndex)).Range = "" Then

However, the two tests will gave exactly the same result, and checking if
the length of a string is zero is more efficient than checking if the string
is equal to an empty string. The reason being that internally strings are
stored in two parts: a header containing the starting address and length of
the content, and the content itself. if you test the length, VBA needs to
refer only to the header part; if you test the content VBA has to retrieve
the header, then use that to retrieve the content. Doubly inefficient in
this case, because the compiler has to convert your literal to a string
variable also, then when it runs the line it has to retrieve both strings,
in order to make the comparison.
 
V

VinceB1 via OfficeKB.com

Hey again,

i tried and tested this method which gave me much the same, upon more testing
i then decided that i would run a test as follows

strBookmark2 = .Bookmarks(Com1).range ' Have also tried strBookmark2 = .
Bookmarks(Com1)

then i have gone on to

Call WriteProp(sPropName:="Comments", sValue:=strBookmark2)

I done this simply to test that the range of the bookmark was printing to the
string strBookmark2 and found that there was nothing in there although com1
contains text.

Any Ideas? again thanks for the help :)
 
C

Cindy M.

Hi VinceB1,
i tried and tested this method which gave me much the same, upon more testing
i then decided that i would run a test as follows

strBookmark2 = .Bookmarks(Com1).range ' Have also tried strBookmark2 = .
Bookmarks(Com1)

then i have gone on to

Call WriteProp(sPropName:="Comments", sValue:=strBookmark2)

I done this simply to test that the range of the bookmark was printing to the
string strBookmark2 and found that there was nothing in there although com1
contains text.
Does it truly "contain" text? Or is your bookmark just marking a point between
two characters?

If you execute .Bookmarks(Com1).Range.Select is all the text selected that you
expect to see in strBookmark2? Or is the insertion mark blinking at the
beginning of the text?

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 
V

VinceB1 via OfficeKB.com

I have discovered that this is due to the range property, the range cannot be
put into a string as shown in a line of text below

(strBookmark is defined as a string)

strBookmark = .Bookmarks(BOOKMARK_LABEL & CStr(lngIndex)).Range

so my question is, is there anyway that you can cast the contents of a range
to a string object?
 
V

VinceB1 via OfficeKB.com

VinceB1 said:
I have discovered that this is due to the range property, the range cannot be
put into a string as shown in a line of text below

(strBookmark is defined as a string)

strBookmark = .Bookmarks(BOOKMARK_LABEL & CStr(lngIndex)).Range

so my question is, is there anyway that you can cast the contents of a range
to a string object?

Hello there Com1 contains text if i simply put

Call WriteProp(sPropName:="Comments", sValue:=Com1)

i get all the details that i require in the comments field, but if i try and
save the details inside this range to a string i do not get no output at all

with ref to the posts above showing

strBookmark2 = .Bookmarks(Com1).range ' Have also tried strBookmark2 = .
Bookmarks(Com1)

and then

Call WriteProp(sPropName:="Comments", sValue:=strBookmark2) i get nothing in
this box whatsoever.

this leads me to beleive that it is the part where i am making the bookmark
equal to the string that is causing the problem.
 
V

VinceB1 via OfficeKB.com

Cindy said:
Hi VinceB1,
i tried and tested this method which gave me much the same, upon more testing
i then decided that i would run a test as follows
[quoted text clipped - 9 lines]
string strBookmark2 and found that there was nothing in there although com1
contains text.

Does it truly "contain" text? Or is your bookmark just marking a point between
two characters?

If you execute .Bookmarks(Com1).Range.Select is all the text selected that you
expect to see in strBookmark2? Or is the insertion mark blinking at the
beginning of the text?

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)

Hello there Com1 contains text if i simply put

Call WriteProp(sPropName:="Comments", sValue:=Com1)

i get all the details that i require in the comments field, but if i try and
save the details inside this range to a string i do not get no output at all

with ref to the posts above showing

strBookmark2 = .Bookmarks(Com1).range ' Have also tried strBookmark2 = .
Bookmarks(Com1)

and then

Call WriteProp(sPropName:="Comments", sValue:=strBookmark2) i get nothing in
this box whatsoever.

this leads me to beleive that it is the part where i am making the bookmark
equal to the string that is causing the problem.
 
V

VinceB1 via OfficeKB.com

Ok... im getting there i think, on further inspection i have narrowed it down
to the

lngIndex = 1 To 8

loop in there it doesn't seem to be stopping as soon as the range = "" and
seems to go right up to Com9 and then trying to print whatever is in there
(Nothing at all). So can any1 see the error in this as i am stumped.



With ActiveDocument

For lngIndex = 1 To 8
If .Bookmarks(BOOKMARK_LABEL & CStr(lngIndex)).Range = "" Then
If lngIndex = 1 Then
MsgBox "There are not valid bookmarked ranges in this document.
"
Else
lngIndex = lngIndex - 1


Exit For
End If
End If
Next lngIndex
strBookmark = .Bookmarks(BOOKMARK_LABEL & CStr(lngIndex)).Range
Call WriteProp(sPropName:="Comments", sValue:=strBookmark)
End With
 
C

Cindy M.

Hi VinceB1,

1. VBA is generous in what it allows, but this can lead to all kinds of weird
errors that are difficult to track down. Technically, if you use "Range" what
you get back is a Range. VBA allows for "default properties" which means if you
try to assign "Range" to something else it will see if the default property
(.Text) fits. To assign the contents of a bookmark to a string variable you
should *technically* use: str = .Bookmarks(index).Range.Text

2. If you're unsure how many bookmarks the document may contain, then I would
suggest
Dim lBookmarks as Long
lBookmarks = ActiveDocument.Bookmarks.Count
for i = 1 to lBookmarks

3. In order to avoid errors, test whether a bookmark exists before trying to
address it:
If .Bookmark.Exists(BOOKMARK_LABEL & CStr(lngIndex)) Then

4. Then, as Jezebel mentioned, it's more efficient to test the Length of a
string, rather than whether it's "empty". I think this test, on a RANGE object,
may be where things are going wrong for you. Test
If Len(.Bookmarks(BOOKMARK_LABEL & CStr(lngIndex)).Range.TEXT) = 0
Ok... im getting there i think, on further inspection i have narrowed it down
to the

lngIndex = 1 To 8

loop in there it doesn't seem to be stopping as soon as the range = "" and
seems to go right up to Com9 and then trying to print whatever is in there
(Nothing at all). So can any1 see the error in this as i am stumped.



With ActiveDocument

For lngIndex = 1 To 8
If .Bookmarks(BOOKMARK_LABEL & CStr(lngIndex)).Range = "" Then
If lngIndex = 1 Then
MsgBox "There are not valid bookmarked ranges in this document.
"
Else
lngIndex = lngIndex - 1


Exit For
End If
End If
Next lngIndex
strBookmark = .Bookmarks(BOOKMARK_LABEL & CStr(lngIndex)).Range
Call WriteProp(sPropName:="Comments", sValue:=strBookmark)
End With

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 

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