为什么我的空间没有被修剪
Why are my spaces not trimmed
这看起来很明显和直接,但它不起作用。我在 Excel VBA 工作。我打开了 Word 应用程序,并从 Word 中 table 内的书签位置提取文本。然后麻烦就开始了。结果字符串是 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
我的代码出了什么问题?
可能是 unicode space,考虑 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
这看起来很明显和直接,但它不起作用。我在 Excel VBA 工作。我打开了 Word 应用程序,并从 Word 中 table 内的书签位置提取文本。然后麻烦就开始了。结果字符串是 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
我的代码出了什么问题?
可能是 unicode space,考虑 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