我正在尝试将csv文件导入到我的访问数据库中。它看起来像这样: “Vol.2016.Aug.23.csv”
当我尝试使用docmd导入它时,它会给我一个错误,它无法导入文件。我想出了由于多个“。”而无法将文件识别为csv的问题。
但是,我将获得以我需要导入的方式命名的文件。有什么办法可以解决这个问题吗?
请帮忙!
这是我的代码:
fileName = "Vol." & Year(Date) & "." & MonthName(Month(Date), True) & "." & Day(Date) & ".csv"
filePath = "C:\Users\House\Desktop\"
DoCmd.TransferText Transfertype:=acImportDelim, TableName:="Table1", fileName:=filePath + fileName, HasFieldNames:=True
答案 0 :(得分:0)
好的 - 这应该对你有用
我在我的系统上进行了测试并确认您的DoCmd调用可能使用旧的MS-ACcess代码库要求并且在“。”上失败。和文件名中的任何其他特殊字符 - 可能是硬编码的东西,用于查找文件名中的第一个点以便找出扩展名
解决方案是使用短文件名
将此添加到您的模块顶部
' Borrowed code from https://support.microsoft.com/en-us/kb/175512
#If VBA7 Then
Declare PtrSafe Function GetShortPathName Lib "kernel32" _
Alias "GetShortPathNameA" (ByVal lpszLongPath As String, _
ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long
#Else
Declare Function GetShortPathName Lib "kernel32" _
Alias "GetShortPathNameA" (ByVal lpszLongPath As String, _
ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long
#End If
Public Function GetShortName(ByVal sLongFileName As String) As String
Dim lRetVal As Long, sShortPathName As String, iLen As Integer
'Set up buffer area for API function call return
sShortPathName = Space(255)
iLen = Len(sShortPathName)
'Call the function
lRetVal = GetShortPathName(sLongFileName, sShortPathName, iLen)
'Strip away unwanted characters.
GetShortName = Left(sShortPathName, lRetVal)
End Function
然后修改您的代码以将文件名转换为短版
Dim filename As String
Dim filePath As String
Dim csvFile As String
filename = "Vol." & Year(Date) & "." & MonthName(Month(Date), True) & "." & Day(Date) & ".csv"
filePath = "C:\Users\House\Desktop\"
' Convert to Short Filename to work with old MS-Access code base
csvFile = GetShortName(filePath & filename)
Debug.Print csvFile
DoCmd.TransferText Transfertype:=acImportDelim, TableName:="Table1", filename:=csvFile, HasFieldNames:=True
答案 1 :(得分:0)
尝试使用以下解决方案。
我没有尝试处理文件名中存在的句点,而是编写了一个脚本,以暂时重命名文件名。基本上我将.
替换为_
以解决导入问题。
Option Explicit
Public Sub ImportTableWithPeriod()
Dim fso As Object: Set fso = CreateObject("Scripting.FileSystemObject")
Dim f As Object
Dim FolderName As String
Dim Filename As String
Dim TempFilepath As String
Dim OrigFilePath As String
Dim TempFileName As String
Dim tempFolderPath As String
Filename = "Vol." & Format(Now(), "yyyy") & "." & _
Format(Now(), "MMM") & "." & _
Format(Now(), "d") & ".csv"
FolderName = "C:\Users\House\Desktop\"
'Build string for replacement names
'Basically I've replaced the periods with underscores
TempFileName = Replace(Left(Filename, Len(Filename) - 4), ".", "-") & ".csv"
TempFilepath = FolderName & TempFileName
OrigFilePath = FolderName & Filename
Set f = fso.getFile(OrigFilePath)
'rename the file temporarily
f.Name = TempFileName
'Import the file with the renamed instance
DoCmd.TransferText Transfertype:=acImportDelim, TableName:="Table1", Filename:=TempFilepath, HasFieldNames:=True
'Put the name back as it was
f.Name = Filename
'Clean up
set fso = nothing
set f = nothing
End Sub