Deleting text within a paragraph.

M

Matt Reed

I am trying to delete text in each of several paragraphs, starting at the
end of the Bolded text and deleting all text until the first tab.

This works find as long as each paragraph has both Bolded text and a tab. My
problem comes in when one of those items is not present in the same
paragraph. Then I end up deleting text in parts of more then one paragraph.

Sample - using CAPITAL letters for bolded text, the word 'tab' follows an
actual tab.

START of line and then it continues until tab, then more stuff and
end of paragraph.

I am trying to delete everything after the word START and before the tab.



Any help would be appreciated.
Matt
PS I'm using WordXP and my current code is bellow.

Sub CleanPara()
Dim objPar As Paragraph
For Each objPar In ActiveDocument.Paragraphs
Selection.Find.ClearFormatting
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Font.Bold = True
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Collapse Direction:=wdCollapseEnd

Selection.Extend
Selection.Find.ClearFormatting
With Selection.Find
.Text = "^t"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.Delete Unit:=wdCharacter, Count:=1

Next objPar
End Sub
 
H

Helmut Weber

Hi Matt,
kind of tricky, just have a look at it.
Impossible here to explain all that is to it.
---
Sub Makro1()
Dim rDcm As Range
Dim oPrg As Paragraph
Dim oRng As Range
Dim rTmp As Range
Set rDcm = ActiveDocument.Range
Dim i As Integer
ResetSearch
Set rTmp = Selection.Range
For i = 1 To rDcm.Paragraphs.Count
Set oRng = rDcm.Paragraphs(i).Range.Duplicate
With oRng.Find
.Text = ""
.Font.Bold = True
.Wrap = wdFindStop
If .Execute Then
rTmp.start = oRng.End
Else
MsgBox "no bold text in Paragraph " & i
GoTo continue
End If
End With
oRng.start = rTmp.start
oRng.End = oRng.Paragraphs(1).Range.End
With oRng.Find
.Text = "^t"
.Format = False
If .Execute Then
rTmp.End = oRng.End - 1
rTmp.Select ' for testing
' rTmp.Delete up to you !!!
Else
MsgBox "no tab in paragraph " & i
End If
End With
continue:
Next
ResetSearch
End Sub
'---
Public Sub ResetSearch()
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
End Sub
 
M

Matt Reed

Wow! Thanks Helmut.
Going to take me a little while to understand what it is actually doing, but
it looks like it will work for my problem.

Thanks again.

Matt

Hi Matt,
kind of tricky, just have a look at it.
Impossible here to explain all that is to it.
---
Sub Makro1()
Dim rDcm As Range
Dim oPrg As Paragraph
Dim oRng As Range
Dim rTmp As Range
Set rDcm = ActiveDocument.Range
Dim i As Integer
ResetSearch
Set rTmp = Selection.Range
For i = 1 To rDcm.Paragraphs.Count
Set oRng = rDcm.Paragraphs(i).Range.Duplicate
With oRng.Find
.Text = ""
.Font.Bold = True
.Wrap = wdFindStop
If .Execute Then
rTmp.start = oRng.End
Else
MsgBox "no bold text in Paragraph " & i
GoTo continue
End If
End With
oRng.start = rTmp.start
oRng.End = oRng.Paragraphs(1).Range.End
With oRng.Find
.Text = "^t"
.Format = False
If .Execute Then
rTmp.End = oRng.End - 1
rTmp.Select ' for testing
' rTmp.Delete up to you !!!
Else
MsgBox "no tab in paragraph " & i
End If
End With
continue:
Next
ResetSearch
End Sub
'---
Public Sub ResetSearch()
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
End Sub
 
H

Helmut Weber

Hi Matt,
out of the office, there is a bit more time for explaining.
I use ranges a lot here. What i am doing is,
loop through all paragraphs in a doc,
search for bold text in a copy of the paragraphs range _only_,
that is what .Wrap = wdFindStop is for.
If found, I remember the position of the end of the found text,
set a temporary range from there til the end of the paragraph,
search that range _only_ for a tab, and
if found, redimension the temporary range
so that it spans from after bold text to right before
the tab.
Improvements possible, probably.
---
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
Ö

Örjan Skoglösa

Hi Helmut,

sorry to be thick, but why do you use a duplicate of the range?

TIA
Örjan
 
H

Helmut Weber

Hi Örjan,
I've experimented a lot with this, including all kinds of
selections, ranges and duplicates, trying real hard to use
range instead of selection. So, unfortunately, the range-duplicate
stayed in the code, as it doesn't throw an error.
But, as you've found out, it is of no use.
Generally speaking, I think, posting working solutions is
alright, as long as one doesn't exclude, that improvements
are possible. For my part, that goes without saying,
and improvements are always welcome.
 
M

Matt Reed

Thanks for all your help Helmut.
I am now using parts of this code in a larger project.
First of all if it all works out would you be interested in see the final
code?
Secondly, I am starting a new thread asking about how to find the specific
"TrailingCharacter" for a Outlinenumberd paragraph or style.
Since that is far from this topic I'll open another thread.

Thanks again.

Matt
PS In reading about .duplicate I understand that if I change the range on
..duplicate the original range does not change. If I copy a range to another
name and change the range of the copy the range of the original is also
change. So, using a .duplicate is exactly what is needed in this case. Or at
least that is the way it reads to me.

Matt




Hi Örjan,
I've experimented a lot with this, including all kinds of
selections, ranges and duplicates, trying real hard to use
range instead of selection. So, unfortunately, the range-duplicate
stayed in the code, as it doesn't throw an error.
But, as you've found out, it is of no use.
Generally speaking, I think, posting working solutions is
alright, as long as one doesn't exclude, that improvements
are possible. For my part, that goes without saying,
and improvements are always welcome.
 
H

Helmut Weber

Hi Matt,
I am always eager to learn. If you want to send me your
final code plus a sample doc, i'd appreciate it.
As far as finding out which listgallery and which
listtemplate the selection is in, I am completely lost.
For the trailing string this could help:
Sub Makro5()
MsgBox Selection.Range.ListParagraphs(1).Range.ListFormat.ListString
End Sub
---
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top