2017-09-15 66 views
-3

请找我下面的代码在那里我得到许可被拒绝错误VBA打开输出许可被拒绝

Sub VBA() 

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

i = "info" 
Set ts = fso.CreateTextFile("z:\" & i & ".txt", True) 

File = "z:\" & i & ".txt" 
n = FreeFile() 
Open File For Output As #n '''HERE I AM GETTING ERROR 
Print #n, s 
End Sub 
+1

你打开一个文件,FSO。然后你试图用内置的VBA工具打开同一个文件。不是两者都可以使用。 – GSerg

+0

@GSerg请指导我如何打开并使用FSO打印。 – sagar

+0

我不认为'FileSystemObject'有助于打印,但即使它确实如此,关于如何使用它打印的“指南”几乎肯定是过于宽泛,无法在主题上进行打印。尝试编写一些代码,以及何时/如果您遇到特定问题会引发另一个问题。 (@ GSerg的评论回答了你目前遇到的问题。) – YowE3K

回答

0

您可以使用下面的函数,并传递任何数量的参数给它。

Public Sub WriteToTextFile(ByVal Filename As String, ParamArray args() As Variant) 
    On Error GoTo ErrProc 

    Dim hFileDest As Integer 
     hFileDest = FreeFile 
    Open Filename For Append As #hFileDest 

    Dim idx As Integer 
    For idx = LBound(args) To UBound(args) 
     Print #hFileDest, args(idx) 
    Next idx 

Leave: 
    On Error Resume Next 
    Close #hFileDest 
    On Error GoTo 0 
    Exit Sub 

ErrProc: 
    MsgBox Err.Description, vbCritical 
    Resume Leave 
End Sub 

要叫它:

Sub T() 
    Dim path_ As String 
     path_ = "D:\Test.txt" 

    WriteToTextFile path_, "One", "Two", "Three", "Four", "Five" 
End Sub 

输出:

One 
Two 
Three 
Four 
Five 
+0

我已经在** ts.WriteLine s **的帮助下解决了上述错误 – sagar

相关问题