Excel 2007:将文件保存在共享驱动器上的宏

发布于 2024-11-16 17:55:17 字数 401 浏览 1 评论 0原文

我正在 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 技术交流群。

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

发布评论

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

评论(2

还给你自由 2024-11-23 17:55:17

看起来您需要在 FilePath 和 sOutPutFile 之间使用“\”:

Open FilePath & Application.PathSeparator & sOutPutFile For Output As #nFileNum

It looks like you need a "\" between FilePath and sOutPutFile:

Open FilePath & Application.PathSeparator & sOutPutFile For Output As #nFileNum
过期情话 2024-11-23 17:55:17

我认为@Doug Glancy 击中要害。您在 FilePathsOutPutFile 之间缺少 \,他的解决方案适用于您的特定情况。然而,一般来说,FilePath 末尾是否已经有 \ 并不总是很明显,这需要在连接字符串之前进行测试。更一般的情况。

另一种方法是使用 FileSystemObject 的 BuildPath 方法,它自动执行此测试,即仅在必要时在现有路径和文件名之间插入附加路径分隔符。

Dim FSO As Object
Set FSO = CreateObject("Scripting.FileSystemObject")

Open FSO.BuildPath(FilePath, sOutPutFile) For Output As #nFileNum

I think @Doug Glancy hit the nail on the head. You're missing a \ between FilePath and sOutPutFile, and his solution works in your particular case. In general, however, it isn't always obvious whether the FilePath 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.

Dim FSO As Object
Set FSO = CreateObject("Scripting.FileSystemObject")

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