Private Sub btnConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConvert.Click
        Dim oEdiDoc As ediDocument
        Dim oSchema As ediSchema
        Dim oSchemas As ediSchemas
        Dim oSegment As ediDataSegment
        Dim sSegmentID As String
        Dim sLoopSection As String
        Dim nArea As Integer
        Dim sValue As String
        Dim sN1Entity As String
        Dim sQlfr As String

        Dim sPath As String
        Dim sEdiFile As String
        Dim sSefFile As String

        Dim oFile As System.IO.File
        Dim oWrite As System.IO.StreamWriter
        Dim sHeader As String
        Dim sDetailEnt As String
        Dim sDetailEntRmr As String

        sPath = AppDomain.CurrentDomain.BaseDirectory & "\"
        sEdiFile = "820.X12"
        sSefFile = "820_4010.EVAL0.SEF" 'evaluation sef file

        oWrite = oFile.CreateText(sPath & "csvOutput.txt")


        'CREATES EDIDOC OBJECT
        oEdiDoc = New ediDocument

        'THIS MAKES CERTAIN THAT FREDI ONLY USES THE SEF FILE PROVIDED, AND THAT IT DOES
        'NOT USE ITS BUILT-IN STANDARD REFERENCE TABLE TO TRANSLATE THE EDI FILE.
        oSchemas = oEdiDoc.GetSchemas
        oSchemas.EnableStandardReference = False

        'THIS OPTIONS STOPS FREDI FROM KEEPING ALL THE SEGMENTS IN MEMORY
        oEdiDoc.CursorType = DocumentCursorTypeConstants.Cursor_ForwardOnly

        'LOADS THE SEF FILE
        oSchema = oEdiDoc.LoadSchema(sPath & sSefFile, 0)

        'LOADS THE EDI FILE
        oEdiDoc.LoadEdi(sPath & sEdiFile)

        'GETS THE FIRST DATA SEGMENT
        oSegment = oEdiDoc.FirstDataSegment

        'LOOP THAT WILL TRAVERSE THRU EDI FILE FROM TOP TO BOTTOM
        Do While Not oSegment Is Nothing

            'DATA SEGMENTS WILL BE IDENTIFIED BY THEIR ID, THE LOOP SECTION AND AREA
            '(OR TABLE) NUMBER THAT THEY ARE IN.
            sSegmentID = oSegment.ID
            sLoopSection = oSegment.LoopSection
            nArea = oSegment.Area

            If nArea = 0 Then
                If sLoopSection = "" Then
                    If sSegmentID = "ISA" Then
                        sValue = oSegment.DataElementValue(1)     'Authorization Information Qualifier
                        sValue = oSegment.DataElementValue(2)     'Authorization Information
                        sValue = oSegment.DataElementValue(3)     'Security Information Qualifier
                        sValue = oSegment.DataElementValue(4)     'Security Information
                        sValue = oSegment.DataElementValue(5)     'Interchange ID Qualifier
                        sValue = oSegment.DataElementValue(6)     'Interchange Sender ID
                        sValue = oSegment.DataElementValue(7)     'Interchange ID Qualifier
                        sValue = oSegment.DataElementValue(8)     'Interchange Receiver ID
                        sValue = oSegment.DataElementValue(9)     'Interchange Date
                        sValue = oSegment.DataElementValue(10)     'Interchange Time
                        sValue = oSegment.DataElementValue(11)     'Interchange Control Standards Identifier
                        sValue = oSegment.DataElementValue(12)     'Interchange Control Version Number
                        sValue = oSegment.DataElementValue(13)     'Interchange Control Number
                        sValue = oSegment.DataElementValue(14)     'Acknowledgment Requested
                        sValue = oSegment.DataElementValue(15)     'Usage Indicator
                        sValue = oSegment.DataElementValue(16)     'Component Element Separator

                    ElseIf sSegmentID = "GS" Then
                        sValue = oSegment.DataElementValue(1)     'Functional Identifier Code
                        sValue = oSegment.DataElementValue(2)     'Application Sender's Code
                        sValue = oSegment.DataElementValue(3)     'Application Receiver's Code
                        sValue = oSegment.DataElementValue(4)     'Date
                        sValue = oSegment.DataElementValue(5)     'Time
                        sValue = oSegment.DataElementValue(6)     'Group Control Number
                        sValue = oSegment.DataElementValue(7)     'Responsible Agency Code
                        sValue = oSegment.DataElementValue(8)     'Version / Release / Industry Identifier Code
                    End If   'sSegmentID
                End If   'sLoopSection

            ElseIf nArea = 1 Then
                If sLoopSection = "" Then
                    If sSegmentID = "ST" Then
                        sValue = oSegment.DataElementValue(1)     'Transaction Set Identifier Code
                        sValue = oSegment.DataElementValue(2)     'Transaction Set Control Number

                    ElseIf sSegmentID = "BPR" Then
                        sValue = oSegment.DataElementValue(1)     'Transaction Handling Code
                        sHeader = oSegment.DataElementValue(2) & ","      'Monetary Amount
                        sValue = oSegment.DataElementValue(3)     'Credit/Debit Flag Code
                        sValue = oSegment.DataElementValue(4)     'Payment Method Code
                        sValue = oSegment.DataElementValue(5)     'Payment Format Code
                        sValue = oSegment.DataElementValue(6)     '(DFI) ID Number Qualifier
                        sValue = oSegment.DataElementValue(7)     '(DFI) Identification Number
                        sValue = oSegment.DataElementValue(8)     'Account Number Qualifier
                        sValue = oSegment.DataElementValue(9)     'Account Number
                        sHeader = sHeader & oSegment.DataElementValue(10) & ","      'Originating Company Identifier
                        sValue = oSegment.DataElementValue(11)     'Originating Company Supplemental Code
                        sValue = oSegment.DataElementValue(12)     '(DFI) ID Number Qualifier
                        sValue = oSegment.DataElementValue(13)     '(DFI) Identification Number
                        sValue = oSegment.DataElementValue(14)     'Account Number Qualifier
                        sHeader = sHeader & oSegment.DataElementValue(15) & ","     'Account Number
                        sHeader = sHeader & oSegment.DataElementValue(16) & ","     'Date

                    ElseIf sSegmentID = "NTE" Then
                        sValue = oSegment.DataElementValue(1)     'Note Reference Code
                        sValue = oSegment.DataElementValue(2)     'Description

                    ElseIf sSegmentID = "REF" Then
                        sValue = oSegment.DataElementValue(1)     'Reference Identification Qualifier
                        sHeader = sHeader & oSegment.DataElementValue(2) & ","     'Reference Identification

                    ElseIf sSegmentID = "DTM" Then
                        sValue = oSegment.DataElementValue(1)     'Date/Time Qualifier
                        sHeader = sHeader & oSegment.DataElementValue(2) & ","     'Date
                    End If   'Segment ID

                ElseIf sLoopSection = "N1" Then
                    If sSegmentID = "N1" Then
                        sN1Entity = oSegment.DataElementValue(1)   'In most cases the loop qualifier is the first element of the first segment in the loop, but not necessarily
                    End If

                    If sN1Entity = "PE" Then
                        If sSegmentID = "N1" Then
                            sValue = oSegment.DataElementValue(1)     'Entity Identifier Code
                            sHeader = sHeader & oSegment.DataElementValue(2) & ","     'Name

                        ElseIf sSegmentID = "N3" Then
                            sValue = oSegment.DataElementValue(1)     'Address Information

                        ElseIf sSegmentID = "N4" Then
                            sValue = oSegment.DataElementValue(1)     'City Name
                            sValue = oSegment.DataElementValue(2)     'State or Province Code
                            sValue = oSegment.DataElementValue(3)     'Postal Code

                        ElseIf sSegmentID = "PER" Then
                            sValue = oSegment.DataElementValue(1)     'Contact Function Code
                            sValue = oSegment.DataElementValue(2)     'Name
                            sValue = oSegment.DataElementValue(3)     'Communication Number Qualifier
                            sValue = oSegment.DataElementValue(4)     'Communication Number
                        End If   'sSegmentID

                    ElseIf sN1Entity = "PR" Then
                        If sSegmentID = "N1" Then
                            sValue = oSegment.DataElementValue(1)     'Entity Identifier Code
                            sHeader = sHeader & oSegment.DataElementValue(2) & ","     'Name

                        ElseIf sSegmentID = "N3" Then
                            sValue = oSegment.DataElementValue(1)     'Address Information

                        ElseIf sSegmentID = "N4" Then
                            sValue = oSegment.DataElementValue(1)     'City Name
                            sValue = oSegment.DataElementValue(2)     'State or Province Code
                            sValue = oSegment.DataElementValue(3)     'Postal Code
                        End If   'sSegmentID

                    ElseIf sN1Entity = "RB" Then
                        If sSegmentID = "N1" Then
                            sValue = oSegment.DataElementValue(1)     'Entity Identifier Code
                            sHeader = sHeader & oSegment.DataElementValue(2) & ","     'Name

                        ElseIf sSegmentID = "N3" Then
                            sValue = oSegment.DataElementValue(1)     'Address Information

                        ElseIf sSegmentID = "N4" Then
                            sValue = oSegment.DataElementValue(1)     'City Name
                            sValue = oSegment.DataElementValue(2)     'State or Province Code
                            sValue = oSegment.DataElementValue(3)     'Postal Code
                        End If   'sSegmentID

                    End If


                End If   'sLoopSection

            ElseIf nArea = 2 Then
                If sLoopSection = "ENT" Then
                    If sSegmentID = "ENT" Then
                        sValue = oSegment.DataElementValue(1)     'Assigned Number
                        sValue = oSegment.DataElementValue(2)     'Entity Identifier Code
                        sValue = oSegment.DataElementValue(3)     'Identification Code Qualifier
                        sDetailEnt = oSegment.DataElementValue(4) & ","     'Identification Code
                    End If   'Segment ID

                ElseIf sLoopSection = "ENT;RMR" Then
                    If sSegmentID = "RMR" Then
                        sValue = oSegment.DataElementValue(1)     'Reference Identification Qualifier
                        sDetailEntRmr = oSegment.DataElementValue(2) & ","     'Reference Identification
                        sValue = oSegment.DataElementValue(3)     'Payment Action Code
                        sDetailEntRmr = sDetailEntRmr & oSegment.DataElementValue(4) & ","     'Monetary Amount

                    ElseIf sSegmentID = "REF" Then
                        sValue = oSegment.DataElementValue(1)     'Reference Identification Qualifier
                        sDetailEntRmr = sDetailEntRmr & oSegment.DataElementValue(2) & ","     'Reference Identification

                    ElseIf sSegmentID = "DTM" Then
                        sQlfr = oSegment.DataElementValue(1)     'Date/Time Qualifier
                        If sQlfr = "097" Then
                            sDetailEntRmr = sDetailEntRmr & oSegment.DataElementValue(2) & ","     'Date

                        ElseIf sQlfr = "003" Then
                            sDetailEntRmr = sDetailEntRmr & oSegment.DataElementValue(2)     'Date
                            oWrite.WriteLine(sHeader & sDetailEnt & sDetailEntRmr) 'write line to csv
                        End If

                    End If   'Segment ID

                ElseIf sLoopSection = "TXP" Then
                    If sSegmentID = "TXP" Then
                        sValue = oSegment.DataElementValue(1)     'Tax Identification Number
                        sValue = oSegment.DataElementValue(2)     'Tax Payment Type Code
                        sValue = oSegment.DataElementValue(3)     'Date
                        sValue = oSegment.DataElementValue(4)     'Tax Information Identification Number
                        sValue = oSegment.DataElementValue(5)     'Tax Amount
                    End If   'Segment ID

                ElseIf sLoopSection = "DED" Then
                    If sSegmentID = "DED" Then
                        sValue = oSegment.DataElementValue(1)     'Type of Deduction
                        sValue = oSegment.DataElementValue(2)     'Reference Identification
                        sValue = oSegment.DataElementValue(3)     'Date
                        sValue = oSegment.DataElementValue(4)     'Amount
                        sValue = oSegment.DataElementValue(5)     'Reference Identification
                        sValue = oSegment.DataElementValue(6)     'Yes/No Condition or Response Code
                        sValue = oSegment.DataElementValue(7)     'Name
                        sValue = oSegment.DataElementValue(8)     'Reference Identification
                        sValue = oSegment.DataElementValue(9)     'Yes/No Condition or Response Code
                    End If   'sSegmentID
                End If   'sLoopSection

            ElseIf nArea = 3 Then
                If sLoopSection = "" Then
                End If  'sLoopSection
            End If  'nArea

            'GETS THE NEXT DATA SEGMENT
            ediDataSegment.Set(oSegment, oSegment.Next)

        Loop

        oWrite.Close()
        'DESTROY OBJECTS
        oEdiDoc = Nothing
        oSchema = Nothing
        oSchemas = Nothing
        oSegment = Nothing

        MessageBox.Show("Done")

    End Sub
    

    Click here to download a trial version of the Framework EDI