Loop throu bookmarks

V

VinceB1

hello there i want to loop through bookmarks named com1 to com8 until finding
the first bookmark with nothing in it and then go to the previous bookmark.

so for example com1 to com3 have details in them but com4 does not search
through the bookmarks find the first one with nothing in "com4" and then
print the details in the previous bookmark.

so far i have

Const BOOKMARK_LABEL As String = "Com"
Const BOOKMARK_CON As String = ""



nPosition = 0
For Each oBookmark In ActiveDocument.Bookmarks
If InStr(1, oBookmark.Name, BOOKMARK_LABEL, vbTextCompare) =
1 Then
nPosition = nPosition + 1
If oBookmark.Range = BOOKMARK_CON Then nPosition =
nPosition - 1

Call WriteProp(sPropName:="Comments", sValue:="i hate
this")
Else: Call WriteProp(sPropName:="Comments", sValue:
=oBookmark.Name)

'Else: Call WriteProp(sPropName:="Comments", sValue:
=oBookmark.Name)
End If
Next oBookmark

i know its miles away but ive confused myself to the point of not knowing
what i'm doing. thanks
 
J

Jezebel

For pIndex = 1 to 8
if len(ActiveDocument.Bookmarks(BOOKMARK_LABEL & cstr(pIndex)).Range = 0
then
TheOneWeWant = pIndex - 1
exit for
end if
Next

Print the details of bookmark (TheOneWeWant )
 
V

VinceB1

Jezebel said:
For pIndex = 1 to 8
if len(ActiveDocument.Bookmarks(BOOKMARK_LABEL & cstr(pIndex)).Range = 0
then
TheOneWeWant = pIndex - 1
exit for
end if
Next

Print the details of bookmark (TheOneWeWant )
hello there i want to loop through bookmarks named com1 to com8 until
finding
[quoted text clipped - 32 lines]
i know its miles away but ive confused myself to the point of not knowing
what i'm doing. thanks


where you have put : Print the details of bookmark (TheOneWeWant)

im actually transfering the data to document properties using

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

value would be the name of the bookmarkin e.g to put com1 value into comments
i have

Com1 = ActiveDocument.Bookmarks("Com1").Range
Call WriteProp(sPropName:="Comments", sValue:=Com1)

could you advise me of the type that the procedure is returning is
TheOneWeWant a bookmark or a integer or even something i have no idea about
 
J

Jean-Guy Marcil

VinceB1 was telling us:
VinceB1 nous racontait que :
Jezebel said:
For pIndex = 1 to 8
if len(ActiveDocument.Bookmarks(BOOKMARK_LABEL &
cstr(pIndex)).Range = 0 then
TheOneWeWant = pIndex - 1
exit for
end if
Next

Print the details of bookmark (TheOneWeWant )
hello there i want to loop through bookmarks named com1 to com8
until finding
[quoted text clipped - 32 lines]
i know its miles away but ive confused myself to the point of not
knowing what i'm doing. thanks


where you have put : Print the details of bookmark (TheOneWeWant)

im actually transfering the data to document properties using

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

value would be the name of the bookmarkin e.g to put com1 value into
comments i have

Com1 = ActiveDocument.Bookmarks("Com1").Range
Call WriteProp(sPropName:="Comments", sValue:=Com1)

could you advise me of the type that the procedure is returning is
TheOneWeWant a bookmark or a integer or even something i have no idea
about

In that sample code, TheOneWeWant returns a Long
TheOneWeWant = pIndex - 1
because pIndex is a long.

So, to get your bookmark name and content, use:
ActiveDocument.Bookmarks(BOOKMARK_LABEL & cstr(TheOneWeWant)).Range.Text

By the way, for future compatibility and ease of code maintenance, it is
better to use full code and not to rely on default properties. So, since it
seems you are interested in the value of the bookmark, i.e. its text, and
assuming Com1 is defined like this:
Dim Com1 As String
it would be better to use
Com1 = ActiveDocument.Bookmarks("Com1").Range.Text
instead of
Com1 = ActiveDocument.Bookmarks("Com1").Range
unless of course Com1 is defined like this:
Dim Com1 As Range

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
V

VinceB1

Ok looking into this im more confused, ive put together what ive got so far
to save confusion

Sub FileSave()
With ActiveDocument

Dim Title As String
Dim Com1 As Range
Dim Com2 As Range 'After here come 3 through 8
Title = ActiveDocument.Bookmarks("Title").Range

Call WriteProp(sPropName:="Title", sValue:=Title) 'Reads from
Bookmark Title and writes to Title field in props

'then i want to read all of the Com values go to the first one with
empty range and write the one before
e.g. com2 is empty but com1 has text go to com2 see that its empty
and therefore use com1.

For pIndex = 1 To 8
If Len(ActiveDocument.Bookmarks(BOOKMARK_LABEL & CStr(pIndex)).
Range) = 0 Then
theOneweWant = pIndex - 1
Exit For
End If
Next

'ActiveDocument.Bookmarks(BOOKMARK_LABEL & CStr(theOneweWant)).Range.text
Call WriteProp(sPropName:="Comments", sValue:=??)
 
V

VinceB1

VinceB1 said:
Ok looking into this im more confused, ive put together what ive got so far
to save confusion

Sub FileSave()
With ActiveDocument

Dim Title As String
Dim Com1 As Range
Dim Com2 As Range 'After here come 3 through 8
Title = ActiveDocument.Bookmarks("Title").Range

Call WriteProp(sPropName:="Title", sValue:=Title) 'Reads from
Bookmark Title and writes to Title field in props

'then i want to read all of the Com values go to the first one with
empty range and write the one before
e.g. com2 is empty but com1 has text go to com2 see that its empty
and therefore use com1.

For pIndex = 1 To 8
If Len(ActiveDocument.Bookmarks(BOOKMARK_LABEL & CStr(pIndex)).
Range) = 0 Then
theOneweWant = pIndex - 1
Exit For
End If
Next

'ActiveDocument.Bookmarks(BOOKMARK_LABEL & CStr(theOneweWant)).Range.text
Call WriteProp(sPropName:="Comments", sValue:=??)

im getting that i have an invalid use of property on the .text after Range.
 
J

Jean-Guy Marcil

VinceB1 was telling us:
VinceB1 nous racontait que :
Ok looking into this im more confused, ive put together what ive got
so far to save confusion

There are indeed many confusing aspects to your code...

Sub FileSave()
With ActiveDocument
'This statement should precede a block that uses methods/properties of
ActiveDocument, I do not see any in your code.
'And where is the "End With" statement?
'e.g.:
With ActiveDocument
.Save
.Close
End With

Dim Title As String

'Why do you need to define 8 range objects?
'Are you going to manipulate the range or the text in the range?
'If you re going to manipulate the text(As it seems you are doing) you
should be declaring String variables
'Also, I do not see any of these being used in the code you posted...
Dim Com1 As Range
Dim Com2 As Range 'After here come 3 through 8

'You have above "With ActiveDocument", and yet you are using ActiveDocument
in this line, in particular reason?
'Above, "Title is defined as aRange, yet you are using the Text (a string)
default proiperty of the Range Property
Title = ActiveDocument.Bookmarks("Title").Range

Call WriteProp(sPropName:="Title", sValue:=Title) 'Reads from
Bookmark Title and writes to Title field in props

'then i want to read all of the Com values go to the first one with
empty range and write the one before
e.g. com2 is empty but com1 has text go to com2 see that its empty
and therefore use com1.

For pIndex = 1 To 8
If Len(ActiveDocument.Bookmarks(BOOKMARK_LABEL &
CStr(pIndex)).
Range) = 0 Then
theOneweWant = pIndex - 1
Exit For
End If
Next

'You commented this line out.... Even if you did not commented it out, it
would not do anything
'You would have to assign the text value of the bookmark range to a
variable, or something...
'ActiveDocument.Bookmarks(BOOKMARK_LABEL & CStr(theOneweWant)).Range.text
Call WriteProp(sPropName:="Comments", sValue:=??)

So, from what I have seen and guesses, here is a potentially useful bit of
code that does what I think you want:

'_______________________________________
Sub FileSave()

Const BOOKMARK_LABEL As String = "Com"

Dim strTitle As String
Dim strBookmark As String
Dim lngIndex As Long

With ActiveDocument
strTitle = .Bookmarks("Title").Range.Text

Call WriteProp(sPropName:="Title", sValue:=strTitle)

For lngIndex = 1 To 8
If Len(.Bookmarks(BOOKMARK_LABEL & CStr(lngIndex)).Range) = 0 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

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

Call WriteProp(sPropName:="Comments", sValue:=strBookmark)
End With

End Sub
'_______________________________________

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
J

Jezebel

By the way, for future compatibility and ease of code maintenance, it is
better to use full code and not to rely on default properties.

You might be right in respect of maintenance, and I guess it's wise in code
for beginners to be explicit. But:

1. There's no future compatability issue. Once the VBA-writers have settled
on a default property, they're stuck with it unless they choose to forego
backward compatability. And if they do decide that (which eventually, they
will) your code probably isn't going to run anyway.

2. Using the default property is more efficient, particularly if your
variable is declared 'as object'. There are some GetTickCount tests around
that compare the two methods, and there is a measurable (albeit not, in most
contexts, terribly significant) difference. The internal logic, apparently,
is that if you specify the property explicitly VBA has to validate it, then
look it up in the table of property pointers for the object, then retrieve
it; if you use the default it simply retrieves it.
 
J

Jean-Guy Marcil

Jezebel was telling us:
Jezebel nous racontait que :
You might be right in respect of maintenance, and I guess it's wise
in code for beginners to be explicit. But:

1. There's no future compatability issue. Once the VBA-writers have
settled on a default property, they're stuck with it unless they
choose to forego backward compatability. And if they do decide that
(which eventually, they will) your code probably isn't going to run
anyway.
2. Using the default property is more efficient, particularly if your
variable is declared 'as object'. There are some GetTickCount tests
around that compare the two methods, and there is a measurable
(albeit not, in most contexts, terribly significant) difference. The
internal logic, apparently, is that if you specify the property
explicitly VBA has to validate it, then look it up in the table of
property pointers for the object, then retrieve it; if you use the
default it simply retrieves it.

Thanks for your thoughts... My only concern is that I do not know all the
default properties... so I do not take chances...I am always explicit...my
code is not minor!

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

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