I have a Word document about 200 pages. The text I pasted from a PDF lost it's spaces for some reason. How do I fix this?
Asked
Active
Viewed 2.6k times
2 Answers
1
This is quite interesting but also not easy
My real answer is to suggest you fix the issue which is how the PDF was exported!
However, this VBa may get you going. There is no undo so create a back up first
Option Explicit
Sub DoIt()
Dim maxChars As Integer
maxChars = 30 'update for the biggest word you want to check for (max characters in the word)
Dim pos As Integer
pos = 0
Dim total As Integer
total = Len(Range.Text)
Do While (pos < Len(Range.Text))
Dim s As String
s = ""
Dim wordToUse As String
wordToUse = ""
Dim i As Integer
For i = 1 To maxChars
s = s + Mid(Range.Text, pos + i, 1)
If SpellCheck(s) = True Then
wordToUse = s
End If
Next i
pos = pos + Len(wordToUse)
Dim lef As String
Dim rig As String
lef = Trim(left(Range.Text, pos))
rig = Trim(Mid(Range.Text, pos + 1))
Range.Text = Trim(lef) + " " + Trim(Replace(rig, " ", " "))
If pos >= total Then
Exit Do
End If
Loop
End Sub
Function SpellCheck(SomeWord As String) As Boolean
'credit https://stackoverflow.com/a/10776225/1221410
SpellCheck = Application.CheckSpelling(SomeWord)
End Function
The logic is simple - keeping adding characters until you find a valid word... at that point, make sure it's not part of a word (eg and exists in land). Then add some white space to the end.
Dave
- 25,513
