为什么我的空间没有被修剪

时间:2015-01-20 13:24:09

标签: excel vba ms-word

这看起来很明显和直截了当,但它不起作用。我在Excel VBA工作。我得到了打开的Word应用程序,并从Word中的表格中的书签位置提取文本。然后麻烦开始了。结果字符串是5个chr(32)空格。但无论我尝试什么,我都无法摆脱空间。为什么空间没有被修剪或更换?

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

我的代码发生了什么?

1 个答案:

答案 0 :(得分:1)

可能是unicode空间,请考虑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

因此,请使用ascw检查字符,并替换为chrw