![]() |
#1
|
||||
|
||||
![]()
I want to perform following task
1. find string "string" 2. for all find, on which paragraph number it was found and write some text file of the same if possible. Msgbox is also ok 3. delete 2 paragraph above and 2 paragraph below the found string |
#2
|
||||
|
||||
![]()
Update
3. delete 2 paragraph above and 2 paragraph below the found string and paragraph found |
#3
|
||||
|
||||
![]()
Is it not possible to find on which paragraph the found string are in word?
|
#4
|
||||
|
||||
![]()
Maybe something like the following, but there are lots of potential unknowns. e.g. what about multiple occurrences of the string? What if the string is found in the paragraphs being deleted?
Code:
Sub FindString() Const strText As String = "string" Dim orng As Range Dim oPara As Range Set orng = ActiveDocument.Range With orng.Find Do While .Execute(strText) Set oPara = orng.Paragraphs(1).Range 'The paragraph with the string If Not oPara.Start = ActiveDocument.Range.Start Then oPara.Previous.Paragraphs(1).Range.Delete End If If Not oPara.Start = ActiveDocument.Range.Start Then oPara.Previous.Paragraphs(1).Range.Delete End If If Not oPara.End = ActiveDocument.Range.End Then oPara.Next.Paragraphs(1).Range.Delete End If If Not oPara.End = ActiveDocument.Range.End Then oPara.Next.Paragraphs(1).Range.Delete End If Exit Do ' Find only the first occurrence Loop End With lbl_Exit: Exit Sub End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#5
|
||||
|
||||
![]()
My doc have multiple occurrences of string but will never be part of paragraphs being deleted.
The question still remains how to get paragraph numbers of each strings that are found. I want to perform this operation for all the string before deleting the paragraphs. Thanks for reply, as this will surely help. I lost track in between. I will try and get back. |
#6
|
||||
|
||||
![]()
Works perfectly.
The question still remains how to get paragraph numbers of each strings that are found. I want to perform this operation for all the string before deleting the paragraphs. |
![]() |
Tags |
word vba |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
boutells | Word | 2 | 08-17-2016 12:18 PM |
Display paragraph of text based on value selected in combo | WordWaza | Word | 0 | 08-09-2013 06:30 AM |
![]() |
Helix86 | Word | 5 | 08-07-2013 10:48 PM |
![]() |
machineworks | Word | 1 | 08-03-2012 08:51 PM |
![]() |
Jamal NUMAN | Word | 2 | 07-12-2011 03:12 AM |