VB编程实例

合集下载
相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

一、导入到Xls文件并打印

Sub OnLButtonUp(ByVal Item, ByVal Flags, ByVal x, ByVal y)

Dim a,fso

a=HMIRuntime.Tags("FileName").Read

Set fso = CreateObject("scripting.filesystemobject")

If fso.FileExists("C:\Model.xls") Then

Dim objExcelApp

Set objExcelApp = CreateObject("Excel.Application")

objExcelApp.Visible = False

objExcelApp.Workbooks.Open "C:\Model.xls"

objExcelApp.Cells(2, 3).Value = HMIRuntime.Tags("NewTag1_1").read

objExcelApp.Cells(4, 5).Value = HMIRuntime.Tags("NewTag1_2").read

objExcelApp.Cells(6, 7).Value = HMIRuntime.Tags("NewTag1_3").read

objExcelApp.Cells(8, 9).Value = HMIRuntime.Tags("NewTag1_4").read

objExcelApp.Cells(10, 11).Value = HMIRuntime.Tags("NewTag1_5").read objExcelApp.ActiveWorkbook.SaveAs("C:\Report\"&CStr(a)&".xls")

objExcelApp.ActiveWorkbook.PrintOut

objExcelApp.Workbooks.Close

objExcelApp.Quit

Set objExcelApp = Nothing

MsgBox "文件已经成功导出/Export Successful"

Else

MsgBox "Excel模板文件不存在"

End if

End Sub

二、从Xls文件导入或者查询

Sub OnLButtonUp(Byval Item, Byval Flags, Byval x, Byval y)

Dim a,fso

a=HMIRuntime.Tags("FileName").Read

Set fso = CreateObject("scripting.filesystemobject")

If fso.FileExists("C:\Report\"&CStr(a)&".xls") Then

Dim objExcelApp

Set objExcelApp = CreateObject("Excel.Application")

objExcelApp.Visible = False

objExcelApp.Workbooks.Open "C:\Report\"&CStr(a)&".xls"

HMIRuntime.Tags("NewTag1_1").Write objExcelApp.Cells(2, 3).Value

HMIRuntime.Tags("NewTag1_2").Write objExcelApp.Cells(4, 5).Value

HMIRuntime.Tags("NewTag1_3").Write objExcelApp.Cells(6, 7).Value

HMIRuntime.Tags("NewTag1_4").Write objExcelApp.Cells(8, 9).Value

HMIRuntime.Tags("NewTag1_5").Write objExcelApp.Cells(10, 11).Value objExcelApp.ActiveWorkbook.Save

objExcelApp.Workbooks.Close

objExcelApp.Quit

Set objExcelApp = Nothing

MsgBox "导入数据成功/Import Successful"

Else

MsgBox "文件不存在/File is not existing"

End if

End Sub

3、WinCC如何实现带确认的按钮操作

网上多是介绍C脚本实现的方法,其实VB脚本的实现更简单,代码如下:

Sub OnLButtonUp(Byval Item, Byval Flags, Byval x, Byval y)

If MsgBox("提示内容",1,"提示标题")=1 Then

HMIRuntime.Tags("TestTag").Write 1

Else

HMIRuntime.Tags("TestTag").Write 0

End If

End Sub

说明

MsgBox("提示内容",1,"提示标题")中的1为消息窗口中按钮的类型

0=vbOKonly

1=vbOKCancel

2=vbAbortRetryIgnore

3=vbYesNoCancel

4=vbYesNo

一、将WinCC变量导出到TEXT文件

Sub OnLButtonUp(ByVal Item, ByVal Flags, ByVal x, ByVal y)

Dim fso,File

Dim a

a=HMIRuntime.Tags("FileName").Read

Const ForWriting = 2

Set fso = CreateObject("Scripting.FileSystemObject")

Set File = fso.OpenTextFile("D:\Export&Import\"&CStr(a)&".txt", ForWriting, True) File.WriteLine(HMIRuntime.Tags("Var_1").read)

File.WriteLine(HMIRuntime.Tags("Var_2").read)

File.WriteLine(HMIRuntime.Tags("Var_3").read)

File.WriteLine(HMIRuntime.Tags("Var_4").read)

相关文档
最新文档