Solved

Word 2010 VBA macro debugging question:

Posted on 2013-10-28
2
831 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 500 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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Shortcuts in Word Just the other day I had a training for Microsoft and they wanted me to show how well the new Windows and Office behaved on a touch device, which by the way is great, but it was only then that I realized that using keyboard shortc…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

821 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