我最近在用WINCC 7。0 SP3 AISA做一个用户数据记录归档。我想做一个一直在记录的,当我选取一个时间段查绚的时候,它自动给我生成一个EXCEL 表格存在一个地方。我做一个按钮,按钮里面写的脚本,不知道什么原因实现不了,请大家帮我看看这个是什么原因,有类似的实例给我看下也行,急用!先谢了。我写的脚本如下:
Sub OnClick(ByVal Item)
Dim sPro,sDsn,sSer,sCon,conn,sSql,oRs,oCom
Dim tagDSNName
Dim m,i
Dim sqldc
Dim LocalBeginTime,LocalEndTime,UTCBeginTime,UTCEndTime,sVal
Dim objExcelApp,objExcelBook,objExcelSheet,sheetname
item.Enabled=False
On Error Resume Next
sheetname="Sheet1"
Set objExcelApp=CreateObject("Excel.Application")
objExcelApp.Visible=False
objExcelApp.Workbooks.Open "f:\rec\mod.xls"
objExcelApp.Worksheet(sheetname).Active
Set tagDSNName=HMIRuntime.Tags("@DatasourceNameRT")
tagDSNName.Read
Set LocalBeginTime=HMIRuntime.Tags("strBeginTime")
LocalBeginTime.Read
Set LocalEndTime=HMIRuntime.Tags("strEndTime")
LocalEndTime.Read
Set sqldc=HMIRuntime.Tags("SQLzdh")
sqldc.Read
UTCBeginTime=DateAdd("h",-8,LocalBeginTime.Value)
UTCEndTime=DateAdd("h",-8,LocalEndTime.Value)
UTCBeginTime=Year(UTCBeginTime)&"-"&Month(UTCBeginTime)&"-"&Day(UTCBeginTime)&" "&Hour(UTCBeginTime)&":"&Minute(UTCBeginTime)&":"&Second(UTCBeginTime)
UTCEndTime=Year(UTCEndTime)&"-"&Month(UTCEndTime)&"-"&Day(UTCEndTime)&" "&Hour(UTCEndTime)&":"&Minute(UTCEndTime)&":"&Second(UTCEndTime)
HMIRuntime.Trace "UTC Begin Time:"&UTCBeginTime&vbCrLf
HMIRuntime.Trace "UTC End Time:"&UTCEndTime&vbCrLf
Set sVal=HMIRuntime.Tags("sVal")
sVal.Read
sPro="Provider=WINCCOLEDBProvider.1;"
sDsn="Catalog=CC_rec_13_02_20_09_51_17R;"
sSer="Data Source=.\WINCC"
sCon=sPro+sDsn+sSer
Set conn=CreateObject("ADODB.Connection")
conn.ConnectionString=sCon
conn.CursorLocation=3
conn.Open
'查询命令文本
sSql="SELECT * FROM UA#rec"
' "select * from UA#rec"
' "Tag:R,'rec\zdh','"+sqldc.Value+"'"
MsgBox "Open with"&vbCr &sCon &vbCr & sSql & vbCr,vbOKCancel
Set oRs=CreateObject("ADODB.Recordset")
Set oCom=CreateObject("ADODB.Command")
oCom.CommandType=8
Set oCom.ActiveConnection=conn
oCom.CommandText=sSql
Set oRs=oCom.Execute
m=oRs.RecordCount
If(m>0) Then
objExcelApp.Woeksheets(sheetname).cells(2,1).value=oRs.Fields(0).Name
oRs.MoveFirst
i=3
Do While Not oRs.EOF
objExcelApp.Woeksheets(sheetname).cells(i,1).value=oRs.Fields(0).Value
oRs.MoveNext
i=i+1
Loop
oRs.Close
Else
MsgBox "没有所需数据......"
item.Enabled=True
Set oRs =Nothing
conn.Close
Set conn =Nothing
Dim patch,filename
filename=CStr(Year(Now))&CStr(Month(Now))&CStr(Hour(Now))&CStr(Minute(Now))
patch="d:\"&filename&".xls"
objExcelApp.Workbooks.SaveAs patch
objExcelApp.Workbooks.Close
objExcelApp.Quit
Set objExcelApp =Nothing
'Exit Sub
End If
End Sub
图片说明:
最佳答案
可参考:《WinCC技巧》
http://wenku.baidu.com/view/b239190bbb68a98271fefac2.html
《wincc7.0按钮的问题》
http://www.ad.siemens.com.cn/service/answer/solution.aspx?Q_ID=55480&cid=1032
《WINCC 7.0用户归档的问题》
http://www.ad.siemens.com.cn/service/answer/solution.aspx?Q_ID=61652&cid=1032
提问者对于答案的评价:
thank you
专家置评
您可以查看WinCC的帮助,在选件Connectivity Pack中有相关连接数据库等样例。
原创文章,作者:more0621,如若转载,请注明出处:https://www.zhaoplc.com/plc220483.html