错误 506:Class 未定义:'FileSystemObject'
Error 506: Class not defined: 'FileSystemObject'
我正在为名为 "ScorBase" 的程序编写 VBA 脚本。我正在尝试调用创建文本文件的子例程,但我 "m encountering this error "506".
这是我正在编写的代码:
Sub emailFile()
' Declare a TextStream.
Dim stream
'As TextStream
dim fso
Set fso = New FileSystemObject
' Create a TextStream.
Set stream = fso.CreateTextFile("C:\Users\eladt\Desktop\creatFile\Mail.txt", True)
stream.WriteLine "user Email."
stream.WriteLine "Maki"
stream.WriteLine "Nigeri"
stream.WriteLine "Sashimi"
stream.Close
End Sub
您原来问题下的评论似乎表明 Microsoft Scripting Runtime 不可用或未安装在您的 PC 上。这可能是无意的,但也可能是因为 IT 管理员在您的环境中对该库的访问权限有限。
幸运的是,有多种编写文本文件的方法。您也许可以使用 ADO 库来编写流..
只需添加对 Microsoft ActiveX Data Objects 2.8 Library
的引用,然后将您的代码替换为:
Sub emailFile()
' Declare an ADO Stream.
Dim stream As ADODB.stream
' Create an ADO Stream.
Set stream = New ADODB.stream
stream.Open
stream.Type = adTypeText
stream.WriteText "user Email.", stWriteLine
stream.WriteText "Maki", stWriteLine
stream.WriteText "Nigeri", stWriteLine
stream.WriteText "Sashimi", stWriteLine
stream.SaveToFile "C:\Users\eladt\Desktop\creatFile\Mail.txt", adSaveCreateOverWrite
stream.Close
End Sub
如果来自另一个应用程序的后期绑定有效,那么您的第 3 方应用程序托管的 VBA 环境存在问题,可能它正在执行某种类型的沙盒。
您不需要任何外部库来写入文件,您可以尝试在本机执行此操作:
Dim hf As Integer
hf = FreeFile
Open "C:\Users\eladt\Desktop\creatFile\Mail.txt" For Output As #hf
Print #hf, "user Email."
Print #hf, "Maki"
Print #hf, "Nigeri"
Print #hf, "Sashimi"
Close #hf
不使用任何外部引用的更简单的方法:
Sub MM_Email_To_File()
Dim FF As Integer
FF = FreeFile
'// The file will be created if it doesn't exist
Open "C:\Users\eladt\Desktop\creatFile\Mail.txt" For Output As #FF
Print #FF, "User Email"
Print #FF, "Maki"
Print #FF, "Nigeri"
Print #FF, "Sashimi"
Close #FF
End Sub
您可以查看 this MSDN article 以获取有关 VBA 中 I/O 操作的更多信息。
我正在为名为 "ScorBase" 的程序编写 VBA 脚本。我正在尝试调用创建文本文件的子例程,但我 "m encountering this error "506".
这是我正在编写的代码:
Sub emailFile()
' Declare a TextStream.
Dim stream
'As TextStream
dim fso
Set fso = New FileSystemObject
' Create a TextStream.
Set stream = fso.CreateTextFile("C:\Users\eladt\Desktop\creatFile\Mail.txt", True)
stream.WriteLine "user Email."
stream.WriteLine "Maki"
stream.WriteLine "Nigeri"
stream.WriteLine "Sashimi"
stream.Close
End Sub
您原来问题下的评论似乎表明 Microsoft Scripting Runtime 不可用或未安装在您的 PC 上。这可能是无意的,但也可能是因为 IT 管理员在您的环境中对该库的访问权限有限。
幸运的是,有多种编写文本文件的方法。您也许可以使用 ADO 库来编写流..
只需添加对 Microsoft ActiveX Data Objects 2.8 Library
的引用,然后将您的代码替换为:
Sub emailFile()
' Declare an ADO Stream.
Dim stream As ADODB.stream
' Create an ADO Stream.
Set stream = New ADODB.stream
stream.Open
stream.Type = adTypeText
stream.WriteText "user Email.", stWriteLine
stream.WriteText "Maki", stWriteLine
stream.WriteText "Nigeri", stWriteLine
stream.WriteText "Sashimi", stWriteLine
stream.SaveToFile "C:\Users\eladt\Desktop\creatFile\Mail.txt", adSaveCreateOverWrite
stream.Close
End Sub
如果来自另一个应用程序的后期绑定有效,那么您的第 3 方应用程序托管的 VBA 环境存在问题,可能它正在执行某种类型的沙盒。
您不需要任何外部库来写入文件,您可以尝试在本机执行此操作:
Dim hf As Integer
hf = FreeFile
Open "C:\Users\eladt\Desktop\creatFile\Mail.txt" For Output As #hf
Print #hf, "user Email."
Print #hf, "Maki"
Print #hf, "Nigeri"
Print #hf, "Sashimi"
Close #hf
不使用任何外部引用的更简单的方法:
Sub MM_Email_To_File()
Dim FF As Integer
FF = FreeFile
'// The file will be created if it doesn't exist
Open "C:\Users\eladt\Desktop\creatFile\Mail.txt" For Output As #FF
Print #FF, "User Email"
Print #FF, "Maki"
Print #FF, "Nigeri"
Print #FF, "Sashimi"
Close #FF
End Sub
您可以查看 this MSDN article 以获取有关 VBA 中 I/O 操作的更多信息。