发布于 2011-03-17 13:00:06
6楼
'定义变量(很重要,不定义的话 自建vbs文件运行没问题,但在WinCC中就运行不了。这个地方让我浪费不少时间)
Dim Hr,Min,half,C_Date,MM,DD,i
Dim N_Column,reportName
Dim xlApp,xlBook,xlSheet
Dim Tag(50)
'分钟不等于0或30时退出(每整半小时自动记录一次数据)
Hr=Hour(Now)
Min=Minute(Now)
If Min = 0 Then
half = 0
Else
If Min = 30 Then
half = 1
else
Ws cript.Quit
End If
End If
'根据日期确定excel文件名(我的记录是早上6点~第二天早6点)
If hr < 6 Then
C_date = Date-1
N_Column = 43 + hr * 2 + half
Else
C_date = Date
N_Column = 7 + (hr - 6) * 2 + half
End If
MM=Month(C_date)
DD=Day(C_date)
If Len(MM)=1 Then
MM="0"&MM
end if
if len(DD)=1 then
DD="0"&DD
end if
reportName=Year(C_date)&MM&DD&".xls"
'每天6:00创建文件(通过copy样式文件来创建,并设置为只读属性,只读属性很重要)(也可纯vbs创建 但excel内格式等设置繁琐 没必要)(若存在同名文件则将之删除)
if ( hr=6 and half=0 ) then
if (CreateObject("s cripting.FileSystemObject").FileExists("d:\WinCC_Report\"&reportName)) Then
CreateObject("s cripting.FileSystemObject").getfile("d:\WinCC_Report\"&reportName).Attributes=0
CreateObject("s cripting.FileSystemObject").DeleteFile("d:\WinCC_Report\"&reportName),true
end if
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open("D:\WinCC_Report\Templet\2D1FAHU.xls")
Set xlSheet = xlBook.Worksheets("Data")
xlSheet.Cells(2, 2).NumberFormatLocal = "@"
xlSheet.Cells(2, 2).Value = Date&" "&Time
xlbook.Saveas("d:\WinCC_Report\"&reportName)
xlbook.Close
xlApp.Quit
Set xlApp = Nothing
CreateObject("s cripting.FileSystemObject").getfile("d:\WinCC_Report\"&reportName).Attributes=1
end if
'若文件夹内无该日期文件 则自动创建文件(上个程序段其实也可以删除)
if (CreateObject("s cripting.FileSystemObject").FileExists("d:\WinCC_Report\"&reportName)) Then
else
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open("D:\WinCC_Report\Templet\2D1FAHU.xls")
Set xlSheet = xlBook.Worksheets("Data")
xlSheet.Cells(2, 2).NumberFormatLocal = "@"
xlSheet.Cells(2, 2).Value = Date&" "&Time
xlbook.Saveas("d:\WinCC_Report\"&reportName)
xlbook.Close
xlApp.Quit
Set xlApp = Nothing
CreateObject("s cripting.FileSystemObject").getfile("d:\WinCC_Report\"&reportName).Attributes=1
end if
'读取WinCC实时数据,WinCC刚激活时,WinCC自身更新Tag时间会很长。我等这一步完成等了5分钟。下个时间段就正常。
'这个地方也浪费我太多时间 以为程序错了 后来设置很多msgbox才找出原因
Tag(3)=HMIRuntime.Tags("FAB_DB215.DBD132").read
Tag(4)=HMIRuntime.Tags("FAB_DB215.DBD128").read
Tag(5)=HMIRuntime.Tags("FAB_DB215.DBD64").read
Tag(6)=HMIRuntime.Tags("FAB_DB312.DBD8").read
Tag(7)=HMIRuntime.Tags("FAB_DB312.DBD4").read
Tag(8)=HMIRuntime.Tags("FAB_DB312.DBD0").read
Tag(9)=HMIRuntime.Tags("FAB_DB312.DBD12").read
Tag(10)=HMIRuntime.Tags("FAB_DB215.DBD136").read
Tag(11)=HMIRuntime.Tags("FAB_DB312.DBD16").read
Tag(12)=HMIRuntime.Tags("FAB_DB312.DBD20").read
Tag(13)=HMIRuntime.Tags("FAB_DB215.DBD140").read
Tag(14)=HMIRuntime.Tags("FAB_DB312.DBD24").read
Tag(15)=HMIRuntime.Tags("FAB_DB312.DBD28").read
Tag(16)=HMIRuntime.Tags("FAB_DB215.DBD144").read
Tag(17)=HMIRuntime.Tags("FAB_DB312.DBD32").read
Tag(18)=HMIRuntime.Tags("FAB_DB312.DBD36").read
Tag(19)=HMIRuntime.Tags("FAB_DB215.DBD148").read
Tag(20)=HMIRuntime.Tags("FAB_DB312.DBD40").read
Tag(21)=HMIRuntime.Tags("FAB_DB215.DBD152").read
Tag(22)=HMIRuntime.Tags("FAB_DB312.DBD44").read
Tag(23)=HMIRuntime.Tags("FAB_DB215.DBD156").read
Tag(24)=HMIRuntime.Tags("FAB_DB312.DBD48").read
Tag(25)=HMIRuntime.Tags("FAB_DB215.DBD160").read
Tag(26)=HMIRuntime.Tags("FAB_DB312.DBD52").read
Tag(27)=HMIRuntime.Tags("FAB_DB215.DBD164").read
Tag(28)=HMIRuntime.Tags("FAB_DB312.DBD56").read
Tag(29)=HMIRuntime.Tags("FAB_DB215.DBD168").read
Tag(30)=HMIRuntime.Tags("FAB_DB215.DBD68").read
Tag(31)=HMIRuntime.Tags("FAB_DB312.DBD60").read
Tag(32)=HMIRuntime.Tags("FAB_DB312.DBD64").read
Tag(33)=HMIRuntime.Tags("FAB_DB215.DBD172").read
Tag(34)=HMIRuntime.Tags("FAB_DB312.DBD68").read
Tag(35)=HMIRuntime.Tags("FAB_DB312.DBD72").read
Tag(36)=HMIRuntime.Tags("FAB_DB215.DBD176").read
Tag(37)=HMIRuntime.Tags("FAB_DB215.DBD72").read
Tag(38)=HMIRuntime.Tags("FAB_DB312.DBD76").read
Tag(39)=HMIRuntime.Tags("FAB_DB312.DBD80").read
Tag(40)=HMIRuntime.Tags("FAB_DB215.DBD180").read
Tag(41)=HMIRuntime.Tags("FAB_DB312.DBD84").read
Tag(42)=HMIRuntime.Tags("FAB_DB215.DBD184").read
Tag(43)=HMIRuntime.Tags("FAB_DB312.DBD88").read
Tag(44)=HMIRuntime.Tags("FAB_DB215.DBD188").read
Tag(45)=HMIRuntime.Tags("FAB_DB312.DBD92").read
Tag(46)=HMIRuntime.Tags("FAB_DB215.DBD192").read
Tag(47)=HMIRuntime.Tags("FAB_DB312.DBD96").read
Tag(48)=HMIRuntime.Tags("FAB_DB215.DBD196").read
Tag(49)=HMIRuntime.Tags("FAB_DB312.DBD100").read
Tag(50)=HMIRuntime.Tags("FAB_DB312.DBD104").read
'输入数据
CreateObject("s cripting.FileSystemObject").getfile("d:\WinCC_Report\"&reportName).Attributes=0
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open("d:\WinCC_Report\"&reportName)
Set xlSheet = xlBook.Worksheets("Data")
for i=3 to 50
xlSheet.Cells(i,N_Column).Value = Tag(i)
Next
xlbook.Save
xlbook.Close
xlApp.Quit
Set xlApp = Nothing
CreateObject("s cripting.FileSystemObject").getfile("d:\WinCC_Report\"&reportName).Attributes=1
'里面还缺少 样本文件不存在 相应的报错程序
'WinCC有关于vbs的帮助文件
'在WinCC项目中创建VBS动作 每分钟运行一次