Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

MS Word 2013-- Macro for deleting paragraphs that start with a certain word

Posted on 2013-12-21
5
Medium Priority
?
4,796 Views
Last Modified: 2013-12-24
I would like to create a Macro in MS Word, 2013 that, when run, deletes any line or paragraph that begins with the word “Delete.”  It would be extra nice if the macro was able to shift the remaining lines/paragraphs up so there’s huge blocks of empty space.  
In addition, I would like the Macro to delete the word “Keep” at the beginning of all remaining lines/paragraphs.  Attached is a sample of my report.
Does anyone have a code?  Thank you so much!

*It may be worth mentioning that these words “Delete” and “Keep” are actually Inserted Merge Fields from Mail Merger.
Help.docx
0
Comment
Question by:mdstalla
[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
5 Comments
 
LVL 27

Accepted Solution

by:
MacroShadow earned 2000 total points
ID: 39734330
Sub Demo()
    Dim oPara As Paragraph

    For Each oPara In ActiveDocument.Paragraphs
        If InStr(1, oPara.Range.Words(1).Text, "Delete") = 1 Then
            oPara.Range.Delete
            oPara.Range.Delete
        ElseIf InStr(1, oPara.Range.Words(1).Text, "Keep") = 1 Then
            oPara.Range.Words(1).Text = Replace(oPara.Range.Words(1).Text, "Keep ", "")
        End If
    Next
End Sub

Open in new window

0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 39734492
Note that you have posted the Mail Merge Main document, and not the result document, on which we suppose that you are working.

This can be done without VBA code with a couple of wildcard Find and Replace operations.

    Find: Delete*^13
    Replace:
and
    Find: Keep (*^13)
    Replace: \1

These operations could be put in a macro:
Sub DelParas()
    With ActiveDocument.Range.Find
        .MatchWildcards = True

        .Text = "Delete*^13"
        .Replacement.Text = ""
        .Execute Replace:=wdReplaceAll
        
        .Text = "Keep (*^13)"
        .Replacement.Text = "\1"
        .Execute Replace:=wdReplaceAll
    End With
End Sub

Open in new window

0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 39734884
mdstalla,

You have posted this question twice. If there are, as at the moment, no comments added, you can delete the other instance yourself, i.e.  without moderator intervention:
http://www.experts-exchange.com/Microsoft/Applications/Q_28323863.html
0
 
LVL 46

Expert Comment

by:aikimark
ID: 39735479
Although this code looks like MacroShadow's, it handles the case where the first 'word' in a paragraph was a space character and neither Keep nor Delete.

Algorithm: Iterate the words in each paragraph, stopping when the first non-trivial string is found.  If the first non-trivial word is "Keep", then delete that word. If the first non-trivial word is "Delete", then delete the entire paragraph.  After all paragraphs have been processed, eliminate extraneous paragraphs.
Public Sub Q_28323866()
    Dim pp As Paragraph
    Dim vItem As Variant
    For Each pp In ActiveDocument.Paragraphs
        For Each vItem In pp.Range.Words
            If Len(Trim(vItem)) <> 0 Then
                Select Case Trim(vItem)
                    Case "Keep"
                        vItem.Delete
                    Case "Delete"
                        pp.Range.Delete
                End Select
                Exit For
            End If
        Next
    Next
    ActiveDocument.Range.Find.Execute findtext:="^p^p^p", replacewith:="^p^p", Replace:=wdReplaceAll
End Sub

Open in new window

0
 

Author Closing Comment

by:mdstalla
ID: 39738036
Wow! Nice work-- this worked perfectly.  Thanks.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

604 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