Remove specific paragraph in word document

YasserKhalil
PlatinumLounger
Posts: 4931
Joined: 31 Aug 2016, 09:02

Remove specific paragraph in word document

Post by YasserKhalil »

Hello everyone

I have a word document and I am trying to clear specific paragraph not to delete (I mean to let the lines empty at this paragraph) .. if the paragraph ends with the string "None" ..
Or if possible to empty the paragraph depending on the first string "Notify us" >> that will be better for me

The code will be in excel where there is a code that open that document and do those changes

Thanks advanced for help
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 78633
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Remove specific paragraph in word document

Post by HansV »

Code: Select all

    Dim objRng As Range
    Set objRng = objDoc.Content
    If objRng.Find.Execute(FindText:="^pNotify us") Then
        Set objRng = objRng.Paragraphs(2).Range
        objRng.MoveEnd Count:=-1
        objRng.Delete
    End If
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4931
Joined: 31 Aug 2016, 09:02

Re: Remove specific paragraph in word document

Post by YasserKhalil »

Thanks a lot Mr. Hans
I got an error 'Argument not optional at the method Find at this line

Code: Select all

If objRng.Find.Execute
I am running the code from excel code not from the word application. So I guess this is the cause
And what's ^p refers to ?

User avatar
HansV
Administrator
Posts: 78633
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Remove specific paragraph in word document

Post by HansV »

Does it work if you change

Code: Select all

    Dim objRng As Range
to

Code: Select all

    Dim objRange As Object
Best wishes,
Hans

User avatar
HansV
Administrator
Posts: 78633
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Remove specific paragraph in word document

Post by HansV »

^p is the code that Word uses in the Find and Replace dialogs to represent a paragraph mark.
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4931
Joined: 31 Aug 2016, 09:02

Re: Remove specific paragraph in word document

Post by YasserKhalil »

Thank you very much .. That works well using Object instead of Range
Now last point: the code deletes the paragraph.. Is there to keep the lines empty instead? I mean to leave the two lines empty instead of completely delete the lines

User avatar
HansV
Administrator
Posts: 78633
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Remove specific paragraph in word document

Post by HansV »

That's a weird request, but try changing

Code: Select all

        objRng.Delete
to

Code: Select all

        objRng.Text = Chr(11)
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4931
Joined: 31 Aug 2016, 09:02

Re: Remove specific paragraph in word document

Post by YasserKhalil »

The idea is to keep the formatting of the whole page as it is ..
Thank you very much for awesome help my tutor
Best and kind regards

YasserKhalil
PlatinumLounger
Posts: 4931
Joined: 31 Aug 2016, 09:02

Re: Remove specific paragraph in word document

Post by YasserKhalil »

Hello Mr. Hans
What if I need to get rid of the space that is left empty after deleting the paragraph. I mean to delete the spacing lines and let only one space line between the previous and the next paragraph

User avatar
HansV
Administrator
Posts: 78633
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Remove specific paragraph in word document

Post by HansV »

Code: Select all

    Dim objRng As Object
    Set objRng = objDoc.Content
    If objRng.Find.Execute(FindText:="^pNotify us") Then
        Set objRng = objRng.Paragraphs(2).Range
        objRng.Delete
    End If
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4931
Joined: 31 Aug 2016, 09:02

Re: Remove specific paragraph in word document

Post by YasserKhalil »

So you do without this line

Code: Select all

objRng.MoveEnd Count:=-1
What does this line do exactly?
And what does the number 2 refers to after the Paragraphs in this line

Code: Select all

Set objRng = objRng.Paragraphs(2).Range

User avatar
HansV
Administrator
Posts: 78633
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Remove specific paragraph in word document

Post by HansV »

After objRng.Find.Execute(FindText:="^pNotify us"), the range objRng consists of the paragraph mark before "Notify us" and the text "Notify us". So this range contains parts of two paragraphs: the one before "Notify us" and the one containing "Notify us".
objRng.Paragraphs(2) is the second of these paragraphs, i.e. the one containing "Notify us".
objRng.MoveEnd Count:=-1 moves the end of the range -1 characters to the right, i.e. 1 character to the left. So when we delete the paragraph, its last character, the paragraph mark, is not removed.
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4931
Joined: 31 Aug 2016, 09:02

Re: Remove specific paragraph in word document

Post by YasserKhalil »

Thanks a lot for this great clarification. I appreciate that a lot
Kind Regards

YasserKhalil
PlatinumLounger
Posts: 4931
Joined: 31 Aug 2016, 09:02

Re: Remove specific paragraph in word document

Post by YasserKhalil »

Hello Mr. Hans

I am trying to delete specific line with two shapes .. the delete will be only if specific cell in excel is empty
Here's the sample document and snapshot of the desired line to be deleted
Untitled.png
How can this be achieved by code in the excel macro?
You do not have the required permissions to view the files attached to this post.