Option Explicit
Sub text_output()
Dim intFileNo As Integer
Dim strFileName As String
Dim intAttr As Integer
Dim tmpAttr As Integer
intFileNo = FreeFile
strFileName = ActiveWorkbook.Path & "\test.txt"
If Dir(strFileName, vbReadOnly Or vbHidden Or vbSystem) <> "" Then
' readonly, hidden, system 属性はそのまま扱えないのでいったん属性を消して処理
intAttr = GetAttr(strFileName)
tmpAttr = intAttr Xor (intAttr And (vbReadOnly Or vbHidden Or vbSystem))
SetAttr strFileName, tmpAttr
Else
intAttr = vbNormal
End If
Open strFileName For Output As #intFileNo
Print #intFileNo, "data"
Close #intFileNo
SetAttr strFileName, intAttr
End Sub
作成 2012.02.08
更新 2012.02.08
更新 2012.02.08
Excel VBAでテキストファイルに書き込む
コード
読み取り専用、隠しファイル、システムファイルはそのまま扱えないため、一旦属性を取り消し、処理したあと属性を再設定します。
タグ: Excel