
Ver lo invisible, romper una cuerda
¿Alguna vez ha copiado un texto de una fuente y lo ha pegado en el Editor de VBA, ventana inmediata, y ha visto lo siguiente:
??? ???? ??? ???? ??? ??? ???? ????? ???? ????
o leer datos de una tabla o archivo y obtener el mismo estilo de salida.
Bueno, esto ocurre porque VBE no admite texto Unicode y, por lo tanto, hay una multitud de idiomas y conjuntos de caracteres que no puede representar correctamente, por lo que simplemente genera ‘?’.
Dicho esto, aunque no podamos visualizar el texto, podemos trabajar con él si sabemos cómo.
A continuación se muestra una rutina simple que puede utilizar para generar una cadena como caracteres ASCII, esto nos permite identificar dichos caracteres si queremos trabajar con ellos, dividir la cadena, reemplazar, …
'---------------------------------------------------------------------------------------
' Procedure : String_ConvertToASCII
' Author : Daniel Pineault, CARDA Consultants Inc.
' Website :
' Purpose : Converts a string into its numeric ascii representation
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
' (CC BY-SA 4.0) -
' Req'd Refs: None required
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' sInput : String to output as ascii numerals
' sSeparator: Separator to use between numerals (default to using ,)
'
' Usage:
' ~~~~~~
' ? String_ConvertToASCII("testing.")
' Returns -> 116,101,115,116,105,110,103,46
'
' Revision History:
' Rev Date(yyyy-mm-dd) Description
' **************************************************************************************
' 1 unknown Initial Release
' 2 2024-06-05 Public Release
' 3 2024-06-05 Fix issue related to Asc vs. AscW
' Added optional HTML Output
'---------------------------------------------------------------------------------------
Public Function String_ConvertToASCII(ByVal sInput As String, _
Optional sSeparator As String = ",", _
Optional bOutputAsHTML As Boolean = False) As String
On Error GoTo Error_Handler
Dim lAsciiVal As Long
Dim i As Long
If Len(sInput) = 0 Then GoTo Error_Handler_Exit
For i = 1 To Len(sInput)
lAsciiVal = AscW(Mid(sInput, i, 1)) 'Inverse => Chr(#) or ChrW() if # > 255
If lAsciiVal 0 Then 'remove trailing separator
String_ConvertToASCII = Left(String_ConvertToASCII, Len(String_ConvertToASCII) - Len(sSeparator))
End If
Error_Handler_Exit:
On Error Resume Next
Exit Function
Error_Handler:
MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
"Error Source: String_ConvertToASCII" & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Description: " & Err.Description & _
Switch(Erl = 0, "", Erl 0, vbCrLf & "Line No: " & Erl) _
, vbOKOnly + vbCritical, "An Error has Occurred!"
Resume Error_Handler_Exit
End Function
Entonces, si lo probamos haciendo (en realidad, la cadena se leería desde un archivo o una tabla ya que realmente no podemos proporcionarla de esta manera):
? String_ConvertToASCII("我能吞下玻璃而不傷身體。")
obtenemos:
230,710,8216,232,402,189,229,144,382,228,184,8249,231,381,187,231,8217,402,232,8364,338,228,184,141,229,8218,183,232,186,171,233,171,8221,227,8364,8218
Esta rutina también es útil a veces para identificar caracteres «invisibles» en una cadena que causan problemas al intentar analizar el texto.
Recursos adicionales

