Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Bookmark instances of user defined style

Posted on 2011-03-11
2
Medium Priority
?
594 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 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The Selection object is designed for user interaction. It has a Range property, so it can be used in most places that a Range object can. Recorded macros must use the Selection because they are simply copying what the user is doing. A Range prope…
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.
Suggested Courses

810 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