Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / September 2006

Tip: Looking for answers? Try searching our database.

Loop throu bookmarks

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
VinceB1 - 08 Sep 2006 09:56 GMT
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
Jezebel - 08 Sep 2006 11:05 GMT
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
VinceB1 - 08 Sep 2006 12:21 GMT
>For pIndex = 1 to 8
>    if len(ActiveDocument.Bookmarks(BOOKMARK_LABEL & cstr(pIndex)).Range = 0
[quoted text clipped - 11 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
Jean-Guy Marcil - 08 Sep 2006 12:38 GMT
VinceB1 was telling us:
VinceB1 nous racontait que :

>> For pIndex = 1 to 8
>>    if len(ActiveDocument.Bookmarks(BOOKMARK_LABEL &
[quoted text clipped - 27 lines]
> 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

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org

VinceB1 - 08 Sep 2006 13:16 GMT
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:=??)
VinceB1 - 08 Sep 2006 13:22 GMT
>Ok looking into this im more confused, ive put together what ive got so far
>to save confusion
[quoted text clipped - 25 lines]
> '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.
Jean-Guy Marcil - 08 Sep 2006 15:56 GMT
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
'_______________________________________

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org

Jezebel - 08 Sep 2006 23:52 GMT
> 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.
Jean-Guy Marcil - 09 Sep 2006 00:21 GMT
Jezebel was telling us:
Jezebel nous racontait que :

>> 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.
[quoted text clipped - 15 lines]
> 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!

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.