Solved

Word 2010 VBA macro debugging question:

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I'm writing to share my clumsy experience in using this elegant tool so you can avoid every stupid mistake I made. (I leave it to the authorities to decide if this deserves a place in the Knowledge archives.)  Now that I am on the other side of my l…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now