wincc6.2,在线表格,旁用一按钮,单机按钮,保存打印excel表格,写了vb脚本,但是单机按钮表格是空的不知哪里有问题?excel表格事先做好样式,这样式有什么特别做法吗还是随便填写文字?样式表格放在C盘,但是单机按钮excel表格就保持在E盘了。
Sub OnLButtonDown(ByVal Item, ByVal Flags,ByVal x, ByVal y)
Dim objExcelApp,objExcelBook,objexcelsheet
Dim m1
Dim m2
Dim m3
Dim m4
Dim i,j
Dim tagshijian,sheetname,username
Dim msg
Set m1 = HMIRuntime.Tags("motor")
Set m2 = HMIRuntime.Tags("tan")
Set m3 =HMIRuntime.Tags("motor_actual")
Set m4 =HMIRuntime.Tags("TankLevel")
Set username =HMIRuntime.Tags("@CurrentUserName")
msg="ok"
sheetname="sheetdemo"
On Error Resume Next
Dim ExcelApp,ExcelBook
SetExcelApp=GetObject(,"Excel.Application")
IfTypeName(ExcelApp)="Application" Then
For Each ExcelBook In ExcelApp.WorkBooks
IfExcelBook.FullName="C:\book1.xls" Then
ExcelApp.ActiveWorkbook.Save
ExcelApp.Workbooks.Close
ExcelApp.Quit
Set ExcelApp=Nothing
Exit For
End If
Next
End If
SetobjExcelApp=CreateObject("Excel.Application")
objExcelApp.Visible=True
objExcelApp.Workbooks.Open"C:\book1.xls"
objExcelApp.Worksheets(sheetname).Activate
With objExcelApp.Worksheets(sheetname)
For i=5 To 30
For j=1 To 5
.cells(i,j)=Null
Next
Next
For i=31 To 31
For j=1 To 4
.cells(i,j)=Null
Next
Next
End With
tagshijian=Now
objExcelApp.Worksheets(sheetname).cells(2,2).value=tagshijian
username.Read
objExcelApp.Worksheets(sheetname).cells(2,5).value=username.Value
For i=5 To 30
with objExcelApp.Worksheets(sheetname)
.cell(i,1).value=tagshijian
m1.Read
.cell(i,2).value=m1.Value
m2.Read
.cell(i,3).value=m2.Value
m3.Read
.cell(i,4).value=m3.Value
m4.Read
.cell(i,5).value=m4.Value
End With
Next
MsgBox msg
Dim patch,filename
filename=CStr(Year(Now))&CStr(Month(Now))&CStr(Day(Now))+CStr(Hour(Now))&CStr(Minute(Now))
patch="E:\"&filename&".xls"
objExcelApp.ActiveWorkbook.SaveAs patch
objExcelApp.Workbooks.Close
objExcelApp.Quit
Set objExcelApp=Nothing
End Sub