PDA

View Full Version : [SOLVED:] Adjust the Space Between 2 Words To 1 Space - Wildcard Search



dj44
11-11-2016, 06:57 AM
Hi folks,

Hope all are well this day :) :)

I have been trying to solve this peculiar space problem.
Through out the document there are some characters a comma followed by X

Example

'X - comma X

I am trying to adjust the space between this and the next word, I don’t know what the next word is

Example

'X the cat sat on the mat (7 spaces)


'X Cup of coffee (5 Spaces)

I want to make them unifrom so there is only 1 space between them

'X Love Food(1 Space)

I have looked all over and, well I'm not sure what to do

I found this


https://cybertext.wordpress.com/2012/01/06/word-find-and-replace-multiple-spaces-between-words/

And I did this





Sub SpaceBetweenWords()

Dim oRng As Range


Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "'X"
Do While .Execute(Forward:=True) = True



'oRng.Characters.First = "'X"

'oRng.Characters.Last


oRng.Collapse wdCollapseEnd
Loop
End With

End Sub



Can I do a search and replace with wildcards or regexular expressions?

Thank you for any tips and advice

Paul_Hossler
11-11-2016, 11:58 AM
Wouldn't this be a macro that replaces 2 or more spaces with just one?

I don't follow the "X" and the "," part since the examples don't have a comma in them

dj44
11-11-2016, 12:14 PM
Hello Paul,

Oops I meant

A single quotation mark Followed by X is word1 or the first word

'X word2 some where
'X word2 some text

etc

How woud I find the spaces?

gmaxey
11-11-2016, 12:21 PM
You don't need a macro for that but here one is:


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oRng As Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "('X) {2,}"
.MatchWildcards = True
.Replacement.Text = "\1 "
.Execute Replace:=wdReplaceAll
End With
lbl_Exit:
Exit Sub
End Sub

dj44
11-11-2016, 12:44 PM
Hello Greg,
nice to see you :)

yes it did it nicely - but it doesnt like the quotation mark for some reason.
I tested it with

#X word 2
#X word2 again

The spaces were only 1 so it works.

Well is suppose its no big deal I can just replace the single quote with a hash sign in this case

dj44
11-11-2016, 01:06 PM
Thanks Greg and have a good weekend,

and folks too

:)