?
Solved

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

Posted on 2013-12-21
5
Medium Priority
?
4,577 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Ever wonder what it's like to get hit by ransomware? "Tom" gives you all the dirty details first-hand – and conveys the hard lessons his company learned in the aftermath.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
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 …

752 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