Solved

Trouble with export text in visual basic to .txt file

Posted on 2012-12-27
2
290 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

785 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