MS Access VBA FileSystemObject 不接受包含空格的路径

发布于 2024-12-11 06:25:06 字数 443 浏览 0 评论 0原文

使用 FileSystemObject 时,如何解决 VBA 不允许文件路径中存在空格的限制?

这是我的代码:

from= "C:\Users\MyAccount\Desktop\a.txt"
to= "C:\Users\MyAccount\Desktop\Folder Name With Spaces\b.txt"
Dim fso As New FileSystemObject
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CopyFile from, to

我已经尝试过在任何空格之前添加 " 的技巧,但它不起作用。实际上会弹出“Bad FileName or Number”错误。

我还尝试用 %20 替换任何空格,这也不起作用。

澄清一下,我事先不知道路径,它是由用户输入的。

How do I get around the limitation VBA have of not allowing spaces in file path when using the FileSystemObject?

Here is my code:

from= "C:\Users\MyAccount\Desktop\a.txt"
to= "C:\Users\MyAccount\Desktop\Folder Name With Spaces\b.txt"
Dim fso As New FileSystemObject
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CopyFile from, to

I have already tried the trick with adding " before any spaces, it doesn't work. The "Bad FileName or Number" error actually pops up.

I have also tried to replace any spaces with %20, which also does not work.

To clarify, I don't know the path beforehand, it is entered by the user.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我ぃ本無心為│何有愛 2024-12-18 06:25:06

无论是在 VBA 中还是使用 FSO 时,文件名或路径中的空格都没有限制。你一定还有其他问题。

例如,如果目标文件夹尚不存在,我认为 FSO 不会创建目标文件夹。

另外:如果您 Dim ... As New ...,则不需要使用 Createobject:您的对象是在 Dim 语句中创建的。

There's no restriction around having spaces in filenames or paths, either in VBA or when using the FSO. You must have some other problem.

For example I don't think FSO will create a destination folder if it doesn't already exist.

Also: you don't need to use Createobject if you Dim ... As New ...: your object is created in the Dim statement.

清引 2024-12-18 06:25:06

我对同一问题的解决方案:

Dim folderPath As String
folderPath = "D:\MyData\BackUp\capdat\City Name"
If Len(Dir$((folderPath & "\OLDData" & Format(Date, "-ddmmyyyy") & ".accdb"))) > 0 Then
    Kill (folderPath & "\OLDData" & Format(Date, "-ddmmyyyy") & ".accdb"
Else
    Do something
End if

My solution to the same issue:

Dim folderPath As String
folderPath = "D:\MyData\BackUp\capdat\City Name"
If Len(Dir$((folderPath & "\OLDData" & Format(Date, "-ddmmyyyy") & ".accdb"))) > 0 Then
    Kill (folderPath & "\OLDData" & Format(Date, "-ddmmyyyy") & ".accdb"
Else
    Do something
End if
岁月染过的梦 2024-12-18 06:25:06

自从我做任何 VBA 以来已经有一段时间了,但我认为您需要将字符串括在引号中以消除路径中的空格,所以这有效:

fso.CopyFile """" + from + """", """" + to+ """"

编辑:

此站点建议使用此例程:

Private Function GetQuotedArgument(ByVal argument As String) As String
Const 引用为字符串 = """"
返回 String.Format("{0}{1}{0}", 引用, 参数)
结束功能

给出:

fso.CopyFile GetQuotedArgument(from), GetQuotedArgument(to)

如果失败,您将不得不求助于文件名的缩写形式...有关这样做的 Microsoft 文章 这里,但不确定它是否适用于 VBA

its a while since I did any VBA but I think you need to enclose your strings in quotes to negate the spaces in the paths, so does this work:

fso.CopyFile """" + from + """", """" + to+ """"

EDIT:

This site suggested this routine:

Private Function GetQuotedArgument(ByVal argument As String) As String
Const Quote As String = """"
Return String.Format("{0}{1}{0}", Quote, argument)
End Function

giving:

fso.CopyFile GetQuotedArgument(from), GetQuotedArgument(to)

Failing that you'll have to resort to short forms of the file names... Microsoft article on doing that here, not sure if it applies to VBA though

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文