How JustAnswer Works:
  • Ask an Expert
    Experts are full of valuable knowledge and are ready to help with any question. Credentials confirmed by a Fortune 500 verification firm.
  • Get a Professional Answer
    Via email, text message, or notification as you wait on our site.
    Ask follow up questions if you need to.
  • 100% Satisfaction Guarantee
    Rate the answer you receive.
Ask Greg Maxey Your Own Question
Greg Maxey
Greg Maxey, Consultant\Microsoft Word Expert
Category: General
Satisfied Customers: 62
Experience:  Microsoft Word Most Valuable Professional (6 years)
Type Your Question Here...
Greg Maxey is online now
A new question is answered every 9 seconds

Ask a Microsoft Word Question Using VBA, I would like to

Customer Question

Ask a Microsoft Word Question
>Using VBA, I would like to delete paragraphs based on the first word.
Submitted: 6 years ago.
Category: General
Expert:  Greg Maxey replied 6 years ago.

A "word" in "Word" can be a pernickety thing. Provides you have something like this:


This is paragraph one

Word1 this is paragraph two

This is paragraph three

Word2 this is paragraph four


Then something like this should work:

Sub DeleteBasedOnFirstWord()

Dim oPar as Word.Paragraph

For Each oPar in ActiveDocument.Range.Paragraphs

Select Case Trim(oPar.Range.Words(1))

Case "Word1", "Word2"


Case Else

'Do nothing

End Select

Next oPar

End Sub


Replace "Word1" and "Word2" with the actual words.



Greg Maxey and 39 other General Specialists are ready to help you
Customer: replied 6 years ago.
<p>It is not working. I set up a string variable to capture the first word in the paragraph. The words I am looking for all have colons ":", and the variable is not capturing the colon. Do I need to escape the colon somehow?</p><p>Actually, I think I discovered the problem. The punctuation is not part of the word object. I modified your code as follows:</p><p>Sub DeleteBasedOnFirstWord()</p><p>    Dim oPar As Word.Paragraph<br/>    Dim strFirstWord As String<br/>    <br/>    For Each oPar In ActiveDocument.Range.Paragraphs<br/>        strFirstWord = Trim(oPar.Range.Words(1)) & ":"<br/>    <br/>        Select Case strFirstWord<br/>        <br/>            Case "From:", "Sent:", "To:", "Cc:"<br/>            <br/>            oPar.Range.Delete<br/>            <br/>            Case Else<br/>            <br/>            'Do nothing<br/>        <br/>        End Select<br/>    <br/>    Next oPar</p><p>End Sub</p><p>It then worked. Thank You.</p>
Expert:  Greg Maxey replied 6 years ago.
Good. Glad your issue is resoloved.