Share via

character code issues in vba using the vbe, msWord

Anonymous
2016-12-23T15:14:36+00:00

I am working with a document (in Hebrew) in which some strings are used to represent numerals (in English, the equivalent of a = 1, b =2, c = 3, ... k = 20, l = 30).

I am writing a program that converts those letter strings to numeric value.

The basic process is:

ltrs = Array(ChrW(1488), ChrW(1489), ChrW(1490), ChrW(1491), ChrW(1492), ChrW(1493), _

           ChrW(1494), ChrW(1495), ChrW(1496), ChrW(1497), ChrW(1499), ChrW(1500), ChrW(1502), _

           ChrW(1504), ChrW(1505), ChrW(1506), ChrW(1508), ChrW(1510), ChrW(1511), ChrW(1512), _

           ChrW(1513), ChrW(1514))

sum = 0

For i = 1 To rPre.Characters.Count

     For j = 0 To 21

        If ltrs(j) = rPre.Characters(i).Text Then

            If j < 9 Then

                sum = sum + j + 1

            ElseIf j >= 9 And j < 19 Then

                sum = sum + 10 * (j - 8)

            ElseIf j >= 19 Then

                sum = sum + 100 * (j - 17)

            End If

        End If

    Next

Next

This works within the sub, but when I attempt to put the operation in a callable  function, the character codes get messed up (the text actually transfers as a string of question marks.

I have tried a number of variations on this (passing string rather than a range and parsing the string, etc.) but I can't make it work.

Any suggestions about how to do this?

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

4 answers

Sort by: Most helpful
  1. Anonymous
    2016-12-25T00:40:38+00:00

    I am a total and complete dope. Sorry for wasting everyone's time: I did not include a line in the function returning the value.

    So that the episode not be a total and complete loss, I include the function below. (It is widely used in traditional Hebrew, and the abjad system in Arabic is a rough equivalent.) It covers my specific use case and is not complete (this would require mapping alternate forms of a character to the same value).

    Thanks and happy holidays to all.

    Function letterValue(rStr As Range) As Long

    Dim ltrs As Variant

    Dim c As String

    Dim i As Long

    Dim j As Long

    Dim sum As Long

    ltrs = Array(ChrW(1488), ChrW(1489), ChrW(1490), ChrW(1491), ChrW(1492), ChrW(1493), ChrW(1494), ChrW(1495), ChrW(1496), ChrW(1497), ChrW(1499), ChrW(1500), ChrW(1502),

    ChrW(1504), ChrW(1505), ChrW(1506), ChrW(1508), ChrW(1510), ChrW(1511), ChrW(1512),

    ChrW(1513), ChrW(1514))

    For i = 1 To rStr.Characters.Count

    '    c = rStr.Characters(i).Text 'debugging

    '    Debug.Print AscW(c)         'debugging

        For j = 0 To 21

            If ltrs(j) = rStr.Characters(i).Text Then

                If j < 9 Then

                    sum = sum + j + 1

                ElseIf j >= 9 And j < 19 Then

                    sum = sum + 10 * (j - 8)

                ElseIf j >= 19 Then

                    sum = sum + 100 * (j - 17)

                End If

            End If

        Next

    Next

    letterValue = sum

    End Function

    Sub testLetterValue()

    Dim rStr As Range

    Dim n As Long

    Set rStr = ActiveDocument.Range

    n = letterValue(rStr)

    Debug.Print n

    End Sub

    Was this answer helpful?

    0 comments No comments
  2. Doug Robbins - MVP - Office Apps and Services 323.1K Reputation points MVP Volunteer Moderator
    2016-12-24T22:46:38+00:00

    This question was posted to the Microsoft Office Programming forum, which IS the correct place to post it.

    Just be cause you cannot answer a question, don't go sending the poster somewhere else.

    Was this answer helpful?

    0 comments No comments
  3. Doug Robbins - MVP - Office Apps and Services 323.1K Reputation points MVP Volunteer Moderator
    2016-12-24T22:45:04+00:00

    Show us the complete code.

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2016-12-24T13:57:35+00:00

    Hi,

    For assistance regarding VBA Coding for Office, we recommend that you post your query on MSDN forums.

    Kind regards.

    Was this answer helpful?

    0 comments No comments