访问vba中的“路径未找到”

时间:2015-12-03 21:24:09

标签: vba access-vba

我有以下代码抛出异常“未找到路径”。

Dim myfso As New FileSystemObject
Set myfso = CreateObject("Scripting.FileSystemObject")
Dim myoFile As Object 
Set myoFile = myfso.CreateTextFile("C:\Users\myname\dropbox_folder\Dropbox\dropboxpath.txt")
myoFile.WriteLine "C:\Users\myname\dropbox_folder\Dropbox\"
myoFile.Close
Set myfso = Nothing
Set myoFile = Nothing

Dim strContents As String
Dim myfso1 As New FileSystemObject
Set myfso1 = CreateObject("Scripting.FileSystemObject")
Dim myoFile1 As Object
Dim mypath As String

Set myoFile1 = myfso1.OpenTextFile("C:\Users\myname\dropbox_folder\Dropbox\dropboxpath.txt", ForReading)
strContents = myoFile1.ReadAll
myoFile1.Close


Dim fso, oFolder, oSubfolder, oFile, queue As Collection

Set fso = CreateObject("Scripting.FileSystemObject")
Set queue = New Collection

queue.Add fso.GetFolder(strContents)

最后一个命令抛出异常:找不到路径。但路径存在“C:\ Users \ myname \ dropbox_folder \ Dropbox \”,这是积极的,不要争论。

奇怪的是,如果你将鼠标指向变量,你会看到: “C:\用户\ MYNAME \ dropbox_folder \收存箱\ 没有第二个“。这对我来说有点奇怪。 此外,如果我运行上一个命令

queue.Add fso.GetFolder(“C:\ Users \ myname \ dropbox_folder \ Dropbox \”)

代码顺利执行。

您认为有什么问题?

1 个答案:

答案 0 :(得分:1)

更改为:

myoFile.Write "C:\Users\myname\dropbox_folder\Dropbox\"

因为WriteLineVbCrLf\r\n)附加到文件中。当您随后ReadAll在路径末尾以VbCrLf结束时,会使其无效。 (CrLf不显示在单行工具提示中,但表现为缺少关闭"