MS Access VBA FileSystemObject 不接受包含空格的路径
使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
无论是在 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.
我对同一问题的解决方案:
My solution to the same issue:
自从我做任何 VBA 以来已经有一段时间了,但我认为您需要将字符串括在引号中以消除路径中的空格,所以这有效:
编辑:
此站点建议使用此例程:
Private Function GetQuotedArgument(ByVal argument As String) As String
Const 引用为字符串 = """"
返回 String.Format("{0}{1}{0}", 引用, 参数)
结束功能
给出:
如果失败,您将不得不求助于文件名的缩写形式...有关这样做的 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:
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:
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