Solved

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

Posted on 2013-12-21
5
4,150 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
5 Comments
 
LVL 27

Accepted Solution

by:
MacroShadow earned 500 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 45

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
This article will show you how to use shortcut menus in the Access run-time environment.
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
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.

730 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