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

时间:2011-06-21 16:09:33

标签: excel excel-vba excel-2007 vba

我正在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

有什么想法?提前谢谢。

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

我觉得@Doug Glancy头上钉了一针。您在\FilePath之间错过了sOutPutFile,他的解决方案适用于您的特定情况。但是,一般情况下,FilePath在结尾处是否已经有\并不总是很明显,这需要在连接字符串之前进行测试以获得更一般的情况。

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

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

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