2 spaces to 1 space macro, Word 2003

A

Alan Stancliff

Hi All,

I am running Word 2003.

Right now, I have a macro that changes 2 spaces to 1 space in a
document, and I need to refine it a bit. I am posting this macro at the
end of this message.

The problems are two:

First, I need this macro to change spaces only in section 2 of a
3-section document, leaving things as they are in section 1 and section 3.

Second, I want this macro to change 2 or more consecutive spaces to 1
space. So if there are 3 or more spaces somewhere in section 2 of the
document, it changes them to 1 space.

Any Ideas?

Sub TwoSpaceToOne
Dim twospace As Range
Set twospace = ActiveDocument.Range
With twospace.Find
..ClearFormatting
..Replacement.ClearFormatting
..Text = " "
..Replacement.Text = " "
..Forward = True
..Wrap = wdFindContinue
..Format = False
..MatchWildcards = False
..Execute Replace:=wdReplaceAll
End With
end sub

Regards,

Alan
 
D

Doug Robbins - Word MVP

Use:

Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="[ ]{2,}", MatchWildcards:=True,
MatchCase:=True, Wrap:=wdFindStop, Forward:=True) = True
With Selection
If .Information(wdActiveEndSectionNumber) = 2 Then
.Text = " "
End If
End With
Loop
End With


--
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
 
M

macropod

Hi Alan,

You could use:

With ActiveDocument
If .Sections.Count > 1 Then
With .Sections(2).Range.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "[ ]{2,}"
.Replacement.Text = " "
.MatchWildcards = True
.MatchCase = True
.Wrap = wdFindStop
.Forward = True
.Execute Replace:=wdReplaceAll
End With
End If
End With

Note the inclusion of error checking to test whether the document has more than one Section.
 
A

Alan Stancliff

Thanks Macropod,

That works wonderfully well

Regards,

Alan

macropod said:
Hi Alan,

You could use:

With ActiveDocument
If .Sections.Count > 1 Then
With .Sections(2).Range.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "[ ]{2,}"
.Replacement.Text = " "
.MatchWildcards = True
.MatchCase = True
.Wrap = wdFindStop
.Forward = True
.Execute Replace:=wdReplaceAll
End With
End If
End With

Note the inclusion of error checking to test whether the document has more than one Section.

--
Cheers
macropod
[MVP - Microsoft Word]


Alan Stancliff said:
Hi All,

I am running Word 2003.

Right now, I have a macro that changes 2 spaces to 1 space in a
document, and I need to refine it a bit. I am posting this macro at the
end of this message.

The problems are two:

First, I need this macro to change spaces only in section 2 of a
3-section document, leaving things as they are in section 1 and section 3.

Second, I want this macro to change 2 or more consecutive spaces to 1
space. So if there are 3 or more spaces somewhere in section 2 of the
document, it changes them to 1 space.

Any Ideas?

Sub TwoSpaceToOne
Dim twospace As Range
Set twospace = ActiveDocument.Range
With twospace.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = " "
.Replacement.Text = " "
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchWildcards = False
.Execute Replace:=wdReplaceAll
End With
end sub

Regards,

Alan
 

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