PDA

View Full Version : Word 2003 - puting space between certain words



Lucy234
03-01-2012, 09:44 AM
I need a script which would allow me to add space after/and before certain words and/or letters/numbers. Let's say I want to add space before numbers 1-8, but not before number 9. Let's say I have to be more specific regarding certain numbers, letters, words after which/or before I have to put space. I need a script in VBA which would allow me to add/remove those exceptions at will. For example, I want to make a space between two sentences / Bird is flying.Her wings are blue., but I want to make sure that there won't be any spaces between numbers, such as..$4,500.00 etc.
I know it's complicated but ty..

fumei
03-01-2012, 12:39 PM
What have you tried so far?

Lucy234
03-01-2012, 01:27 PM
Everything that I know in the CTRL+H area..

fumei
03-01-2012, 08:26 PM
OK, that is Find and Replace. You can probably do SOME of what you want using Find and Replace, but certainly not all...I think.

"I want to make a space between two sentences / Bird is flying.Her wings are blue., but I want to make sure that there won't be any spaces between numbers, such as..$4,500.00 etc."

These are two different things, yes?

You need to really spell out the rules you have to work out. For example, are you going to be selecting the text "Bird is flying.Her wings are blue.", or are you trying to find it?

Frosty
03-01-2012, 08:30 PM
Have you clicked the More button? Checked out the "Special" menu? If you're simply looking for a way to search for "." and replace with ". " but you don't want to have $4,500.00 become $4,500. 00... then you simply need to search for "^$." and replace with "^$. " ... but I'm assuming you already know about that in the CTRL+H area.

If that doesn't help, check out wildcard searches... you can get pretty complicated without resorting to macro writing.
http://word.mvps.org/faqs/General/UsingWildcards.htm

And if your knowledge of CTRL+H is really robust, and you already know about regular "special" searches and really complicated wildcard searches... perhaps you can be a bit more specific? What exact search are you trying to do, and why is it failing?

Frosty
03-01-2012, 08:31 PM
Whoops, sorry Fumei, didn't mean to step on your post.

fumei
03-01-2012, 10:20 PM
lol, not at all. The more input from us the better. Good suggestions. I am unclear as to whether some sort of coded searching is an actual requirement. Which is why I asked if this was something tied to text that is selected...or not.

Lucy234
03-01-2012, 11:08 PM
For example. If there is a space between one "period" and two numbers, how to make it just? 6 . 6 = 6.6
but Only in the case where there is space after and before period after which there are numbers on Both sides..
And, how to enter text between Certain words..? I can replace one sign with another, but how to sign between 2 Certain signs? or lets say 10 of them specifically?

Talis
03-02-2012, 11:46 AM
"For example. If there is a space between one "period" and two numbers, how to make it just? 6 . 6 = 6.6 but Only in the case where there is space after and before period after which there are numbers on Both sides."

Find/Replace, check 'Use wildcards'


Find: ([0-9]) . ([0-9])
Replace: \1.\2


Sorry, can't understand second part. Can you give an example?

fumei
03-02-2012, 03:20 PM
Yes, I do not understand:

"I can replace one sign with another, but how to sign between 2 Certain signs? or lets say 10 of them specifically?"

Certain signs????

Lucy234
03-02-2012, 06:00 PM
Yes, I do not understand:

"I can replace one sign with another, but how to sign between 2 Certain signs? or lets say 10 of them specifically?"

Certain signs????


Yes. For example: "Love is nice exciting"

"School partying must be coordinated."

Word END must be put between bold words. Not just after every "nice" or every "school" but only in situations where there are two of those bold words together.

macropod
03-02-2012, 06:45 PM
Cross-posted at: http://www.office-forums.com/add-space-after-period-using-macro-t2339559.html
where there has already been extensive discussion, including the provision of macros to do the task.
Lucy: For cross-posting etiquette, please read http://www.excelguru.ca/content.php?184

fumei
03-02-2012, 08:15 PM
"Yes. For example: "Love is nice exciting"

"School partying must be coordinated."

Word END must be put between bold words. Not just after every "nice" or every "school" but only in situations where there are two of those bold words together."

And that something to do with "certain signs"????

Lucy, I am not trying to be critical but you need to try harder to be clear when asking things.

What do you mean by Word END? End? What end?

"Not just after every "nice" or every "school" but only in situations where there are two of those bold words together."

Except...even in your example those words are NOT together. So what do you mean? And does this Word END (whatever THAT means...) go between nice and exciting - after all they are bold.

Again, please be clear and specific.

Lucy234
03-03-2012, 01:31 AM
"Yes. For example: "Love is nice exciting"

"School partying must be coordinated."

Word END must be put between bold words. Not just after every "nice" or every "school" but only in situations where there are two of those bold words together."

And that something to do with "certain signs"????

Lucy, I am not trying to be critical but you need to try harder to be clear when asking things.

What do you mean by Word END? End? What end?

"Not just after every "nice" or every "school" but only in situations where there are two of those bold words together."

Except...even in your example those words are NOT together. So what do you mean? And does this Word END (whatever THAT means...) go between nice and exciting - after all they are bold.

Again, please be clear and specific.


Sorry, I meant "And"

Love is nice AND exciting.

School AND partying must be coordinated.


AND is between school - partying and nice - exciting

I want to be able to chose certain words, like those, between which the word "AND" should be put.

But, if the sentence would be: "School flying must be coordinated." In That case the word "AND" won't be put after school because it's the word flying and not partying. So how to define words between which there should be something put, whether it's a word or a sign or whatever?

macropod
03-03-2012, 02:04 AM
So how to define words between which there should be something put, whether it's a word or a sign or whatever?Unless you have clear rules for when to edit/skip, you'd have to do that on a case-by-case basis.

As for the earlier question about the periods, have you even tried the macros I posted in reply to your cross-post at: http://www.office-forums.com/add-space-after-period-using-macro-t2339559.html?

Lucy234
03-03-2012, 03:00 AM
I do have clear rules for when to edit or skip, but I don't know how to write them in VBA..perhaps if I would have something like that I could add/or remove things I need.

macropod
03-03-2012, 03:47 AM
Well, if you don't tell us exactly what those rules are, you can't expect any advice given to satisfy those rules, can you?! So far, you're just doling them out piecemeal, wasting everyone's time.

Lucy234
03-03-2012, 07:27 AM
Well, if you don't tell us exactly what those rules are, you can't expect any advice given to satisfy those rules, can you?! So far, you're just doling them out piecemeal, wasting everyone's time.

Oh very well then!

If there is a word "desk" and after which there is a word "table" put word "pen" between so it should look like "desk pen table".

macropod
03-03-2012, 02:15 PM
In that case, an ordinary Find/Replace, where:
Find = desk table
Replace = desk pen table

Lucy234
03-03-2012, 11:14 PM
In that case, an ordinary Find/Replace, where:
Find = desk table
Replace = desk pen table


Great, now what if the word "pen" has to be entered between the two specific words, but those two can be any from the list of 20 different words?


desk table - desk pen table / ok


but


desk chair - desk pen chair

chair table - chair pen table

shoes book - shoes pen book

book chair - book pen chair etc.


Is there a way to write 20 words in a row and say: Put "AND" between any of those two in the text?

macropod
03-03-2012, 11:47 PM
You're not paying attention. I asked you to say what the rules are, and instead you gave one example. Ultimately, it's all a matter of using Find/Replace. With a macro, you could feed 20 word pairs in and use Find/Replace to insert another word between them. Now, if you:
• have a complete list of what the word pairs are
• have a complete list of what words should be inserted between them
• tell us what those details,
maybe we can make some progress.

Lucy234
03-04-2012, 12:51 AM
You're not paying attention. I asked you to say what the rules are, and instead you gave one example. Ultimately, it's all a matter of using Find/Replace. With a macro, you could feed 20 word pairs in and use Find/Replace to insert another word between them. Now, if you:
• have a complete list of what the word pairs are
• have a complete list of what words should be inserted between them
• tell us what those details,
maybe we can make some progress.

It's not just a pair of 2 from 20 but it can be ANY pair as long as it's consisted from the word list of 20 different words.


Words are: job, skill, jump, hide, spam, duck, hike, read, walk, talk, sleep, leap, lose, snooze, joint, point, clock, flock, go, no.


Now, I need "X" word to be inserted between two of those from this list. It can be any two, but the "X" must be inserted ONLY if the two words from the list are next to each other, otherwise no. The list must be flexible so I can add and remove content at will.

I know this is not an easy task, maybe you have friends who can solve this problem.

macropod
03-04-2012, 01:12 AM
Not a difficult task at all!

Try:
Sub Demo()
Application.ScreenUpdating = False
Dim strWords As String, StrFndA As String, StrFndB As String, StrAdd As String, i As Long, j As Long
strWords = "job,skill,jump,hide,spam,duck,hike,read,walk,talk,sleep,leap,lose,snooze,jo int,point,clock,flock,go,no"
StrAdd = "X"
With ActiveDocument.Content.Find
.ClearFormatting
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Replacement.Text = ""
.Wrap = wdFindContinue
For i = 0 To UBound(Split(strWords, ",")) - 1
For j = i + 1 To UBound(Split(strWords, ","))
StrFndA = Split(strWords, ",")(i)
StrFndB = Split(strWords, ",")(j)
.Text = StrFndA & " " & StrFndB
.Replacement.Text = StrFndA & " " & StrAdd & " " & StrFndB
.Execute Replace:=wdReplaceAll
.Text = StrFndB & " " & StrFndA
.Replacement.Text = StrFndB & " " & StrAdd & " " & StrFndA
.Execute Replace:=wdReplaceAll
Next j
Next i
End With
Application.ScreenUpdating = True
End Sub
Because the macro searches for each pair of words in both orders, it doesn't matter what order the words are listed in on the strWords line.

As for adding words to the list at will, you'll need to add them to the strWords line, separated by commas and with no spaces before/after. The only way around that would be to keep the list somewhere else (eg a separate document). Having them listed in an inputbox for editing would be too unwieldly and restrictive.

Lucy234
03-04-2012, 02:49 AM
That's Exactly what I had in mind. Works like a charm. Ty