Solved

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

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

No matter the version of Windows you are using, you may have some problems with Windows Search running too slow or possibly not running at all. Before jumping into how you can solve this issue, just know there are many other viable alternative deskt…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
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 …

777 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