Access VBA-重命名名称中带句点的文件



因此,我正试图将文件(Enrollment tracking 07.30.xlsx(重命名为"Enrollment traceing.xlsx",但它讨厌日期中的句点,而且不会这样做。

Path = "V:CORPDATA17MBRSHIPANDBILLAlegeus Migration TrackerDaily Reports"
OriginalName = Dir("V:CORPDATA17MBRSHIPANDBILLAlegeus Migration TrackerDaily Reports*Enrollment tracking*")
NewName = "V:CORPDATA17MBRSHIPANDBILLAlegeus Migration TrackerDaily ReportsEnrollment tracking.xlsx"
Name Path & OriginalName As NewName

它将抛出运行时错误75:"路径/文件访问错误"。。。

有办法做到这一点吗?我可以用"OriginalName"变量来识别文件名(它将显示为"Enrollment tracking 07.30.xlsx"(,但我无法用它来重命名文件。

周期不是问题,而是路径。

仔细看看你的最后一行:

Name Path & OriginalName As NewName

问题是您已经将路径放入变量OriginalName中,如下所示:

Path = "V:CORPDATA17MBRSHIPANDBILLAlegeus Migration TrackerDaily Reports"
OriginalName = Dir("V:CORPDATA17MBRSHIPANDBILLAlegeus Migration TrackerDaily Reports*Enrollment tracking*")

因此,您正在尝试重命名路径下的文件&原始名称,应为:

"V:CORPDATA17MBRSHIPANDBILLAlegeus Migration TrackerDaily ReportsV:
CORPDATA17MBRSHIPANDBILLAlegeus Migration TrackerDaily Reports
*Enrollment tracking*"

这显然会导致错误。

试着简单地将最后一行替换为:

Name OriginalName As NewName

我在我的电脑上尝试了这种方法,它奏效了,这是我的完整代码:

Sub changeFileName()
origFile = "C:UsersnameDesktop1.2.3.txt"
NewFile = "C:UsersnameDesktop123.txt"
Name origFile As NewFile
End Sub

EDIT:你实际上可以使用Dir((函数,当我测试上面的代码时,我忘记了包含它。这个代码也对我有效,所以可能文件/路径名有错误?这似乎就是错误所表明的。参见代码:

Sub changeFileName2()
Path = "C:UsersnameDesktop"
origFile = Dir("C:UsersnameDesktop1.2.3.txt")
NewFile = "C:UsersnameDesktop123.txt"
Name Path & origFile As NewFile
End Sub

最新更新