Replacing spaces with 1 space or a tab

T

Tonya Marshall

Copying and pasting text from a web page or an email frequently replaces
tabs with numerous spaces. Is there a way to replace > 1 space with a tab?
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Tonya Marshall > écrivait :
In this message, < Tonya Marshall > wrote:

|| Copying and pasting text from a web page or an email frequently replaces
|| tabs with numerous spaces. Is there a way to replace > 1 space with a
tab?
|| --
|| Tonya Marshall
|| tonz AT harborside DOT com

Try this:

'_______________________________________
Sub ReplaceSpacesByTabs()

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
'Find between 2 and 100 consecutive spaces
'Increase 100 to the maximum number of spaces you think
'you need to replace
.Text = " {2,100}"
.Replacement.Text = "^t"
.Forward = True
.Wrap = wdFindContinue
.Format = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

'To reset the browser
Application.Browser.Target = wdBrowsePage

End Sub
'_______________________________________

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
T

Tonya Marshall

Well, Jean-Guy,
I run the macro and nothing happens. Not even an error message.
The macro resembles other replacement macros that I have so the only
thing I can think of is perhaps the number of spaces is incorrectly
entered into the macro.
Thank you,
 
P

Peter Hewett

Hi Tonya Marshall

JG, must have skipped a line of code, I added it back so use this amended version:

Sub ReplaceSpacesByTabs()

With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
'Find between 2 and 100 consecutive spaces
'Increase 100 to the maximum number of spaces you think
'you need to replace
.Text = " {2,100}"
.Replacement.Text = "^t"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll

'To reset the browser
Application.Browser.Target = wdBrowsePage

End Sub

The above works only on the selected block of text, if you want to run it against the
entire document use this version:

Sub ReplaceSpacesByTabs()

With ActiveDocument.Content.Find
.ClearFormatting
.Replacement.ClearFormatting
'Find between 2 and 100 consecutive spaces
'Increase 100 to the maximum number of spaces you think
'you need to replace
.Text = " {2,100}"
.MatchWildcards = True
.Replacement.Text = "^t"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.Execute Replace:=wdReplaceAll
End With
'To reset the browser
Application.Browser.Target = wdBrowsePage

End Sub

HTH + Cheers - Peter
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Tonya Marshall > écrivait :
In this message, < Tonya Marshall > wrote:

|| Well, Jean-Guy,
|| I run the macro and nothing happens. Not even an error message.
|| The macro resembles other replacement macros that I have so the only
|| thing I can think of is perhaps the number of spaces is incorrectly
|| entered into the macro.
|| Thank you,

As Peter pointed out, I forgot to include the all important
.MatchWildcards = True
when doing wild card searches!

Sorry about that!
And as Peter said, it will work only on the selected text if some text is
selected, unless you add
Selection.Collapse
at the start.

That will teach me to post code before rushing out of the house to get the
kids at school!

Sorry again to have made you waste some time on this.
--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
T

Tonya Marshall

Thank you Jean-Guy and Peter,
It works perfectly.
Jean-Guy, you didn't make me waste time. I learned something new and
that's definitely not a waste of time.
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Tonya Marshall > écrivait :
In this message, < Tonya Marshall > wrote:

|| Thank you Jean-Guy and Peter,
|| It works perfectly.
|| Jean-Guy, you didn't make me waste time. I learned something new and
|| that's definitely not a waste of time.
||

You are too kind !

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
D

Doug Robbins - Word MVP

Why not just use

..Text = " {2,}"

which means "2 or more spaces". Then you do not need to set (and will not
be limited by) a maximum number. Not that 100 should not be enough <g>
--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
T

Tonya Marshall

Doug said:
Why not just use

.Text = " {2,}"

which means "2 or more spaces". Then you do not need to set (and will not
be limited by) a maximum number. Not that 100 should not be enough <g>

Well, you know, brevity is the soul of wit, and what that has to do with
this is not entirely clear to me. <g> Thank you very much, I like your
briefer version, Doug. I keep telling myself that I'll learn to
write macros if I get enough macros from others.
Tonya
 

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