Solved

Bookmark instances of user defined style

Posted on 2011-03-11
2
580 Views
Last Modified: 2012-08-14
Dear Experts:

Below macro is supposed to bookmark paragraphs a certain way where a user-defined style (user-style) has been applied.

The code does not find any paragraph where the user-defined style (user-style) has been applied. What is wrong with this code?

Help is much appreciated. Thank you very much in advance.

Regards, Andreas
Sub BookmarkInstancesOfStyle

Dim strLanguages As Variant
Dim i As Long
Dim frange As Range
 i = 0

Selection.HomeKey wdStory
Selection.Find.Style = ActiveDocument.Styles("user-style")
 
 strLanguages = Array("de", "en", "es", "fr", "it")
 With Selection.Find
 Do While .Execute(FindText:="", Forward:=True, _
 MatchWildcards:=False, Wrap:=wdFindStop, MatchCase:=False) = True
 Set frange = Selection.Range
 ActiveDocument.Bookmarks.Add "MainTOC_" & strLanguages(i), frange
 i = i + 1
 Selection.Collapse wdCollapseEnd
 Loop
 End With
End Sub

Open in new window

0
Comment
Question by:AndreasHermle
2 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 35112096
Hi Andreas,

Your code seems to work for me, but here is a slightly re-arranged version anyway.
Sub BookmarkInstancesOfStyle2()
    Dim strLanguages As Variant
    Dim i As Long
    Dim frange As Range
    
    strLanguages = Array("de", "en", "es", "fr", "it")
    Set frange = ActiveDocument.Range
    With frange.Find
        .Wrap = wdFindStop
        .Style = "user-style"
        Do While .Execute
            ActiveDocument.Bookmarks.Add "MainTOC_" & strLanguages(i), frange
            i = i + 1
            frange.Collapse wdCollapseEnd
            frange.End = ActiveDocument.Range.End
        Loop
    End With
End Sub

Open in new window

0
 

Author Closing Comment

by:AndreasHermle
ID: 35115512
Hi Graham,

great, your new code works for me just fine. Thank you so much for your professional help. Have a nice weekend. Regards, Andreas
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
This video teaches the viewer how to align pictures around text while keeping the text properly aligned in the document.
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now