Excel 2007:将文件保存在共享驱动器上的宏
我正在 Excel 中编写宏,并尝试将 .txt 文件保存到共享驱动器。我已经尝试了下面的两组代码,但收到“运行时错误‘76’:未找到路径”。这是文件路径的正确语法吗?
FilePath = ThisWorkbook.Path & "\\server.name\$foldername"
sOutPutFile = "filename.txt"
FilePath = "\\server.name\$foldername"
sOutPutFile = "filename.txt"
我在以下行中收到错误:
Open FilePath & sOutPutFile For Output As #nFileNum
有什么想法吗?提前致谢。
I am writing a macro in Excel and I am trying to save a .txt file to a share drive. I have tried both sets of code below and I get 'Run-time error '76': Path Not Found". Is this the correct syntax for file path?
FilePath = ThisWorkbook.Path & "\\server.name\$foldername"
sOutPutFile = "filename.txt"
FilePath = "\\server.name\$foldername"
sOutPutFile = "filename.txt"
I get the error on the following line:
Open FilePath & sOutPutFile For Output As #nFileNum
Any thoughts? Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来您需要在 FilePath 和 sOutPutFile 之间使用“\”:
It looks like you need a "\" between FilePath and sOutPutFile:
我认为@Doug Glancy 击中要害。您在
FilePath
和sOutPutFile
之间缺少\
,他的解决方案适用于您的特定情况。然而,一般来说,FilePath
末尾是否已经有\
并不总是很明显,这需要在连接字符串之前进行测试。更一般的情况。另一种方法是使用 FileSystemObject 的
BuildPath
方法,它自动执行此测试,即仅在必要时在现有路径和文件名之间插入附加路径分隔符。I think @Doug Glancy hit the nail on the head. You're missing a
\
betweenFilePath
andsOutPutFile
, and his solution works in your particular case. In general, however, it isn't always obvious whether theFilePath
already has a\
at the end of it or not, and this requires testing before concatenating the strings for a more general case.Another approach is to use the FileSystemObject's
BuildPath
method, which does this testing automatically, i.e. inserts an additional path separator between the existing path and the filename only if necessary.