Solved

Bookmark instances of user defined style

Posted on 2011-03-11
2
586 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 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

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

Do you ever need to create a 20 page Word document for some testing purpose? Are you tired of copying & pasting old boring "lorem ipsum" text over and over again, increasing font size and line space in order to make the document 20+ pages long? Look…
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…
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
Suggested Courses

737 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