Solved

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

Posted on 2013-12-21
5
3,674 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 26

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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

Suggested Solutions

The System Center Operations Manager 2012, known as SCOM, is a part of the Microsoft system center product that provides the user with infrastructure monitoring and application performance monitoring. SCOM monitors:   Windows or UNIX/LinuxNetwo…
Technology opened people to different means of presenting information, but PowerPoint remains to be above competition. Know why PPT still works today.
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
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…

757 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

26 Experts available now in Live!

Get 1:1 Help Now