文档库 最新最全的文档下载
当前位置:文档库 › VB编程实例

VB编程实例

VB编程实例
VB编程实例

一、导入到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)

file.WriteLine(HMIRuntime.Tags("Var_5").read)

File.Close

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

End Sub

二、从TXT文件中读取数据到WinCC变量

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

Dim fso

Dim txtfile

Dim a

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

Set fso = CreateObject("scripting.filesystemobject")

If fso.FileExists("D:\\Export&Import\\"&CStr(a)&".txt") Then

Set txtfile = fso.OpenTextFile("D:\\Export&Import\\"&CStr(a)&".txt") HMIRuntime.Tags("Var_1").Write txtfile.ReadLine

HMIRuntime.Tags("Var_2").Write txtfile.ReadLine

HMIRuntime.Tags("Var_3").Write txtfile.ReadLine

HMIRuntime.Tags("Var_4").Write txtfile.ReadLine

HMIRuntime.Tags("Var_5").Write txtfile.ReadLine

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

txtfile.Close

Else

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

End if

End Sub

相关文档