Solved

Trouble with export text in visual basic to .txt file

Posted on 2012-12-27
2
289 Views
Last Modified: 2012-12-31
Hi:

I've produced the following code:

Public Class background

    Dim stRep As String
    Dim strReport As String
    Dim oFSO As Object
    Dim objReport As Object

    Private Sub MakeFile1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MakeFile1.Click
        strReport = "report.txt"

        stRep = InputBox(TextBox1.Text)
        SetAttr(oFSO = CreateObject("Scripting.FileSystemObject"))
        SetAttr(objReport = oFSO.CreateTextFile(strReport, True))

        If oFSO.FolderExists(stRep) Then
            For Each oF1 In oFSO.GetFolder(stRep).Files
                objReport.WriteLine(oF1.Name)
            Next
        End If

        objReport.Close()

    End Sub

    Private Sub SetAttr(ByVal p1 As Object)
        Throw New NotImplementedException
    End Sub

End Class
=================================
The goal is to create a .txt file and append it if it already exists.  I'm working in VB 2010.  However, the bolded line is giving me trouble.  I've received the following message at runtime....

Operator '=' is not defined for 'Nothing' and type 'FileSystemObject'.

Any ideas on how to address this?  I will keep doing research, but would appreciate feedback!

Robert
0
Comment
Question by:RobertGeoff
2 Comments
 
LVL 49

Expert Comment

by:Rgonzo1971
ID: 38723513
Hi,

Did you check the reference?

Shouldn't it be
 SetAttr(Set oFSO = CreateObject("Scripting.FileSystemObject"))

go on the safe side and do
Set oFSO = CreateObject("Scripting.FileSystemObject")

 SetAttr oFSO


Regards
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 250 total points
ID: 38724436
You are working with VB2010 and yet you wrote VB6 code!

>The goal is to create a .txt file and append it if it already exists.

Use

System.IO.File.AppendAllText("filepath", "text to append")

It will create the file if it does not exist and append to existing file if it already exists.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now