代表者の戯言

textfile output from VB



Creating a text file from VB and loading it into an Excel file is a task I often do. I would like to share this method as a personal memo.



◆ Name of the text file to be created: myfolder & "\NOTEPAD"


(Please specify `myfolder` in advance on the user side.)


Example: myfolder = "C:\Users\User\Desktop"



◆ content: This is the content to be written into the text file.


◆ fileplace: Specify the location of the file.


◆ excelname: Specify the name of the Excel file.


On the Excel side, use a macro to make sure that myfolder & "\NOTEPAD" can be loaded.



Note that `fileplace` and `myfolder` are not the same. `fileplace` refers to the location where the text file is saved, while `myfolder` is where the Excel file exists. (This Excel file will be used to open the fileplace.)


VB

‘------------------------------------------------------------------------------------------


Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click


Dim textfile As IO.StreamWriter


Dim myfolder = "C:\Users\mypcname\Desktop"


Dim content = "ABCDEFG"



textfile = New IO.StreamWriter(myfolder & "\NOTEPAD")


textfile.Write(content)


textfile.WriteLine()


textfile.Close()


Call openexcel()


End Sub



Private Sub openexcel()


Dim fileplace = "C:\Users\mypc\fileplace"


Dim excelname As String = "macroname.xlsm”


GC.Collect()


System.Diagnostics.Process.Start(fileplace & "\" & excelname)


End Sub


End Class