1, 000. 00

F

Francis Hookham

I use a macro in Word which among other things 'tidies up' double spaces and
places where there should not be a space or a comma - it also makes sure
there is a space or a comma where there should be one - here is part of it

Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = " ."
.Replacement.Text = "."
End With
Selection.Find.Execute Replace:=wdReplaceAll

Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = " ,"
.Replacement.Text = ","
End With
Selection.Find.Execute Replace:=wdReplaceAll

Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "."
.Replacement.Text = ". "
End With
Selection.Find.Execute Replace:=wdReplaceAll

Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ","
.Replacement.Text = ", "
End With
Selection.Find.Execute Replace:=wdReplaceAll

The trouble is it puts a space after the comma of thousands and the period
of
decimals in 1, 000. 00 which should be 1,000.00

Can you help by showing me how to recognise a numeral as the second
character to the right of any commas and stops and, if there is, deleting
the intervening space

(I suppose there is a slight possibility that a sentence might start with a
numeral but I can live with that)

Thanks

Francis Hookham
 
P

Paul Berkowitz

Replace your last two Find/Replace with these two. The 'Find' entry's
MatchWildcards Property in VB Editor Help tells you that .MatchWildcards =
True is the same as "Use Wildcards" in the UI's Find/Replace dialog, and the
regular Word Help tells you how to use wildcrads (though it was awfully
tricky to figure this one out). I've made the 'With' blocks inclusive ti
save repeating Selection.Find over and over too. (You can do the same with
the first two blocks.)


With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "(.)([!0-9])"
.MatchWildcards = True
.Replacement.Text = "\1 \2"
.Execute Replace:=wdReplaceAll
End With

With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "(,)([!0-9])"
.MatchWildcards = True
.Replacement.Text = "\1 \2"
.Execute Replace:=wdReplaceAll
End With


--
Paul Berkowitz
MVP Entourage
Entourage FAQ Page: <http://www.entourage.mvps.org/toc.html>
AppleScripts for Entourage: <http://macscripter.net/scriptbuilders/>

Please "Reply To Newsgroup" to reply to this message. Emails will be
ignored.

PLEASE always state which version of Entourage you are using - 2001 or X.
It's often impossible to answer your questions otherwise.
 
F

Francis Hookham

Many thanks Paul for

Replace your last two Find/Replace with these two. The 'Find' entry's
Match Wildcards Property in VB Editor Help tells you that. Match Wildcards =
True is the same as "Use Wildcards" in the UI's Find/Replace dialog, and the
regular Word Help tells you how to use wildcards (though it was awfully
tricky to figure this one out). I've made the 'With' blocks inclusive to
save repeating Selection. Find over and over too. (You can do the same with
the first two blocks.)

That works well in the macro reproduced below

It is the start of a much longer macro for correcting copy for a club
magazine

It will remove double spaces and many other typos of that sort

I am having difficulty with characters like ( ) ? ! where I need to
remove/add spaces before/after, as the case maybe

In the macro below I have simply repeated the remove double spaces section
several times but I wonder if there is a better way

(Incidentally, I am writing this on the Mac (Office Word 2001) but I shall
want it to run universally on PC Word as well

Sub TidyUp()
Selection.HomeKey Unit:=wdStory

'Remove double spaces
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = " "
.Replacement.Text = " "
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.Execute Replace:=wdReplaceAll

'Remove space before stops
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = " ."
.Replacement.Text = "."
End With
Selection.Find.Execute Replace:=wdReplaceAll

'Remove space after commas
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ", "
.Replacement.Text = ","
End With
Selection.Find.Execute Replace:=wdReplaceAll

'Remove space before commas
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = " ,"
.Replacement.Text = ","
End With
Selection.Find.Execute Replace:=wdReplaceAll

'Remove space after stops
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ". "
.Replacement.Text = "."
End With
Selection.Find.Execute Replace:=wdReplaceAll

'Add space after sentence stop but not in #,##0.00
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "(.)([!0-9])"
.MatchWildcards = True
.Replacement.Text = "\1 \2"
.Execute Replace:=wdReplaceAll
End With

'Add space after comma in sentence but not in #,##0.00
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "(,)([!0-9])"
.MatchWildcards = True
.Replacement.Text = "\1 \2"
.Execute Replace:=wdReplaceAll
End With
End Sub
 
J

John McGhie [MVP - Word]

Hi Francis:

There comes a time when you need to make a choice between Find/Replace and
parsing the line in VBA and using sophisticated string manipulation.

To get out of your immediate problem you can do this:

With Selection.Find
.Text = " "
.Replacement.Text = " "
End With

Dim boolIsFound as Boolean
boolIsFound = True
While boolIsFound = true
boolIsFound = Selection.Find.Execute Replace:=wdReplaceAll
WEnd

Just check that on your compiler: I wrote that off the top of my head. But
what happens there is that the Execute method returns True if it made any
changes and false if it didn't. So that will iterate the Execute until no
more changes occur.

Let me know if it chucks errors and I will have a proper look at it when I
get home.

You want to avoid sophisticated string parsing logic if you can because it
makes for complex code that can be rather slow to execute. I would
encourage your authors to make greater use of the Grammar checker to spot
some of these things.

In a professional workplace, I find that if you get into the habit of
returning submitted documents that have errors marked by the spelling or
grammar checker, with a note telling the author to fix it, Authors very
quickly clean up their act :)

Hope this helps

from "Francis said:
Many thanks Paul for

Replace your last two Find/Replace with these two. The 'Find' entry's
Match Wildcards Property in VB Editor Help tells you that. Match Wildcards =
True is the same as "Use Wildcards" in the UI's Find/Replace dialog, and the
regular Word Help tells you how to use wildcards (though it was awfully
tricky to figure this one out). I've made the 'With' blocks inclusive to
save repeating Selection. Find over and over too. (You can do the same with
the first two blocks.)

That works well in the macro reproduced below

It is the start of a much longer macro for correcting copy for a club
magazine

It will remove double spaces and many other typos of that sort

I am having difficulty with characters like ( ) ? ! where I need to
remove/add spaces before/after, as the case maybe

In the macro below I have simply repeated the remove double spaces section
several times but I wonder if there is a better way

(Incidentally, I am writing this on the Mac (Office Word 2001) but I shall
want it to run universally on PC Word as well

Sub TidyUp()
Selection.HomeKey Unit:=wdStory

'Remove double spaces
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = " "
.Replacement.Text = " "
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.Execute Replace:=wdReplaceAll

'Remove space before stops
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = " ."
.Replacement.Text = "."
End With
Selection.Find.Execute Replace:=wdReplaceAll

'Remove space after commas
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ", "
.Replacement.Text = ","
End With
Selection.Find.Execute Replace:=wdReplaceAll

'Remove space before commas
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = " ,"
.Replacement.Text = ","
End With
Selection.Find.Execute Replace:=wdReplaceAll

'Remove space after stops
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ". "
.Replacement.Text = "."
End With
Selection.Find.Execute Replace:=wdReplaceAll

'Add space after sentence stop but not in #,##0.00
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "(.)([!0-9])"
.MatchWildcards = True
.Replacement.Text = "\1 \2"
.Execute Replace:=wdReplaceAll
End With

'Add space after comma in sentence but not in #,##0.00
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "(,)([!0-9])"
.MatchWildcards = True
.Replacement.Text = "\1 \2"
.Execute Replace:=wdReplaceAll
End With
End Sub

--

Please respond only to the newsgroup to preserve the thread.

John McGhie, Consultant Technical Writer,
McGhie Information Engineering Pty Ltd
Sydney, Australia. GMT + 10 Hrs
+61 4 1209 1410, mailto:[email protected]
 
F

Francis Hookham

Many thanks John McGhie but

boolIsFound = Selection.Find.Execute Replace:=wdReplaceAll

produces "Expected: End of statement" in red

Can you also help me with the other part of my problem which is, how do I
replace/add spaces round special characters like "?", "(", ")", "!" as
appropriate - these characters are not "findable" in the normal way

Many thanks

Francis

Hi Francis:

There comes a time when you need to make a choice between Find/Replace and
parsing the line in VBA and using sophisticated string manipulation.

To get out of your immediate problem you can do this:

With Selection.Find
.Text = " "
.Replacement.Text = " "
End With

Dim boolIsFound as Boolean
boolIsFound = True
While boolIsFound = true
boolIsFound = Selection.Find.Execute Replace:=wdReplaceAll
WEnd

Just check that on your compiler: I wrote that off the top of my head. But
what happens there is that the Execute method returns True if it made any
changes and false if it didn't. So that will iterate the Execute until no
more changes occur.

Let me know if it chucks errors and I will have a proper look at it when I
get home.

You want to avoid sophisticated string parsing logic if you can because it
makes for complex code that can be rather slow to execute. I would
encourage your authors to make greater use of the Grammar checker to spot
some of these things.

In a professional workplace, I find that if you get into the habit of
returning submitted documents that have errors marked by the spelling or
grammar checker, with a note telling the author to fix it, Authors very
quickly clean up their act :)

Hope this helps

from "Francis said:
Many thanks Paul for

Replace your last two Find/Replace with these two. The 'Find' entry's
Match Wildcards Property in VB Editor Help tells you that. Match Wildcards =
True is the same as "Use Wildcards" in the UI's Find/Replace dialog, and the
regular Word Help tells you how to use wildcards (though it was awfully
tricky to figure this one out). I've made the 'With' blocks inclusive to
save repeating Selection. Find over and over too. (You can do the same with
the first two blocks.)

That works well in the macro reproduced below

It is the start of a much longer macro for correcting copy for a club
magazine

It will remove double spaces and many other typos of that sort

I am having difficulty with characters like ( ) ? ! where I need to
remove/add spaces before/after, as the case maybe

In the macro below I have simply repeated the remove double spaces section
several times but I wonder if there is a better way

(Incidentally, I am writing this on the Mac (Office Word 2001) but I shall
want it to run universally on PC Word as well

Sub TidyUp()
Selection.HomeKey Unit:=wdStory

'Remove double spaces
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = " "
.Replacement.Text = " "
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.Execute Replace:=wdReplaceAll

'Remove space before stops
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = " ."
.Replacement.Text = "."
End With
Selection.Find.Execute Replace:=wdReplaceAll

'Remove space after commas
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ", "
.Replacement.Text = ","
End With
Selection.Find.Execute Replace:=wdReplaceAll

'Remove space before commas
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = " ,"
.Replacement.Text = ","
End With
Selection.Find.Execute Replace:=wdReplaceAll

'Remove space after stops
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ". "
.Replacement.Text = "."
End With
Selection.Find.Execute Replace:=wdReplaceAll

'Add space after sentence stop but not in #,##0.00
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "(.)([!0-9])"
.MatchWildcards = True
.Replacement.Text = "\1 \2"
.Execute Replace:=wdReplaceAll
End With

'Add space after comma in sentence but not in #,##0.00
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "(,)([!0-9])"
.MatchWildcards = True
.Replacement.Text = "\1 \2"
.Execute Replace:=wdReplaceAll
End With
End Sub

--

Please respond only to the newsgroup to preserve the thread.

John McGhie, Consultant Technical Writer,
McGhie Information Engineering Pty Ltd
Sydney, Australia. GMT + 10 Hrs
+61 4 1209 1410, mailto:[email protected]
 
F

Francis Hookham

Hi John McGhie - in my posting Friday, 23 April 2004 6:43 pm I was still
whittling on about "?", "(", ")", "!" - the answer is to uncheck Wild cards

The one problem which remains is how to remove multiple spaces rather than
simply repeating removing double spaces several time hoping they have all
been trapped - do I recall "white space" meaning multiple spaces?

Your help again please

Thanks

Francis Hookham



Friday, 23 April 2004 6:43 pm

Many thanks John McGhie but

boolIsFound = Selection.Find.Execute Replace:=wdReplaceAll

produces "Expected: End of statement" in red

Can you also help me with the other part of my problem which is, how do I
replace/add spaces round special characters like "?", "(", ")", "!" as
appropriate - these characters are not "findable" in the normal way
 
E

Elliott Roper

Francis Hookham said:
Hi John McGhie - in my posting Friday, 23 April 2004 6:43 pm I was still
whittling on about "?", "(", ")", "!" - the answer is to uncheck Wild cards

The one problem which remains is how to remove multiple spaces rather than
simply repeating removing double spaces several time hoping they have all
been trapped - do I recall "white space" meaning multiple spaces?
You do, but it does not work with wildcards on. My trick is to search
for " ^w" and replace with " ". Looking for space whitespace speeds up
the operation bigtime.

Wait on! If you want to keep wildcards on, I think there is a way of
telling it to match any number of a character.
yep! find is " {2,}", replace is " ". That's precisely what you want.
Find 2 or more spaces, replace with one.
 
J

John McGhie [MVP - Word]

Hi Francis:

Ahhhh... Sorry about that. I knew I should not have trusted to memory.
Hang on, let me fire up Virtual PC...

That should have been:

boolIsFound = Selection.Find.Execute(Replace:=wdReplaceAll)


Note: That MUST be all one line in the VBA Editor.

Sorry about that.

from "Francis said:
Many thanks John McGhie but

boolIsFound = Selection.Find.Execute Replace:=wdReplaceAll

produces "Expected: End of statement" in red

Can you also help me with the other part of my problem which is, how do I
replace/add spaces round special characters like "?", "(", ")", "!" as
appropriate - these characters are not "findable" in the normal way

Many thanks

Francis

Hi Francis:

There comes a time when you need to make a choice between Find/Replace and
parsing the line in VBA and using sophisticated string manipulation.

To get out of your immediate problem you can do this:

With Selection.Find
.Text = " "
.Replacement.Text = " "
End With

Dim boolIsFound as Boolean
boolIsFound = True
While boolIsFound = true
boolIsFound = Selection.Find.Execute Replace:=wdReplaceAll
WEnd

Just check that on your compiler: I wrote that off the top of my head. But
what happens there is that the Execute method returns True if it made any
changes and false if it didn't. So that will iterate the Execute until no
more changes occur.

Let me know if it chucks errors and I will have a proper look at it when I
get home.

You want to avoid sophisticated string parsing logic if you can because it
makes for complex code that can be rather slow to execute. I would
encourage your authors to make greater use of the Grammar checker to spot
some of these things.

In a professional workplace, I find that if you get into the habit of
returning submitted documents that have errors marked by the spelling or
grammar checker, with a note telling the author to fix it, Authors very
quickly clean up their act :)

Hope this helps

from "Francis said:
Many thanks Paul for

Replace your last two Find/Replace with these two. The 'Find' entry's
Match Wildcards Property in VB Editor Help tells you that. Match Wildcards =
True is the same as "Use Wildcards" in the UI's Find/Replace dialog, and the
regular Word Help tells you how to use wildcards (though it was awfully
tricky to figure this one out). I've made the 'With' blocks inclusive to
save repeating Selection. Find over and over too. (You can do the same with
the first two blocks.)

That works well in the macro reproduced below

It is the start of a much longer macro for correcting copy for a club
magazine

It will remove double spaces and many other typos of that sort

I am having difficulty with characters like ( ) ? ! where I need to
remove/add spaces before/after, as the case maybe

In the macro below I have simply repeated the remove double spaces section
several times but I wonder if there is a better way

(Incidentally, I am writing this on the Mac (Office Word 2001) but I shall
want it to run universally on PC Word as well

Sub TidyUp()
Selection.HomeKey Unit:=wdStory

'Remove double spaces
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = " "
.Replacement.Text = " "
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.Execute Replace:=wdReplaceAll

'Remove space before stops
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = " ."
.Replacement.Text = "."
End With
Selection.Find.Execute Replace:=wdReplaceAll

'Remove space after commas
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ", "
.Replacement.Text = ","
End With
Selection.Find.Execute Replace:=wdReplaceAll

'Remove space before commas
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = " ,"
.Replacement.Text = ","
End With
Selection.Find.Execute Replace:=wdReplaceAll

'Remove space after stops
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ". "
.Replacement.Text = "."
End With
Selection.Find.Execute Replace:=wdReplaceAll

'Add space after sentence stop but not in #,##0.00
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "(.)([!0-9])"
.MatchWildcards = True
.Replacement.Text = "\1 \2"
.Execute Replace:=wdReplaceAll
End With

'Add space after comma in sentence but not in #,##0.00
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "(,)([!0-9])"
.MatchWildcards = True
.Replacement.Text = "\1 \2"
.Execute Replace:=wdReplaceAll
End With
End Sub

--

Please respond only to the newsgroup to preserve the thread.

John McGhie, Consultant Technical Writer,
McGhie Information Engineering Pty Ltd
Sydney, Australia. GMT + 10 Hrs
+61 4 1209 1410, mailto:[email protected]
 
J

John McGhie [MVP - Word]

Hi Francis:

No: "white space" means "any character that doesn't print".

The only way to remove multiple spaces (quickly) is to search for two and
replace with one and iterate until you don't find any instances of two.
That's what the code I sent you does, and it's the quickest way.

If you were in a VERY long document and you wanted to reduce the run-time,
you would start off looking for ten spaces, and iterate until you don't find
any. Then search for FIVE and iterate until you don't find any.

Then TWO and iterate again. That will run very quickly, but has the
overhead of more coding and a slower execution for the first iteration of
each replace because you need to change the Find text.

Cheers

from "Francis said:
Hi John McGhie - in my posting Friday, 23 April 2004 6:43 pm I was still
whittling on about "?", "(", ")", "!" - the answer is to uncheck Wild cards

The one problem which remains is how to remove multiple spaces rather than
simply repeating removing double spaces several time hoping they have all
been trapped - do I recall "white space" meaning multiple spaces?

Your help again please

Thanks

Francis Hookham



Friday, 23 April 2004 6:43 pm

Many thanks John McGhie but

boolIsFound = Selection.Find.Execute Replace:=wdReplaceAll

produces "Expected: End of statement" in red

Can you also help me with the other part of my problem which is, how do I
replace/add spaces round special characters like "?", "(", ")", "!" as
appropriate - these characters are not "findable" in the normal way

--

Please respond only to the newsgroup to preserve the thread.

John McGhie, Consultant Technical Writer,
McGhie Information Engineering Pty Ltd
Sydney, Australia. GMT + 10 Hrs
+61 4 1209 1410, mailto:[email protected]
 

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