You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Win11日文版Office LTSC2021下VBA读取CSV遇运行时错误-2147467259(80004005)

问题

本人使用安装Office LTSC 2021的日文版Win11笔记本,编写了如下VBA代码用于将CSV文件转换为XLSX文件:

Sub ImportCSVtoExcel()
    Dim fd As FileDialog
    Dim strFilePath As String
    Dim conn As Object
    Dim rs As Object
    Dim strQuery As String
    Dim strUTF8Conn As String
    
    ' Create Dialogue Box for CSV file selection
    Set fd = Application.FileDialog(msoFileDialogFilePicker)
    fd.Filters.Clear
    fd.Filters.Add "CSV Files", "*.csv"
    fd.Title = "Please choose your CSV file"
    
    ' Display Dialog Window
    If fd.Show = -1 Then
        strFilePath = fd.SelectedItems(1)
    Else
        MsgBox "No files have been selected!"
        Exit Sub
    End If
    
    ' Create ADO Connection OBject
    Set conn = CreateObject("ADODB.Connection")
    Set rs = CreateObject("ADODB.Recordset")
    
    ' Set the text into UTF-8 code
        strUTF8Conn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strFilePath & ";Extended Properties=""text;HDR=Yes;FMT=Delimited;"";"
    
    ' Open the UT8-8 Text File
    conn.Open strUTF8Conn
    
    ' SQL Queries
    strQuery = "SELECT * FROM [" & strFilePath & "]"
    
    ' Perform the Query and export the results into Excel
    rs.Open strQuery, conn
    Sheet1.Cells.Clear
    Sheet1.Range("A1").CopyFromRecordset rs
    
    ' Close all connections and clear all Objects.
    rs.Close
    Set rs = Nothing
    conn.Close
    Set conn = Nothing
    
    MsgBox "CSV imported into Excel"
End Sub

执行conn.Open strUTF8Conn语句时,弹出VBA运行时错误-2147467259(80004005)提示。已确认文件名和路径无问题,请教该错误的其他可能原因及修正方法?

错误原因及修正方法
  • 连接字符串配置错误
    ACE OLEDB连接文本文件时,Data Source应指定文件所在的文件夹路径,而非完整文件名;同时需明确声明UTF-8编码。修正代码如下:

    ' 提取CSV文件所在文件夹路径
    Dim strFolderPath As String
    strFolderPath = Left(strFilePath, InStrRev(strFilePath, "\"))
    ' 修正UTF-8连接字符串
    strUTF8Conn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strFolderPath & ";Extended Properties=""text;HDR=Yes;FMT=Delimited;CharacterSet=65001;"";"
    ' 修正SQL查询语句(仅使用文件名)
    strQuery = "SELECT * FROM [" & Mid(strFilePath, InStrRev(strFilePath, "\") + 1) & "]"
    
  • ACE OLEDB驱动版本不匹配
    日文版Win11可能存在Office与驱动的32/64位兼容问题:

    1. 检查Office LTSC 2021的位数(32位/64位);
    2. 下载对应版本的Microsoft Access Database Engine 2016 Redistributable;
    3. 若已安装不同位版本的Office,用静默参数安装避免冲突:
      ' 64位驱动静默安装
      AccessDatabaseEngine_X64.exe /passive
      ' 32位驱动静默安装
      AccessDatabaseEngine.exe /passive
      
  • CSV文件编码或格式异常

    • 若CSV带UTF-8 BOM,ACE驱动可能无法识别:用记事本打开文件,选择“另存为”,编码选UTF-8(不带签名)后替换原文件;
    • 若CSV使用非逗号分隔符(如日文环境常用制表符),需在连接字符串的Extended Properties中添加Delimiter=\t(制表符)或对应分隔符。
  • 文件夹权限限制
    若CSV存放在系统目录(如C:\Windows)或受保护文件夹(如Program Files),VBA无读写权限,将文件移至普通用户目录(如“文档”文件夹)重试。

内容的提问来源于stack exchange,提问作者Leon Zhou

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 01:41:02