|
かなさん、こんにちは。
VBAは久しぶり & Office2003は初めて。
ということで練習もかねて作成しました。
FileSystemObjectを使って、
Sub aaa01()
Dim WSHshell As Object, DTpath As String
Dim FSO As Object, objText As Object, Fname As String
Dim MyDATA As String
MyDATA = "123"
'↓デスクトップパスを取得
Set WSHshell = CreateObject("WScript.Shell")
DTpath = WSHshell.specialfolders("Desktop")
Fname = DTpath & "\test.txt"
Set FSO = CreateObject("Scripting.FileSystemObject")
'↓書き込み専用でOpen、無ければファイルを作成
Set objText = FSO.OpenTextFile(Fname, 2, True)
'↓MyDATAの内容を書き出し
objText.Write MyDATA
objText.Close
Set objText = Nothing
Set FSO = Nothing
Set WSHshell = Nothing
End Sub
Sub aaa02()
Dim WSHshell As Object, DTpath As String
Dim FSO As Object, objText As Object, Fname As String
Dim MyDATA As String
Set WSHshell = CreateObject("WScript.Shell")
DTpath = WSHshell.specialfolders("Desktop")
Fname = DTpath & "\test.txt"
Set FSO = CreateObject("Scripting.FileSystemObject")
Set objText = FSO.OpenTextFile(Fname, 1)
MyDATA = objText.ReadAll
objText.Close
ThisWorkbook.Worksheets("Sheet1").Range("A1").Value = MyDATA
Set objText = Nothing
Set FSO = Nothing
Set WSHshell = Nothing
End Sub
|
|