
Public Class ThisWorkbook

    Private Sub ThisWorkbook_Startup() Handles Me.Startup
        Dim btn_calc As Button = New Button
        btn_calc.Text = "ایجاد فایل بانک"
        AddHandler btn_calc.Click, AddressOf btn_calc_Click
        Me.ActionsPane.Controls.Add(btn_calc)
    End Sub

    Private Sub btn_calc_Click(sender As Object, e As EventArgs)
        Dim bankTXT As String = vbNullString
        Dim rng As Excel.Range = Globals.Sheet1.Range("B2")
        Dim filePath As String = IO.Path.GetDirectoryName(
                Reflection.Assembly.GetExecutingAssembly().CodeBase)
        If filePath.StartsWith("file:\") Then filePath = filePath.Substring(6)
        If Not filePath.EndsWith("\") Then filePath += "\"
        filePath += "bank.txt"
        If IO.File.Exists(filePath) Then IO.File.Delete(filePath)
        Do Until rng.Value = vbNullString
            Dim salary = rng.Value
            Dim accountNo = vbNullString
            If rng.Next.Value <> vbNullString Then
                accountNo = rng.Next.Value
            Else
                rng.Next.NoteText("شماره حساب وارد نشده است.")
            End If
            bankTXT += "*" & accountNo & "*" & salary & "*" & vbCrLf
            rng = rng.Offset(1, 0)
        Loop
        Using sw As IO.StreamWriter = IO.File.CreateText(filePath)
            sw.Write(bankTXT)
        End Using
        Shell("Notepad.exe" & " " & filePath)
    End Sub

    Private Sub ThisWorkbook_Shutdown() Handles Me.Shutdown

    End Sub

End Class
