如何通过vbs在wincc的画面中写入excel中r1c1位的字符串

在wincc画面中建立一个i/o域,如何用vbs脚本写入(读出)excel中的数值,只要有一个位置r1c1的数值就可以了。
新手,一点方向都没有
请问:
1、脚本是在全局脚本写还是在对象属性写
2、脚本的基本例子给一个,越详细越好
3、链接的文本看了不少,没有方向

最佳答案

本人做的源代码:
Sub OnClick(ByVal Item)                                     
Dim fso,myfile
Set fso = CreateObject("scripting.FileSystemObject")
Set MyFile = fso.GetFile("d:\\data.xlsx")
Dim ObjExcelApp
Set objExcelApp = CreateObject("Excel.Application")
‘objExcelApp.Visible = True
objExcelApp.Workbooks.Open MyFile
‘上面的程序段是为了打开d盘excel文件
Dim aa_data
objExcelApp.worksheets ("sheet1").Cells(2, 9).VAlue = HMIRuntime.Tags("aa").read
Dim i,j
If objExcelApp.worksheets ("sheet1").Cells(2, 9).VAlue>1.0 Then
i=2
j=6
End If
If objExcelApp.worksheets ("sheet1").Cells(2, 9).VAlue>0.8 And  objExcelApp.worksheets ("sheet1").Cells(2, 9).VAlue<1.0 Then
i=7
j=11
End If
If objExcelApp.worksheets ("sheet1").Cells(2, 9).VAlue>0.6 And  objExcelApp.worksheets ("sheet1").Cells(2, 9).VAlue<0.8 Then
i=12
j=16
End If
If objExcelApp.worksheets ("sheet1").Cells(2, 9).VAlue<0.6Then
i=17
j=21
End If
‘判断数据保存位置
Do While i<j
objExcelApp.worksheets ("sheet1").Cells(i, 1).VAlue =objExcelApp.worksheets ("sheet1").Cells(i+1, 1).VAlue
objExcelApp.worksheets ("sheet1").Cells(i, 2).VAlue = objExcelApp.worksheets ("sheet1").Cells(i+1, 2).VAlue
i=i+1 
Loop 
objExcelApp.worksheets ("sheet1").Cells(i, 1).VAlue =HMIRuntime.Tags("yy").read
objExcelApp.worksheets ("sheet1").Cells(i, 2).VAlue =HMIRuntime.Tags("xx").read

‘写数据到excel表格
objExcelApp.ActiveWorkbook.Save
‘保存表格
Dim cons_data,ax1_data,ax2_data,ax3_data,ax4_data,ax5_data,ax6_data
Set cons_data=HMIRuntime.Tags("cons")
Set ax1_data=HMIRuntime.Tags("ax1")
Set ax2_data=HMIRuntime.Tags("ax2")
Set ax3_data=HMIRuntime.Tags("ax3")
Set ax4_data=HMIRuntime.Tags("ax4")
Set ax5_data=HMIRuntime.Tags("ax5")
Set ax6_data=HMIRuntime.Tags("ax6")


cons_data.Value = objExcelApp.worksheets ("sheet1").Cells(23,7).value
ax1_data.Value = objExcelApp.worksheets ("sheet1").Cells(23, 6).value
ax2_data.Value = objExcelApp.worksheets ("sheet1").Cells(23, 5).value
ax3_data.Value = objExcelApp.worksheets ("sheet1").Cells(23, 4).value
ax4_data.Value = objExcelApp.worksheets ("sheet1").Cells(23, 3).value
ax5_data.Value = objExcelApp.worksheets ("sheet1").Cells(23, 2).value
ax6_data.Value = objExcelApp.worksheets ("sheet1").Cells(23, 1).value
‘上面的作用是将Excel表格中的数据读到wincc,存到临时变量里面
objExcelApp.Workbooks.Close
objExcelApp.Quit
Set ObjEXceLapp = Nothing
‘到这里为止,关闭刚才打开的excel程序了
cons_data.Write 
ax1_data.Write 
ax2_data.Write 
ax3_data.Write 
ax4_data.Write 
ax5_data.Write 
ax6_data.Write 
‘最后是把读到的excel值从临时变量中写入它对应的wincc变量中


End Sub

提问者对于答案的评价:
谢谢。

原创文章,作者:more0621,如若转载,请注明出处:https://www.zhaoplc.com/plc264900.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2019年6月11日
下一篇 2019年6月11日

相关推荐