Reply
 
Thread Tools Display Modes
  #1   Report Post  
Posted to microsoft.public.word.docmanagement
MattyP MattyP is offline
external usenet poster
 
Posts: 6
Default Macro to Generate Bookmarks Based on Text Style

Hello -
A few days ago, I posted a question to see if it were possible (and what the
code might be) to create a macro that would autmatically add Bookmarks to a
Word document, based on text style (in this case, Heading 1's). I got a
great reply (credit to the original responder !), but later realized it would
definitely be helpful if I could tweak it a bit more.

The macro code originally provided is below. After running it, any text
strings formatted as Heading 1 are automatically identified as Bookmarks
(which is great). The catch is, these are named as "Bookmark 1", "Bookmark
2", etc., rather than the being named the same as the actual text string
itself. So, the million dollar question is this --- is there any way to
modify the macro code below (or create a new one) that automatically
generates Bookmarks based on text style (i.e., Heading 1), and also names
them the same as the text string ? Any guidance would be most appreciated !

- Matt

Here's the macro code I originally received (which works absolutely great,
but does name the Bookmarks "generically" --- 1,2,3, etc.)..........

------------------------------------------
Sub Macro1()
'
' Macro1 Macro
' Macro recorded 3/8/2009 by MattyP
'
Selection.HomeKey wdStory
Selection.Find.Style = ActiveDocument.Styles("Heading 1")
Dim frange As Range
Dim i As Long
i = 1
With Selection.Find
Do While .Execute(FindText:="", Forward:=True, _
MatchWildcards:=False, Wrap:=wdFindStop, MatchCase:=False) = True
Set frange = Selection.Range
ActiveDocument.Bookmarks.Add "Bookmark" & i, frange
i = i + 1
Selection.Collapse wdCollapseEnd
Loop
End With

End Sub
  #2   Report Post  
Posted to microsoft.public.word.docmanagement
Lene Fredborg Lene Fredborg is offline
external usenet poster
 
Posts: 1,291
Default Macro to Generate Bookmarks Based on Text Style

Some comments before making any attempts to change the macro:

There are some restrictions to bookmark names:

1. Bookmarks must be named uniquely, i.e. you cannot have two bookmarks with
the same name in the same document. The naming convention used in the macro
makes sure that this is fulfilled.

2. A bookmark name can consist of no more than 40 characters. The name is
not allowed to start with a number and it can contain only letters, numbers
and underscores (e.g. no spaces).

As you can imagine, this means that your headings cannot directly be used as
bookmark names. Spaces will need to be removed. If the text starts with a
number, the name will need to be changed. In addition, it will be necessary
to keep track of which names have already be used since it is not unrealistic
that the first 40 characters of two or more of the headings are identical.
Also, if you change one or more heading texts, the bookmark names will no
longer reflect the content anyway.

Maybe you want to keep the macro in the current version?

--
Regards
Lene Fredborg - Microsoft MVP (Word)
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word


"MattyP" wrote:

Hello -
A few days ago, I posted a question to see if it were possible (and what the
code might be) to create a macro that would autmatically add Bookmarks to a
Word document, based on text style (in this case, Heading 1's). I got a
great reply (credit to the original responder !), but later realized it would
definitely be helpful if I could tweak it a bit more.

The macro code originally provided is below. After running it, any text
strings formatted as Heading 1 are automatically identified as Bookmarks
(which is great). The catch is, these are named as "Bookmark 1", "Bookmark
2", etc., rather than the being named the same as the actual text string
itself. So, the million dollar question is this --- is there any way to
modify the macro code below (or create a new one) that automatically
generates Bookmarks based on text style (i.e., Heading 1), and also names
them the same as the text string ? Any guidance would be most appreciated !

- Matt

Here's the macro code I originally received (which works absolutely great,
but does name the Bookmarks "generically" --- 1,2,3, etc.)..........

------------------------------------------
Sub Macro1()
'
' Macro1 Macro
' Macro recorded 3/8/2009 by MattyP
'
Selection.HomeKey wdStory
Selection.Find.Style = ActiveDocument.Styles("Heading 1")
Dim frange As Range
Dim i As Long
i = 1
With Selection.Find
Do While .Execute(FindText:="", Forward:=True, _
MatchWildcards:=False, Wrap:=wdFindStop, MatchCase:=False) = True
Set frange = Selection.Range
ActiveDocument.Bookmarks.Add "Bookmark" & i, frange
i = i + 1
Selection.Collapse wdCollapseEnd
Loop
End With

End Sub

  #3   Report Post  
Posted to microsoft.public.word.docmanagement
Suzanne S. Barnhill Suzanne S. Barnhill is offline
external usenet poster
 
Posts: 33,624
Default Macro to Generate Bookmarks Based on Text Style

You do know that the built-in heading styles are bookmarked automatically by
Word?

--
Suzanne S. Barnhill
Microsoft MVP (Word)
Words into Type
Fairhope, Alabama USA
http://word.mvps.org

"MattyP" wrote in message
...
Hello -
A few days ago, I posted a question to see if it were possible (and what
the
code might be) to create a macro that would autmatically add Bookmarks to
a
Word document, based on text style (in this case, Heading 1's). I got a
great reply (credit to the original responder !), but later realized it
would
definitely be helpful if I could tweak it a bit more.

The macro code originally provided is below. After running it, any text
strings formatted as Heading 1 are automatically identified as Bookmarks
(which is great). The catch is, these are named as "Bookmark 1",
"Bookmark
2", etc., rather than the being named the same as the actual text string
itself. So, the million dollar question is this --- is there any way to
modify the macro code below (or create a new one) that automatically
generates Bookmarks based on text style (i.e., Heading 1), and also names
them the same as the text string ? Any guidance would be most appreciated
!

- Matt

Here's the macro code I originally received (which works absolutely great,
but does name the Bookmarks "generically" --- 1,2,3, etc.)..........

------------------------------------------
Sub Macro1()
'
' Macro1 Macro
' Macro recorded 3/8/2009 by MattyP
'
Selection.HomeKey wdStory
Selection.Find.Style = ActiveDocument.Styles("Heading 1")
Dim frange As Range
Dim i As Long
i = 1
With Selection.Find
Do While .Execute(FindText:="", Forward:=True, _
MatchWildcards:=False, Wrap:=wdFindStop, MatchCase:=False) = True
Set frange = Selection.Range
ActiveDocument.Bookmarks.Add "Bookmark" & i, frange
i = i + 1
Selection.Collapse wdCollapseEnd
Loop
End With

End Sub


  #4   Report Post  
Posted to microsoft.public.word.docmanagement
MattyP MattyP is offline
external usenet poster
 
Posts: 6
Default Macro to Generate Bookmarks Based on Text Style

Hi Lene -
Thanks for the info (definitely insightful !). Actually though, I believe
I've already accounted for all the 'potential problems' below (as I already
have one document - with all the same headings - where Bookmarks by section
heading name have already been added manually). The headings are all unique
and aren't numbered, but did have to change the spaces to underscores (I
formatted them as white font, so they wouldn't actually appear in the
document !).

The problem is I've got a bunch more documents (set-up exactly the same,
minus the Bookmarks), that I'd like to add Bookmarks to as well (and a macro
would save all kinds of time !). The macro below does work well, but it's
sometimes difficult to determine which "Bookmark #" each one is referring to !

So, if it would be possible, a macro to create Bookmarks by section name
would definitely help me out !

- Matt





"Lene Fredborg" wrote:

Some comments before making any attempts to change the macro:

There are some restrictions to bookmark names:

1. Bookmarks must be named uniquely, i.e. you cannot have two bookmarks with
the same name in the same document. The naming convention used in the macro
makes sure that this is fulfilled.

2. A bookmark name can consist of no more than 40 characters. The name is
not allowed to start with a number and it can contain only letters, numbers
and underscores (e.g. no spaces).

As you can imagine, this means that your headings cannot directly be used as
bookmark names. Spaces will need to be removed. If the text starts with a
number, the name will need to be changed. In addition, it will be necessary
to keep track of which names have already be used since it is not unrealistic
that the first 40 characters of two or more of the headings are identical.
Also, if you change one or more heading texts, the bookmark names will no
longer reflect the content anyway.

Maybe you want to keep the macro in the current version?

--
Regards
Lene Fredborg - Microsoft MVP (Word)
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word


"MattyP" wrote:

Hello -
A few days ago, I posted a question to see if it were possible (and what the
code might be) to create a macro that would autmatically add Bookmarks to a
Word document, based on text style (in this case, Heading 1's). I got a
great reply (credit to the original responder !), but later realized it would
definitely be helpful if I could tweak it a bit more.

The macro code originally provided is below. After running it, any text
strings formatted as Heading 1 are automatically identified as Bookmarks
(which is great). The catch is, these are named as "Bookmark 1", "Bookmark
2", etc., rather than the being named the same as the actual text string
itself. So, the million dollar question is this --- is there any way to
modify the macro code below (or create a new one) that automatically
generates Bookmarks based on text style (i.e., Heading 1), and also names
them the same as the text string ? Any guidance would be most appreciated !

- Matt

Here's the macro code I originally received (which works absolutely great,
but does name the Bookmarks "generically" --- 1,2,3, etc.)..........

------------------------------------------
Sub Macro1()
'
' Macro1 Macro
' Macro recorded 3/8/2009 by MattyP
'
Selection.HomeKey wdStory
Selection.Find.Style = ActiveDocument.Styles("Heading 1")
Dim frange As Range
Dim i As Long
i = 1
With Selection.Find
Do While .Execute(FindText:="", Forward:=True, _
MatchWildcards:=False, Wrap:=wdFindStop, MatchCase:=False) = True
Set frange = Selection.Range
ActiveDocument.Bookmarks.Add "Bookmark" & i, frange
i = i + 1
Selection.Collapse wdCollapseEnd
Loop
End With

End Sub

  #5   Report Post  
Posted to microsoft.public.word.docmanagement
MattyP MattyP is offline
external usenet poster
 
Posts: 6
Default Macro to Generate Bookmarks Based on Text Style

I do (I'm assuming you're talking about the 'Document Map' toolbar button) ?
Only problem is, that 'catches' all the built-in heading styles (so the list
is a bit longer and cumbersome to wade through !). Alternatively, is there a
way to modify the Document Map feature, to only 'catch' certain built-in
heading styles (i.e., just Heading 1) ?

"Suzanne S. Barnhill" wrote:

You do know that the built-in heading styles are bookmarked automatically by
Word?

--
Suzanne S. Barnhill
Microsoft MVP (Word)
Words into Type
Fairhope, Alabama USA
http://word.mvps.org

"MattyP" wrote in message
...
Hello -
A few days ago, I posted a question to see if it were possible (and what
the
code might be) to create a macro that would autmatically add Bookmarks to
a
Word document, based on text style (in this case, Heading 1's). I got a
great reply (credit to the original responder !), but later realized it
would
definitely be helpful if I could tweak it a bit more.

The macro code originally provided is below. After running it, any text
strings formatted as Heading 1 are automatically identified as Bookmarks
(which is great). The catch is, these are named as "Bookmark 1",
"Bookmark
2", etc., rather than the being named the same as the actual text string
itself. So, the million dollar question is this --- is there any way to
modify the macro code below (or create a new one) that automatically
generates Bookmarks based on text style (i.e., Heading 1), and also names
them the same as the text string ? Any guidance would be most appreciated
!

- Matt

Here's the macro code I originally received (which works absolutely great,
but does name the Bookmarks "generically" --- 1,2,3, etc.)..........

------------------------------------------
Sub Macro1()
'
' Macro1 Macro
' Macro recorded 3/8/2009 by MattyP
'
Selection.HomeKey wdStory
Selection.Find.Style = ActiveDocument.Styles("Heading 1")
Dim frange As Range
Dim i As Long
i = 1
With Selection.Find
Do While .Execute(FindText:="", Forward:=True, _
MatchWildcards:=False, Wrap:=wdFindStop, MatchCase:=False) = True
Set frange = Selection.Range
ActiveDocument.Bookmarks.Add "Bookmark" & i, frange
i = i + 1
Selection.Collapse wdCollapseEnd
Loop
End With

End Sub





  #6   Report Post  
Posted to microsoft.public.word.docmanagement
Suzanne S. Barnhill Suzanne S. Barnhill is offline
external usenet poster
 
Posts: 33,624
Default Macro to Generate Bookmarks Based on Text Style

Well, I wasn't referring to the DM per se, just to the fact that if you use
Insert | Cross-reference or Insert | Hyperlink, you'll find the headings
already present, and the bookmarks are used by Word to generate an automatic
TOC. In the Document Map, however, you can choose how many heading levels to
view: switch to Outline view, select only Level 1, and the DM will follow
suit.

--
Suzanne S. Barnhill
Microsoft MVP (Word)
Words into Type
Fairhope, Alabama USA
http://word.mvps.org

"MattyP" wrote in message
...
I do (I'm assuming you're talking about the 'Document Map' toolbar button)
?
Only problem is, that 'catches' all the built-in heading styles (so the
list
is a bit longer and cumbersome to wade through !). Alternatively, is
there a
way to modify the Document Map feature, to only 'catch' certain built-in
heading styles (i.e., just Heading 1) ?

"Suzanne S. Barnhill" wrote:

You do know that the built-in heading styles are bookmarked automatically
by
Word?

--
Suzanne S. Barnhill
Microsoft MVP (Word)
Words into Type
Fairhope, Alabama USA
http://word.mvps.org

"MattyP" wrote in message
...
Hello -
A few days ago, I posted a question to see if it were possible (and
what
the
code might be) to create a macro that would autmatically add Bookmarks
to
a
Word document, based on text style (in this case, Heading 1's). I got
a
great reply (credit to the original responder !), but later realized it
would
definitely be helpful if I could tweak it a bit more.

The macro code originally provided is below. After running it, any
text
strings formatted as Heading 1 are automatically identified as
Bookmarks
(which is great). The catch is, these are named as "Bookmark 1",
"Bookmark
2", etc., rather than the being named the same as the actual text
string
itself. So, the million dollar question is this --- is there any way
to
modify the macro code below (or create a new one) that automatically
generates Bookmarks based on text style (i.e., Heading 1), and also
names
them the same as the text string ? Any guidance would be most
appreciated
!

- Matt

Here's the macro code I originally received (which works absolutely
great,
but does name the Bookmarks "generically" --- 1,2,3, etc.)..........

------------------------------------------
Sub Macro1()
'
' Macro1 Macro
' Macro recorded 3/8/2009 by MattyP
'
Selection.HomeKey wdStory
Selection.Find.Style = ActiveDocument.Styles("Heading 1")
Dim frange As Range
Dim i As Long
i = 1
With Selection.Find
Do While .Execute(FindText:="", Forward:=True, _
MatchWildcards:=False, Wrap:=wdFindStop, MatchCase:=False) = True
Set frange = Selection.Range
ActiveDocument.Bookmarks.Add "Bookmark" & i, frange
i = i + 1
Selection.Collapse wdCollapseEnd
Loop
End With

End Sub




Reply
Thread Tools
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Finding text based on paragraph style - doesn't work? Paul Moloney Microsoft Word Help 6 October 16th 07 09:16 AM
Generate document based on another document with check boxes Ted Microsoft Word Help 4 July 27th 07 07:37 AM
Is it possible to generate footer based on data? MatthewTap Microsoft Word Help 3 January 3rd 06 06:37 AM
selecting text based on 'style' stumped Microsoft Word Help 2 February 2nd 05 05:23 AM
How do I create a style that will generate the word note? newshost.allthenewsgroups.com Tables 0 January 30th 05 07:17 PM


All times are GMT +1. The time now is 07:32 AM.

Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 Microsoft Office Word Forum - WordBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Word"