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 / November 2007

Tip: Looking for answers? Try searching our database.

Setting a bookmark by macro

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Deejay - 14 Nov 2007 03:11 GMT
Please could someone provide the code to do the following?

Take the characters from the right of bookmarkA to the end of the line and
name them bookmarkB.

Thanks!
Greg Maxey - 14 Nov 2007 12:23 GMT
Something like this:

Sub Scratchmacro()
Dim oBMs As Bookmarks
Dim oRng As Word.Range
Set oBMs = ActiveDocument.Bookmarks
oBMs("bma").Range.Select
Set oRng = ActiveDocument.Bookmarks("\line").Range
oRng.Start = oBMs("bma").End + 1
oRng.End = oRng.End - 1
oBMs.Add "bmb", oRng
MsgBox oBMs("bmb").Range.Text
End Sub

Signature

Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

> Please could someone provide the code to do the following?
>
> Take the characters from the right of bookmarkA to the end of the
> line and name them bookmarkB.
>
> Thanks!
Deejay - 14 Nov 2007 13:28 GMT
Thank you so so much although the message is not always accurate it does
work. May I just trouble you on one further point. How do I check prior to
running this code whether bookmark "bmb" already exists so it doesn't run it
in vain?

Thanks once again.

> Something like this:
>
[quoted text clipped - 16 lines]
> >
> > Thanks!
Greg Maxey - 14 Nov 2007 21:45 GMT
Sub Scratchmacro()
Dim oBM As Bookmark
Dim oBMs As Bookmarks
Dim oRng As Word.Range
Set oBMs = ActiveDocument.Bookmarks
For Each oBM In oBMs
 If oBM.Name = "bmb" Then
   Exit Sub
 End If
Next oBM
oBMs("bma").Range.Select
Set oRng = ActiveDocument.Bookmarks("\line").Range
oRng.Start = oBMs("bma").End + 1
oRng.End = oRng.End - 1
oBMs.Add "bmb", oRng
MsgBox oBMs("bmb").Range.Text
End Sub

Signature

Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

> Thank you so so much although the message is not always accurate it does
> work. May I just trouble you on one further point. How do I check prior to
[quoted text clipped - 24 lines]
>> >
>> > Thanks!
Deejay - 14 Nov 2007 22:27 GMT
Thanks again. I hope you'll allow one more query. You put an exit sub command
to get out of it. In my case your code is nested in a larger macro and I need
it to carry on with other things after the line immediately prior to the
msgbox. So how do I get it simply to leave this section. Must I make your
code a second macro and the first one will call on it or is there a way to
have them all in one?

Kind regards

> Sub Scratchmacro()
> Dim oBM As Bookmark
[quoted text clipped - 42 lines]
> >> >
> >> > Thanks!
Greg Maxey - 15 Nov 2007 00:51 GMT
Sub Scratchmacro()
Dim oBM As Bookmark
Dim oBMs As Bookmarks
Dim bAddBM As Boolean
Dim oRng As Word.Range
Set oBMs = ActiveDocument.Bookmarks
bAddBM = True
For Each oBM In oBMs
 If oBM.Name = "bmb" Then
   bAddBM = False
   Exit For
 End If
Next oBM
If bAddBM Then
 oBMs("bma").Range.Select
 Set oRng = ActiveDocument.Bookmarks("\line").Range
 oRng.Start = oBMs("bma").End + 1
 oRng.End = oRng.End - 1
 oBMs.Add "bmb", oRng
 MsgBox oBMs("bmb").Range.Text
End If
End Sub

Signature

Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

> Thanks again. I hope you'll allow one more query. You put an exit sub
> command to get out of it. In my case your code is nested in a larger
[quoted text clipped - 63 lines]
>>>>>
>>>>> Thanks!
NZ VBA Developer - 15 Nov 2007 03:29 GMT
Greg,

Rather than looping through the whole bookmarks collection and setting a
Boolean variable, couldn't you just use .Exists like this?

Sub Scratchmacro( )
Dim oBMs As Bookmarks
Set oBMs = ActiveDocument.Bookmarks
If Not oBMs.Exists("bmb") Then
Dim oRng As Word.Range
  oBMs("bma").Range.Select
  Set oRng = oBMs("\line").Range
  oRng.Start = oBMs("bma").End + 1
  oRng.End = oRng.End - 1
  oBMs.Add "bmb", oRng
  MsgBox oBMs("bmb").Range.Text
End If
End Sub

Or maybe add a similar check (with some feedback) to make sure the "bma"
exists before trying to select it? e.g.:

If Not oBMs.Exists("bmb") Then
  If oBMs.Exists("bma") Then
  Dim oRng As Word.Range
     oBMs("bma").Range.Select
     Set oRng = oBMs("\line").Range
     oRng.Start = oBMs("bma").End + 1
     oRng.End = oRng.End - 1
     oBMs.Add "bmb", oRng
     MsgBox oBMs("bmb").Range.Text
  Else
     MsgBox "The 'bma' bookmark is missing."
  End If
Else
  MsgBox "The 'bmb' bookmark already exist."
End If

Might be quicker and saves a couple of declarations...

BTW, Deejay, don't worry too much about the small differences between my
code and Greg's. They both work equally well. I just cleaned up a couple of
things - made use of the oBMs object throughout and held off creating the
Range object until it was needed. Minor stuff really.

However, I did discover that if the 'bma' bookmarks is on the last line of
the document, the result isn't quite what's expected. Needs a bit of error
handling for this particular case.
Signature

Cheers!
The Kiwi Koder

> Sub Scratchmacro()
> Dim oBM As Bookmark
[quoted text clipped - 86 lines]
> >>>>>
> >>>>> Thanks!
Greg Maxey - 15 Nov 2007 13:06 GMT
You are quite right.  To be honest I had forgotten about Exists.  I
considered avoiding the loop using error handling:

On Error Resume Next
Set oRng = oBMs("bmb").Range
If Err.Number = 5941 Then
 bAddBM = True
End If
On Error GoTo 0

If view of your comments perhaps:

Sub Scratchmacro()
Dim oBMs As Bookmarks
Set oBMs = ActiveDocument.Bookmarks
If Not oBMs.Exists("bmb") And oBMs.Exists("bma") Then
 Dim oRng As Word.Range
 oBMs("bma").Range.Select
 Set oRng = ActiveDocument.Bookmarks("\line").Range
 oRng.Start = oBMs("bma").End + 1
 If oRng.End <> ActiveDocument.Range.End - 1 Then
   oRng.End = oRng.End - 1
 End If
 oBMs.Add "bmb", oRng
Else
 MsgBox "Bookmark bma does not exist or bookmark bmb already exists"
End If
End Sub

Signature

Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

> Greg,
>
[quoted text clipped - 141 lines]
>>>>>>>
>>>>>>> Thanks!
 
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.