?
Solved

Bookmark instances of user defined style

Posted on 2011-03-11
2
Medium Priority
?
588 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
This video walks the viewer through the process of creating an MLA formatted document, as well as a bibliography with citations.
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.

752 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