From: andreas on
Dear Experts:

below code is supposed to replace redundant spaces with tabstops just
within the selected paragraphs. Regrettably all the redundant spaces
within the whole document get worked on.

How has the code to be changed to work only on the selection (the
selection for example could be only just one paragraph).

Help is much appreciated. Thank you very much in advance.

Regards, Andreas



Sub SearchAndReplaceJustinSelectedParagraphs()

Dim intcount As Long
Dim myRange As Range


intcount = 0
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting

Set myRange = Selection.Range
myRange.Select

With myRange.Find
..Forward = True
.Wrap = wdFindContinue
.ClearFormatting
.Text = " {2;}"
.Replacement.Text = "^t"
.MatchWildcards = True
While .Execute(Replace:=wdReplaceOne)
myRange.Collapse wdCollapseEnd
intcount = intcount + 1
Wend
End With

MsgBox intcount
End Sub
From: Doug Robbins - Word MVP on
Use:

Dim myRange As Range
Dim intcount As Long
intcount = 0
Set myRange = Selection.Range
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:=" {2,}", Forward:=True, _
MatchWildcards:=True, Wrap:=wdFindContinue) = True
If Selection.Range.Start > myRange.Start And Selection.Range.End <
myRange.End Then
Selection.Text = vbTab
intcount = intcount + 1
Else
GoTo Message
End If
Loop
End With
Message:
MsgBox intcount


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com

"andreas" <andreas.hermle(a)gmx.de> wrote in message
news:f07e103a-b88a-4116-a61c-56c6ef3e1d4c(a)r27g2000yqn.googlegroups.com...
> Dear Experts:
>
> below code is supposed to replace redundant spaces with tabstops just
> within the selected paragraphs. Regrettably all the redundant spaces
> within the whole document get worked on.
>
> How has the code to be changed to work only on the selection (the
> selection for example could be only just one paragraph).
>
> Help is much appreciated. Thank you very much in advance.
>
> Regards, Andreas
>
>
>
> Sub SearchAndReplaceJustinSelectedParagraphs()
>
> Dim intcount As Long
> Dim myRange As Range
>
>
> intcount = 0
> Selection.Find.ClearFormatting
> Selection.Find.Replacement.ClearFormatting
>
> Set myRange = Selection.Range
> myRange.Select
>
> With myRange.Find
> .Forward = True
> .Wrap = wdFindContinue
> .ClearFormatting
> .Text = " {2;}"
> .Replacement.Text = "^t"
> .MatchWildcards = True
> While .Execute(Replace:=wdReplaceOne)
> myRange.Collapse wdCollapseEnd
> intcount = intcount + 1
> Wend
> End With
>
> MsgBox intcount
> End Sub

From: macropod on
Hi andreas,

Try:
Sub Andreas()
Dim i As Integer
With Selection
i = .Characters.Count
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = " {2,}"
.Replacement.Text = "^t"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
MsgBox i - .Characters.Count & " replacements made"
End With
End Sub

--
Cheers
macropod
[Microsoft MVP - Word]


"andreas" <andreas.hermle(a)gmx.de> wrote in message news:f07e103a-b88a-4116-a61c-56c6ef3e1d4c(a)r27g2000yqn.googlegroups.com...
> Dear Experts:
>
> below code is supposed to replace redundant spaces with tabstops just
> within the selected paragraphs. Regrettably all the redundant spaces
> within the whole document get worked on.
>
> How has the code to be changed to work only on the selection (the
> selection for example could be only just one paragraph).
>
> Help is much appreciated. Thank you very much in advance.
>
> Regards, Andreas
>
>
>
> Sub SearchAndReplaceJustinSelectedParagraphs()
>
> Dim intcount As Long
> Dim myRange As Range
>
>
> intcount = 0
> Selection.Find.ClearFormatting
> Selection.Find.Replacement.ClearFormatting
>
> Set myRange = Selection.Range
> myRange.Select
>
> With myRange.Find
> .Forward = True
> .Wrap = wdFindContinue
> .ClearFormatting
> .Text = " {2;}"
> .Replacement.Text = "^t"
> .MatchWildcards = True
> While .Execute(Replace:=wdReplaceOne)
> myRange.Collapse wdCollapseEnd
> intcount = intcount + 1
> Wend
> End With
>
> MsgBox intcount
> End Sub
From: Doug Robbins - Word MVP on
Hi Paul,

While that code does limit the changes to the selected text, the count of
the replacements made is a bit odd.

If there is one instance of two spaces, the count is 1
One instance of three spaces, the count is 2
and similar anomalies for other combinations of instances and spaces.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com

"macropod" <macropod(a)invalid.invalid> wrote in message
news:ujbJ2zh2KHA.2284(a)TK2MSFTNGP06.phx.gbl...
> Hi andreas,
>
> Try:
> Sub Andreas()
> Dim i As Integer
> With Selection
> i = .Characters.Count
> With .Find
> .ClearFormatting
> .Replacement.ClearFormatting
> .Text = " {2,}"
> .Replacement.Text = "^t"
> .Forward = True
> .Wrap = wdFindStop
> .Format = False
> .MatchCase = False
> .MatchWholeWord = False
> .MatchAllWordForms = False
> .MatchSoundsLike = False
> .MatchWildcards = True
> .Execute Replace:=wdReplaceAll
> End With
> MsgBox i - .Characters.Count & " replacements made"
> End With
> End Sub
>
> --
> Cheers
> macropod
> [Microsoft MVP - Word]
>
>
> "andreas" <andreas.hermle(a)gmx.de> wrote in message
> news:f07e103a-b88a-4116-a61c-56c6ef3e1d4c(a)r27g2000yqn.googlegroups.com...
>> Dear Experts:
>>
>> below code is supposed to replace redundant spaces with tabstops just
>> within the selected paragraphs. Regrettably all the redundant spaces
>> within the whole document get worked on.
>>
>> How has the code to be changed to work only on the selection (the
>> selection for example could be only just one paragraph).
>>
>> Help is much appreciated. Thank you very much in advance.
>>
>> Regards, Andreas
>>
>>
>>
>> Sub SearchAndReplaceJustinSelectedParagraphs()
>>
>> Dim intcount As Long
>> Dim myRange As Range
>>
>>
>> intcount = 0
>> Selection.Find.ClearFormatting
>> Selection.Find.Replacement.ClearFormatting
>>
>> Set myRange = Selection.Range
>> myRange.Select
>>
>> With myRange.Find
>> .Forward = True
>> .Wrap = wdFindContinue
>> .ClearFormatting
>> .Text = " {2;}"
>> .Replacement.Text = "^t"
>> .MatchWildcards = True
>> While .Execute(Replace:=wdReplaceOne)
>> myRange.Collapse wdCollapseEnd
>> intcount = intcount + 1
>> Wend
>> End With
>>
>> MsgBox intcount
>> End Sub

From: andreas on
On Apr 12, 8:50 am, "Doug Robbins - Word MVP"
<d...(a)REMOVECAPSmvps.org> wrote:
> Use:
>
> Dim myRange As Range
> Dim intcount As Long
> intcount = 0
> Set myRange = Selection.Range
> Selection.Find.ClearFormatting
> With Selection.Find
>     Do While .Execute(FindText:=" {2,}", Forward:=True, _
>         MatchWildcards:=True, Wrap:=wdFindContinue) = True
>         If Selection.Range.Start > myRange.Start And Selection.Range.End <
> myRange.End Then
>             Selection.Text = vbTab
>             intcount = intcount + 1
>         Else
>             GoTo Message
>         End If
>     Loop
> End With
> Message:
> MsgBox intcount
>
> --
> Hope this helps.
>
> Please reply to the newsgroup unless you wish to avail yourself of my
> services on a paid consulting basis.
>
> Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
>
> "andreas" <andreas.her...(a)gmx.de> wrote in message
>
> news:f07e103a-b88a-4116-a61c-56c6ef3e1d4c(a)r27g2000yqn.googlegroups.com...
>
>
>
> > Dear Experts:
>
> > below code is supposed to replace redundant spaces with tabstops just
> > within the selected paragraphs. Regrettably all the redundant spaces
> > within the whole document get worked on.
>
> > How has the code to be changed to work only on the selection (the
> > selection for example could be only just one paragraph).
>
> > Help is much appreciated. Thank you very much in advance.
>
> > Regards, Andreas
>
> > Sub SearchAndReplaceJustinSelectedParagraphs()
>
> > Dim intcount As Long
> > Dim myRange As Range
>
> > intcount = 0
> > Selection.Find.ClearFormatting
> > Selection.Find.Replacement.ClearFormatting
>
> > Set myRange = Selection.Range
> > myRange.Select
>
> > With myRange.Find
> > .Forward = True
> >   .Wrap = wdFindContinue
> >   .ClearFormatting
> >   .Text = " {2;}"
> >   .Replacement.Text = "^t"
> >   .MatchWildcards = True
> >   While .Execute(Replace:=wdReplaceOne)
> >   myRange.Collapse wdCollapseEnd
> >      intcount = intcount + 1
> >      Wend
> > End With
>
> > MsgBox intcount
> > End Sub- Hide quoted text -
>
> - Show quoted text -

Hi Doug,

great, Exactly what I wanted. And thank you very much for your
insights into replacing redundant spacing.

There is one thing I would like to ask you. I would like to preserve
the original selection for further operations in the same macro.

How is this achieved?

Help is much appreciated. Thank you very much in advance. Regards,
Andreas