VBA
◆写文件
Dim sFile As Object, fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
sFile.WriteLine (String(8, " ") & "<Request>")
sFile.Close
Set sFile = Nothing
Set fso = Nothing
◆选择文件夹
With Application.FileDialog(msoFileDialogFolderPicker)
If .Show = -1 Then
folder = .SelectedItems(1)
End If
End With
MsgBox folder & "/TaskInfo.xml"
◆循环/IF
Do While Cells(rowIndex, 2).Value <> ""
For compCol = 8 To 17 Step 1
If idStr < 10 Then
sFile.WriteLine (String(12, " ") & "<Complete id=""0" & idStr & """ />")
Else
sFile.WriteLine (String(12, " ") & "<Complete id=""" & idStr & """ />")
End If
Next compCol
Loop
◆完整
Sub CreateFile()
Dim sFile As Object, fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
folder = "D:"
With Application.FileDialog(msoFileDialogFolderPicker)
If .Show = -1 Then
folder = .SelectedItems(1)
End If
End With
MsgBox folder & "/TaskInfo.xml"
Set sFile = fso.CreateTextFile(folder & "/TaskInfo.xml", True)
sFile.WriteLine ("<?xml version=""1.0"" encoding=""UTF-8""?>")
sFile.WriteLine ("<Tasks>")
rowIndex = 4
Do While Cells(rowIndex, 2).Value <> ""
‘ タスクID
taskId = Cells(rowIndex, 2).Value
‘ 家電対象
obj = Cells(rowIndex, 4).Value
sFile.WriteLine (String(4, " ") & "<Task id=""" & taskId & """ obj=""" & obj & """>")
sFile.WriteLine (String(8, " ") & "<Request>")
For compCol = 8 To 17 Step 1
‘ 補完ID
compId = Cells(rowIndex, compCol).Value
If compId <> "" Then
idStr = compCol - 7
If idStr < 10 Then
sFile.WriteLine (String(12, " ") & "<Complete id=""0" & idStr & """ />")
Else
sFile.WriteLine (String(12, " ") & "<Complete id=""" & idStr & """ />")
End If
End If
Next compCol
sFile.WriteLine (String(8, " ") & "</Request>")
‘ URL
URL = Cells(rowIndex, 5).Value
‘ Path
Path = Cells(rowIndex, 6).Value
‘ Method
method = Cells(rowIndex, 7).Value
sFile.WriteLine (String(8, " ") & "<Api url=""" & URL & """ path=""" & Path & """ method=""" & method & """ >")
sFile.WriteLine (String(12, " ") & "<Input>")
‘ 入力定義
inputText = Cells(rowIndex, 18).Value
sFile.WriteLine (inputText)
sFile.WriteLine (String(12, " ") & "</Input>")
sFile.WriteLine (String(8, " ") & "</Api>")
sFile.WriteLine (String(8, " ") & "<Response>")
‘ 出力応答文種別
outputStatus = Cells(rowIndex, 19).Value
sFile.WriteLine (outputStatus)
sFile.WriteLine (String(8, " ") & "</Response>")
sFile.WriteLine (String(4, " ") & "</Task>")
sFile.WriteLine
rowIndex = rowIndex + 1
Loop
sFile.WriteLine ("</Tasks>")
sFile.Close
Set sFile = Nothing
Set fso = Nothing
End Sub
Sub DeleteFile()
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
fso.DeleteFile ("D:/TestFile.txt")
End Sub
原文地址:https://www.cnblogs.com/xuemanjiangnan/p/8378744.html