[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Word 2010 VBA macro debugging question:

Posted on 2013-10-28
2
Medium Priority
?
847 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 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

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.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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 …
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.
Suggested Courses

649 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