Last active
October 30, 2025 19:59
-
-
Save redraiment/b22a5962e5ded4ad1ce4da146cf9175b to your computer and use it in GitHub Desktop.
DuckDB ODBC UTF-8 Character Encoding Fix for Excel VBA
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Function ConvertUtf8ToUnicode(bytes() As Byte) As String | |
| Dim ostream As Object | |
| Set ostream = CreateObject("ADODB.Stream") | |
| With ostream | |
| .Type = 1 ' Binary | |
| .Open | |
| .Write bytes | |
| .Position = 0 | |
| .Type = 2 'Text | |
| .Charset = "UTF-8" | |
| ConvertUtf8ToUnicode = .ReadText(-1) | |
| .Close | |
| End With | |
| End Function | |
| Public Sub Execute(sql As String, range As range) | |
| Dim connection As Object | |
| Set connection = CreateObject("ADODB.Connection") | |
| connection.Open "Driver={DuckDB Driver};Database=:memory:;" | |
| Dim rs As Object | |
| Set rs = CreateObject("ADODB.Recordset") | |
| rs.Open sql, connection | |
| Dim data As Variant | |
| data = rs.GetRows() | |
| Dim rows As Long, cols As Long | |
| cols = UBound(data, 1) | |
| rows = UBound(data, 2) | |
| Dim cells As Variant | |
| ReDim cells(rows, cols) | |
| rs.MoveFirst | |
| Dim row As Long, col As Long, bytes() As Byte | |
| For row = 0 To rows | |
| For col = 0 To cols | |
| If adVarChar <= rs.Fields(col).Type And rs.Fields(col).Type <= adLongVarBinary And Not IsNull(rs.Fields(col).value) Then | |
| bytes = data(col, row) | |
| cells(row, col) = ConvertUtf8ToUnicode(bytes) | |
| Else | |
| cells(row, col) = data(col, row) | |
| End If | |
| Next col | |
| rs.MoveNext | |
| Next row | |
| range.Resize(rows + 1, cols + 1).value = cells | |
| rs.Close | |
| Set rs = Nothing | |
| connection.Close | |
| Set connection = Nothing | |
| End Sub |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: