Macro to delete certain word in word document if marked as Yes in excel sheet

siddo

Board Regular
Joined
May 26, 2020
Messages
106
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hi, I'm trying to figure out a macro which can do the following.

Currently I'm using mail merge function to population fields from excel to word, however I have to pit stop now because for one filed in excel I have a list which has drop down values as (Yes/No). I want macro to delete specific paragraph in word if the value is Yes and if the value is No - I want the macro to do nothing.

Is it possible ?
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Hi siddo. Your request is not very clear. It's hard to tell which paragraphs U want to delete? What does the yes/no represent in the Word doc... specific words/terms in the Word document, paragraph numbers or something else? Anyways, here's a basic outline for looping through a Word doc paragraphs, finding some word ("Lastword" in this eg.) and deleting the paragraph that contains the searched word. U will need to adjust the file path to suit and change the searched for term as needed. Please keep a copy of your Word document before running the code. HTH. Dave
Code:
Dim oPara As Object
Set WdApp = CreateObject("Word.Application")
WdApp.Documents.Open Filename:="C:\Foldername\FileNm.doc", ReadOnly:=False
WdApp.ActiveDocument.Select
'check each para
For Each oPara In WdApp.ActiveDocument.Paragraphs
'if not blank para
If oPara.Range.Text <> Chr(13) Then
'if not blank para, find "Lastword" in para
With oPara.Range.Find
    .Text = "Lastword"
    .Forward = True
    .Execute
    If .found = True Then
    oPara.Range.Delete
    End If
End With
End If
Next oPara
WdApp.ActiveDocument.Close savechanges:=True
WdApp.Quit
Set WdApp = Nothing
 
Upvote 0
Solution

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,631
Latest member
a_potato

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top