D_Bester
D_Bester

Reputation: 5931

Why are my spaces not trimmed

This seems obvious and straightforward, but it's not working. I'm working from in Excel VBA. I get the open Word application and extract text from a bookmarked spot within a table in Word. Then the trouble starts. The resulting string is 5 chr(32) spaces. But whatever I try, I can't get rid of the spaces. Why are the spaces not being trimmed or replaced?

Dim Wd As Word.Application
Set Wd = GetObject(, "Word.Application")
Dim doc As Word.Document
'Dim r As Word.Range
'Dim p As Word.Paragraph
Dim tb As Word.Table

Set doc = Wd.ActiveDocument
Set tb = doc.Tables(1)
'tb.Select

Dim Place As String
Place = Trim(doc.Bookmarks("County").Range.Text)

'outputs length 5
Debug.Print Len(Place)

'this outputs 32 5 times so I know we have chr(32) and not something else
Dim i As Integer
For i = 1 To Len(Place)
    Debug.Print Asc(Mid(Place, i, 1))
Next

'try trim
Place = Trim(Place)
Debug.Print Len(Place)
'still 5 spaces

'try replace
Dim s As String
s = VBA.Replace(Place, Chr(32), "")
Debug.Print Len(Place)
'still 5 spaces

What is happening with my code?

Upvotes: 1

Views: 88

Answers (1)

Alex K.
Alex K.

Reputation: 175936

Probably a unicode space, consider U2000 EN QUAD Whitespace:

x="W" & chrw(&h2000) & "W"
?x
W W
?asc(mid(x,2,1))
 32    <= normalized
?ascw(mid(x,2,1))
 8192  <= true character

So examine the char with ascw and replace with chrw

Upvotes: 1

Related Questions