[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Word 2010 VBA macro debugging question:

Posted on 2013-10-28
2
Medium Priority
?
848 Views
Last Modified: 2013-11-04
Good afternoon, I have a macro that I have used for editing documents and I am running into an issue.  A part of the macro finds strikethrough text and then deletes it.  However, I get an error at the wrd.delete portion of this macro:

Private Sub RemoveStrikeThrough()
    Dim wrd As Range
    Dim rng As Word.Range
   
    For Each rng In ActiveDocument.StoryRanges 'get first story of each story type
        Do
            For Each wrd In rng.Words
                If wrd.Font.StrikeThrough = True Then
                    wrd.Delete
                End If
            Next wrd
            Set rng = rng.NextStoryRange 'get next story of this story type
        Loop Until rng Is Nothing
    Next rng
End Sub

Can anyone help me understand why this won't execute correctly?

Thanks,
Bevos
0
Comment
Question by:Bevos
2 Comments
 
LVL 16

Expert Comment

by:terencino
ID: 39607193
This modification works for me, can you test it in your situation?
Private Sub RemoveStrikeThrough()
    Dim wrd
    Dim rng As Range
   
    For Each rng In ActiveDocument.StoryRanges
            For Each wrd In rng.Words
                If wrd.Font.StrikeThrough = True Then
                    wrd.Delete
                End If
            Next wrd
    Next rng
End Sub

Open in new window

0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 2000 total points
ID: 39608147
You don't tell us what error you are getting, but there is always a danger when using the For Next construct if a member of the collection is deleted. The pointer to the next member is not always correct after the collection has been disturbed.

An alternative is to use indexing:
For Each i = 1 to rng.Words,Count
       Set wrd = rng.Words(i)

Open in new window


but this can get very slow with large document as the application has to count the words from the start of the range for each iteration of the loop.

In this case, I suggest that you use Find and Replace:
Private Sub RemoveStrikeThrough()
    Dim wrd As Range
    Dim rng As Word.Range
   
    For Each rng In ActiveDocument.StoryRanges 'get first story of each story type
        Do
            With rng.Find
                .MatchWholeWord = True
                .Font.StrikeThrough = True
                .Replacement.Text = ""
                .Execute Replace:=wdReplaceAll
            End With
            Set rng = rng.NextStoryRange 'get next story of this story type
        Loop Until rng Is Nothing
        Next rng
End Sub

Open in new window

0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

I would like to show you some basics you can do with Mailings in MS Word. It´s quite handy feature you can use for creating envelopes, labels, personalized letters etc. First question could be what is this feature good for? Mailing can really he…
Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses
Course of the Month19 days, 14 hours left to enroll

872 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