2013-03-26 190 views
6

在Windows 7上使用MS Access 2003中,我發現下面這個函數去除從ANSI字符串的所有口音,改變(例如)閱兵式,以SENOR:插入Unicode字符爲VBA字符串

Public Function RemoveAccents(ByVal inputString As String) As String 
Const accentString As String = "ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÏÑÒÓÔÕÖÙÚÛÜÝàáâãäåçèéêëìíîïðñòóóôõöùúûüýÿ" 
Const nonAccentStr As String = "AAAAAACEEEEIIIIINOOOOOUUUUYaaaaaaceeeeiiiionoooooouuuuyy" 
Dim i As Integer 

For i = 1 To Len(accentString) 
inputString = Replace(inputString, Mid(accentString, i, 1), Mid(nonAccentStr, i, 1), , , vbBinaryCompare) 
Next i 
RemoveAccents = inputString 
End Function 

但是,當我試圖與卡隆(U-010D)(č)(HTML č)添加拉丁文小寫字母C的功能常量accentString,這樣,

Const accentString As String = "ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÏÑÒÓÔÕÖÙÚÛÜÝàáâãäåç" & ChrW$(&H10D) & "èéêëìíîïðñòóóôõöùúûüýÿ" 
Const nonAccentStr As String = "AAAAAACEEEEIIIIINOOOOOUUUUYaaaaaacceeeeiiiionoooooouuuuyy" 

我無法運行該功能。有沒有一種語法可以讓我調整這個函數以從包含不在ANSI字符集中的字符的字符串中去除變音符號?

+1

嘖嘖,即使我只說英語,住在通常是同質的美國,但我仍然需要處理Unicode字符,這個問題幫助了我。 – GlennFromIowa 2016-07-06 15:27:12

回答

4

在常量聲明中不能有像ChrW()這樣的函數。

修訂

如果你可以讓這些公共變量,而不是常量,那麼這應該照顧它給你:

Const cWithCaron As String = &H10D 
Public accentString As String 
Public nonAccentStr As String 


Sub TestStrings() 
Dim clnString As String 

    accentString = "ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÏÑÒÓÔÕÖÙÚÛÜÝàáâãäåç" & ChrW(cWithCaron) & "èéêëìíîïðñòóóôõöùúûüýÿ" 

    nonAccentStr = "AAAAAACEEEEIIIIINOOOOOUUUUYaaaaaacceeeeiiiionoooooouuuuyy" 
    'I added this variable to test the function: 
    clnString = RemoveAccents(accentString) 
    'And a message box to display the results: 
    MsgBox clnString = nonAccentStr 

End Sub 

Public Function RemoveAccents(ByVal inputString As String) As String 

Dim i As Integer 

    For i = 1 To Len(accentString) 

     inputString = Replace(inputString, Mid(accentString, i, 1), Mid(nonAccentStr, i, 1), , , vbBinaryCompare) 
    Next i 

    RemoveAccents = inputString 

End Function 
+0

大衛,你的建議使我能夠保存和運行該功能,但現在該功能並沒有用普通的c代替斯拉夫&H10D。它將斯拉夫語hacek留在c上。 – Commata 2013-03-26 17:46:05

+0

啊,我明白了。讓我仔細看看。 – 2013-03-26 17:52:46

+0

爲什麼這些必須被聲明爲'CONST'?如果這些變量是在其他地方設置的「PUBLIC」變量(例如,調用RemoveAccents函數的子例程),我可以想到可能的解決方法。 – 2013-03-26 17:53:56