Код:
Option Explicit
Const xlCSV = 6
Const xlWindows = 2
Dim strTemplateFile
Dim strSourceFile
Dim strDestFolder
Dim objFSO
Dim objExcel
Dim objTemplateFile
Dim objSourceFile
Dim i
Dim strDestFile
Dim anyValue
strTemplateFile = "C:\Мои проекты\0191\Архив\Архив\Шаблон\check.csv"
strDestFolder = "C:\Мои проекты\0191\Архив\Архив\Итог"
If WScript.Arguments.Count = 1 Then
strSourceFile = WScript.Arguments.Item(0)
Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(strTemplateFile) Then
If objFSO.FileExists(strSourceFile) Then
If objFSO.FolderExists(strDestFolder) Then
Set objExcel = WScript.CreateObject("Excel.Application")
objExcel.Workbooks.OpenText strTemplateFile, , , , , , , , , , , , , , , , , True
Set objTemplateFile = objExcel.Workbooks.Item(1)
Set objSourceFile = objExcel.Workbooks.Open(strSourceFile, False, True)
For i = 1 To objSourceFile.Worksheets.Item(1).UsedRange.Rows.Count - 2
With objTemplateFile.Worksheets.Item(1)
anyValue = objSourceFile.Worksheets.Item(1).Cells.Item(i + 1, 5).Value
.Range("B3").Value = anyValue
.Range("F2").Value = anyValue
anyValue = objSourceFile.Worksheets.Item(1).Cells.Item(i + 1, 6).Value
.Range("D3").Value = anyValue
.Range("D4").Value = anyValue
.Range("H3").Value = anyValue
.Range("L3").Value = Round((anyValue * 18) / 118, 1)
End With
strDestFile = objFSO.BuildPath(strDestFolder, objFSO.GetBaseName(strTemplateFile) & "_" & objFSO.GetBaseName(strSourceFile) & "_" & Right("000" & CStr(i), 3) & "." & objFSO.GetExtensionName(strTemplateFile))
If objFSO.FileExists(strDestFile) Then
objFSO.DeleteFile strDestFile, True
End If
objTemplateFile.SaveAs strDestFile, xlCSV, , , , , , , , , , True
Next
objSourceFile.Close False
objTemplateFile.Close False
objExcel.Quit
Set objExcel = Nothing
Else
WScript.Echo "Can't find destination folder [" & strDestFolder & "]."
WScript.Quit 4
End If
Else
WScript.Echo "Can't find source file [" & strSourceFile & "]."
WScript.Quit 3
End If
Else
WScript.Echo "Can't find template file [" & strTemplateFile & "]."
WScript.Quit 2
End If
Set objFSO = Nothing
Else
WScript.Echo "Usage: cscript.exe //nologo """ & WScript.ScriptName & """ <Source file>"
WScript.Quit 1
End If
WScript.Quit 0