2

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?

Dave
  • 25,513
tsilvs
  • 152

2 Answers2

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.

How do I add VBA in MS Office?

Dave
  • 25,513
1

What worked for me was just select all the text in the word file and click on Clear formatting:

screenshot of tool

Peregrino69
  • 5,004
anji
  • 11