代码严格按照原版运行的,见后续图
Dim sPro, sDsn, sSer, sCon, sSql
Dim conn, oRs, oCom
Dim DSNName
Dim i As Integer
Dim sStart, sStop As String
Sub get_wincc_data()
'--Get Database DSN name-----------------------------------
Set DSNName = CreateObject("CCHMIRuntime.HMIRuntime")
sDsn = DSNName.Tags("@DatasourceNameRT").Read
'--build connection string-----------------------------------
sPro = "Provider=WinCCOLEDBProvider.1;"
sDsn = "Catalog=" & sDsn & ";"
sSer = "Data Source=YUER\WinCC"
MsgBox sSer
sCon = sPro & sDsn & sSer
MsgBox sCon
Set conn = CreateObject("ADODB.Connection")
conn.ConnectionString = sCon
conn.CursorLocation = 3
conn.Open
Set oRs = CreateObject("ADODB.Recordset")
Set oCom = CreateObject("ADODB.Command")
oCom.CommandType = 1
Set oCom.ActiveConnection = conn
'查询启止时间
sStart = Year(DTPicker1.Value) & "-" & Month(DTPicker1.Value) & "-" & Day(DTPicker1.Value) & " 00:00:00"
sStop = Year(DTPicker1.Value) & "-" & Month(DTPicker1.Value) & "-" & Day(DTPicker1.Value) & " 23:00:00"
MsgBox sStart
MsgBox sStop
'转为UTC时间
sStart = DateAdd("h", -8, CDate(sStart))
sStop = DateAdd("h", -8, CDate(sStop))
'读取Fan1_T1
sSql = "Tag:R,('ProcessValueArchive\Fan1_T1'),'" & sStart & "','" & sStop & "' order by datetime"
oCom.CommandText = sSql
Set oRs = oCom.Execute
If (oRs.EOF) Then
oRs.Close
Else
oRs.MoveFirst
i = 0
Do While Not oRs.EOF
Sheet1.Cells(i + 4, 2) = oRs.Fields("RealValue").Value
oRs.MoveNext
i = i + 1
Loop
oRs.Close
End If