技成培训网 技成学习APP 技成工具箱 技成企业服务

欢迎回到技成培训网

技成培训网

电工题库宝典

技成电工课堂

PLC练习题

技成PLC课堂

变频器故障查询

技成PLC学吧

伺服驱动故障查询

技成PLC网校

当前位置:技成首页 > 文章中心 > 技成百科 > 如何通过vbs在wincc的画面中写入excel中r1c1位的字符串

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

刘玉蓉 发布于2014-10-28 17:42 1 0 标签:

在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

若有收获,就点个赞吧!

文章来源于网络及文献如有侵权请联系站长
分享到:

微信扫一扫分享

阅读与本文标签相同的文章

相关推荐

最新文章 热门文章
最新课程 免费课程
PLC资料下载 电工电气资料下载
应用新秀
变频器故障代码查询APP
伺服故障代码查询APP
技成
手机APP
热门标签