Search code examples
vbams-word

Getting char from string at specified index


As stated how to get char from string at specified index in VBA (Visual Basic for Applications)? I searched Google and these do not work:

s(index) , s.Chars(index),s,Characters(index)

So how to get char at specified index?


Solution

  • If s is your string than you could do it this way:

    Mid(s, index, 1)
    

    Edit based on comment below question.

    It seems that you need a bit different approach which should be easier. Try in this way:

    Dim character As String 'Integer if for numbers
    's = ActiveDocument.Content.Text - we don't need it
    character = ActiveDocument.Characters(index)