VBA(コマンド実行とテキストファイル操作)

http://officetanaka.net/excel/vba/file/file08c.htm
http://officetanaka.net/excel/vba/file/file08b.htm
https://www.sejuku.net/blog/89852

Windows Script Host Object Model」を参照設定で追加して再起動必要


Sub ボタン_Click()

Dim wsh As Object
Dim res As WshExec
Dim lines() As String
Dim buf As String

'画面を更新しない
Application.ScreenUpdating = False
'確認メッセージを表示しない
Application.DisplayAlerts = False


'コマンド実行してテキストファイル書込
Set wsh = CreateObject("WScript.Shell")
Set res = wsh.Exec("cmd /c " & "set")

Do While res.Status = 0
DoEvents
Loop

lines = Split(res.StdOut.ReadAll, vbCrLf)

Open ActiveWorkbook.path & "\test.txt" For Output As #1
For Each Line In lines
Print #1, Line
Next Line
Close #1

Set res = Nothing
Set wsh = Nothing


'テキストファイル読込
Open ActiveWorkbook.path & "\test.txt" For Input As #1
Do Until EOF(1)
Line Input #1, buf
Debug.Print (buf)
Loop
Close #1


'確認メッセージを表示する
Application.DisplayAlerts = True
'画面を更新する
Application.ScreenUpdating = True

MsgBox ("処理完了")

End Sub