Remove specific paragraph in word document
-
- PlatinumLounger
- Posts: 4931
- Joined: 31 Aug 2016, 09:02
Remove specific paragraph in word document
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
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.
-
- Administrator
- Posts: 78633
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Remove specific paragraph in word document
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
Hans
-
- PlatinumLounger
- Posts: 4931
- Joined: 31 Aug 2016, 09:02
Re: Remove specific paragraph in word document
Thanks a lot Mr. Hans
I got an error 'Argument not optional at the method Find at this line
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 ?
I got an error 'Argument not optional at the method Find at this line
Code: Select all
If objRng.Find.Execute
And what's ^p refers to ?
-
- Administrator
- Posts: 78633
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Remove specific paragraph in word document
Does it work if you change
to
Code: Select all
Dim objRng As Range
Code: Select all
Dim objRange As Object
Best wishes,
Hans
Hans
-
- Administrator
- Posts: 78633
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Remove specific paragraph in word document
^p is the code that Word uses in the Find and Replace dialogs to represent a paragraph mark.
Best wishes,
Hans
Hans
-
- PlatinumLounger
- Posts: 4931
- Joined: 31 Aug 2016, 09:02
Re: Remove specific paragraph in word document
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
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
-
- Administrator
- Posts: 78633
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Remove specific paragraph in word document
That's a weird request, but try changing
to
Code: Select all
objRng.Delete
Code: Select all
objRng.Text = Chr(11)
Best wishes,
Hans
Hans
-
- PlatinumLounger
- Posts: 4931
- Joined: 31 Aug 2016, 09:02
Re: Remove specific paragraph in word document
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
Thank you very much for awesome help my tutor
Best and kind regards
-
- PlatinumLounger
- Posts: 4931
- Joined: 31 Aug 2016, 09:02
Re: Remove specific paragraph in word document
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
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
-
- Administrator
- Posts: 78633
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Remove specific paragraph in word document
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
Hans
-
- PlatinumLounger
- Posts: 4931
- Joined: 31 Aug 2016, 09:02
Re: Remove specific paragraph in word document
So you do without this line
What does this line do exactly?
And what does the number 2 refers to after the Paragraphs in this line
Code: Select all
objRng.MoveEnd Count:=-1
And what does the number 2 refers to after the Paragraphs in this line
Code: Select all
Set objRng = objRng.Paragraphs(2).Range
-
- Administrator
- Posts: 78633
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Remove specific paragraph in word document
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.
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
Hans
-
- PlatinumLounger
- Posts: 4931
- Joined: 31 Aug 2016, 09:02
Re: Remove specific paragraph in word document
Thanks a lot for this great clarification. I appreciate that a lot
Kind Regards
Kind Regards
-
- PlatinumLounger
- Posts: 4931
- Joined: 31 Aug 2016, 09:02
Re: Remove specific paragraph in word document
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 How can this be achieved by code in the excel macro?
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 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.