仅从完整路径获取文件名

时间:2017-10-02 15:12:54

标签: vba ms-access access-vba ms-access-2013

在VBA中,我如何才能从下面的完整路径中获取文件名?当我在下面的语法中使用时,它导入完整的文件路径(没有.xlsx)作为表名,这不是我所追求的。

Public Function importExcelSheets() As Long
Dim Directory As String, TableName As String, strDir As String, strFile As String, I As Long
Directory = "C:\Test"
On Error Resume Next
 I = 0
 If Left(Directory, 1) <> "\" Then
     strDir = Directory & "\"
 Else
     strDir = Directory
 End If
 strFile = Dir(strDir & "*.XLSX")
 TableName = strDir & strFile
 While strFile <> ""
     I = I + 1
     strFile = strDir & strFile
     DoCmd.TransferSpreadsheet acImport, , TableName, strFile, True
     strFile = Dir()
 Wend
 importExcelSheets = I
End Function

1 个答案:

答案 0 :(得分:2)

您在此处指定:TableName = strDir & strFile表名是完整路径。只需删除目录组件:TableName = strFile

您只需删除最后5个字符即可删除.xslxTableName = Left(TableName, Len(TableName) -5 )