Search code examples
vbabatch-fileeditexcel-addins

How to edit a .bat file with Excel VBA


I'm developing an Excel Add-in for .bat / .vbs files. The add-in has 2 options (1) create a new .bat / and .vbs file and (2) modify an existing .bat and .vbs file.

The "create" option is working great and does exactly what we need it to do.

I'm having issues with the "modify" piece. When the user selects "modify", and Excel form is displayed with a List Box. The user scrolls through the list and selects the one they need to modify. So far so go.

The next step is for the add-in to open (not run) both the .bat and .vbs file. I've been able to get the .bat file to execute; which in turns executes the .vbs file. But that is not what I need it to do.

If I was going to manually open the .bat or .vbs file. I would right click on the .bat or .vbs file and select "edit". The appropriate file opens in NotePad. I can then make any changes and save the file.

What I'm looking duplicate is the "right click and select edit steps" with VBA.

Any suggestions would be greatly appreicated. Thanks for your help in advance....


Solution

  • I am not familiar with Add-Ins, sorry if misleading. This does work in normal code:

    Assume there is a userform frmTest7 with TextBox1, ListBox1 and CommandButton1 (to save changes). The userform code as follows.

    Option Explicit
    
    Private Sub CommandButton1_Click()
    
        Dim fso As FileSystemObject
        Dim oFile As TextStream
        
        Dim FilePath As String
        Dim strFile As String
        
        FilePath = "C:\Users\" & Environ("UserName") & "\desktop\test_bat.bat"
        
        Dim i As Long
        
        For i = 0 To frmTest7.ListBox1.ListCount - 1
            strFile = strFile & frmTest7.ListBox1.List(i) & vbCrLf
        Next i
        
        Set fso = New FileSystemObject
        
        If fso.FileExists(FilePath) Then
            'fso.DeleteFile (FilePath)
            Set oFile = fso.CreateTextFile(FilePath, True)
            oFile.WriteLine strFile
        End If
        
        If Not (oFile Is Nothing) Then oFile.Close
        
        Set oFile = Nothing
        Set fso = Nothing
    
    End Sub
    
    Private Sub ListBox1_Click()
    
        frmTest7.TextBox1.Text = frmTest7.ListBox1.List(frmTest7.ListBox1.ListIndex)
    
    End Sub
    
    Private Sub TextBox1_AfterUpdate()
    
        frmTest7.ListBox1.List(frmTest7.ListBox1.ListIndex) = frmTest7.TextBox1.Text
    
    End Sub
    
    Private Sub UserForm_Activate()
    
        Dim fso As FileSystemObject
        Dim oFile As TextStream
        
        Dim FilePath As String
        Dim strFile As String
        
        FilePath = "C:\Users\" & Environ("UserName") & "\desktop\test_bat.bat"
        
        Set fso = New FileSystemObject
        Set oFile = fso.OpenTextFile(FilePath, ForReading)
        
        strFile = oFile.ReadAll
        
        oFile.Close
        
        Dim arrStrFile() As String
        
        strFile = Replace(strFile, vbCr, "")
        arrStrFile = Split(strFile, Chr(10))
        
        frmTest7.ListBox1.Clear
        frmTest7.ListBox1.List = arrStrFile
        
        Set oFile = Nothing
        Set fso = Nothing
    
    End Sub