Solved

Word 2010 VBA macro debugging question:

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

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.
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

733 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