如前所述,如何从 VBA(Visual Basic for Applications)中指定索引处的字符串中获取字符?我搜索了谷歌,这些不起作用:
s(index)
,
s.Chars(index)
,s,Characters(index)
那么如何在指定索引处获取字符呢?
如果s
是你的字符串,那么你可以这样做:
Mid(s, index, 1)
根据以下问题的评论进行编辑。
似乎您需要一些不同的方法,这应该更容易。以这种方式尝试:
Dim character As String 'Integer if for numbers
's = ActiveDocument.Content.Text - we don't need it
character = Activedocument.Characters(index)
从指定索引处的字符串中获取一个字符
Dim pos As Integer
Dim outStr As String
pos = 2
Dim outStr As String
outStr = Left(Mid("abcdef", pos), 1)
outStr="b"
char = split_string_to_char(text)(index)
------
Function split_string_to_char(text) As String()
Dim chars() As String
For char_count = 1 To Len(text)
ReDim Preserve chars(char_count - 1)
chars(char_count - 1) = Mid(text, char_count, 1)
Next
split_string_to_char = chars
End Function