Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-09-2018, 11:53 AM
David Matthews David Matthews is offline Macro to remove spaces between characters Windows 7 64bit Macro to remove spaces between characters Office 2016
Novice
Macro to remove spaces between characters
 
Join Date: May 2018
Posts: 6
David Matthews is on a distinguished road
Default Macro to remove spaces between characters

Hi all,

Been Googling this for the last couple of days and have come up with nothing--nada!

My problem is this: I have a document that contains about 75% of the words with spaces between the characters like the image attached. Crazy, eh? Well, it certainly begs the question, "how the heck did that happen?" What I have learned from my research is sometimes this can occur in PDF to Word conversions and vice versa.

As it's about 150 pages long you can imagine how long it would take to manually take out all the spaces, even with find and replace.

Soooooo, I'm looking for a macro that will remove the single spaces between the characters (I can manually remove the doubles between the words with F&R).

This is what I've developed so far:

Sub RemoveSpaces()
'
' RemoveSpaces Macro
'
'


Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^? ^?"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
End Sub

However i can't get it to loop.

Please advise.

Thank you,

David Matthews (not the band )
Attached Images
File Type: png screenshot.png (40.1 KB, 38 views)
Reply With Quote
  #2  
Old 05-09-2018, 12:35 PM
gmaxey gmaxey is offline Macro to remove spaces between characters Windows 7 32bit Macro to remove spaces between characters Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,421
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Code:
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey, http://gregmaxey.com/word_tips.html, 5/9/2018
Dim oRng As Word.Range
  Set oRng = ActiveDocument.Range
  With oRng.Find
    .Text = "  "
    .Replacement.Text = "***"
    .MatchWildcards = True
    .Execute Replace:=wdReplaceAll
  End With
  Set oRng = ActiveDocument.Range
  With oRng.Find
    .Text = "(*) (*)"
    .Replacement.Text = "\1\2"
    .MatchWildcards = True
    .Execute Replace:=wdReplaceAll
  End With
  Set oRng = ActiveDocument.Range
  With oRng.Find
    .Text = "***"
    .Replacement.Text = " "
    .Execute Replace:=wdReplaceAll
  End With
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 05-09-2018, 01:30 PM
David Matthews David Matthews is offline Macro to remove spaces between characters Windows 7 64bit Macro to remove spaces between characters Office 2016
Novice
Macro to remove spaces between characters
 
Join Date: May 2018
Posts: 6
David Matthews is on a distinguished road
Default

Oh, my gosh. You just cut my work on this doc about 90%!

It isn't getting everything perfectly, but it functioned on the vast majority!

What the devil is that thing doing???

Thank you, thank you, thank you!

David Matthews
Reply With Quote
  #4  
Old 05-09-2018, 02:31 PM
macropod's Avatar
macropod macropod is offline Macro to remove spaces between characters Windows 7 64bit Macro to remove spaces between characters Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

You don't need a macro for this - just a single wildcard Find/Replace, where:
Find = ([! ])^32
Replace = \1
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 05-09-2018, 05:46 PM
gmaxey gmaxey is offline Macro to remove spaces between characters Windows 7 32bit Macro to remove spaces between characters Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,421
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Well, based on your sample where words are separated by two spaces, it replaces those two spaces with a temporary marker "***". Then it joined any character separated from any other character with a space. Finally it replaced the temporary marker with a space.

Paul is right, you don't need a macro. You asked for one though. Using Paul's refined construction:

Code:
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey, http://gregmaxey.com/word_tips.html, 5/9/2018
Dim oRng As Word.Range
  Set oRng = ActiveDocument.Range
  With oRng.Find
    .Text = "([! ])^32 "
    .Replacement.Text = "\1"
    .MatchWildcards = True
    .Execute Replace:=wdReplaceAll
  End With
lbl_Exit:
  Exit Sub
End Sub
Which basically finds any character not a space and a space and replaces it with that character.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #6  
Old 05-09-2018, 05:52 PM
macropod's Avatar
macropod macropod is offline Macro to remove spaces between characters Windows 7 64bit Macro to remove spaces between characters Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by gmaxey View Post
Using Paul's refined construction:
which could be reduced to:
Code:
Sub Demo()
ActiveDocument.Range.Find.Execute FindText:="([! ])^32 ", ReplaceWith:="\1", MatchWildcards:=True, Replace:=wdReplaceAll
End Sub
but I'm sure you knew that
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 05-10-2018, 02:43 AM
gmaxey gmaxey is offline Macro to remove spaces between characters Windows 7 32bit Macro to remove spaces between characters Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,421
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Aye, and further reduced to:

Code:
Sub Demo()
  ActiveDocument.Range.Find.Execute "([! ]) ", , , True, , , , , , "\1", 2
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #8  
Old 05-10-2018, 02:46 AM
macropod's Avatar
macropod macropod is offline Macro to remove spaces between characters Windows 7 64bit Macro to remove spaces between characters Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Or even:
Code:
Sub Demo(): ActiveDocument.Range.Find.Execute "([! ]) ", , , True, , , , , , "\1", 2: End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #9  
Old 05-10-2018, 02:55 AM
gmaxey gmaxey is offline Macro to remove spaces between characters Windows 7 32bit Macro to remove spaces between characters Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,421
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Bloated with needless excess:

Code:
Sub D(): ActiveDocument.Range.Find.Execute "([! ]) ", , , True, , , , , , "\1", 2: End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro to remove spaces between characters How to remove blank spaces between rows in a table, Jamal NUMAN Word 2 04-28-2017 12:59 PM
A Widlcard or Macro to Properly Remove Spaces Between Commas? CrossReach Word 6 01-23-2017 04:28 AM
Can I remove spaces between only Chinese characters? sivartnosredna7 Word 2 01-09-2017 05:33 AM
How to remove spaces from Cells which are at the end of value? LearnerExcel Excel 2 12-19-2016 01:10 PM
editing text and remove spaces romanticbiro Word VBA 5 07-04-2014 07:42 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 08:09 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft