-
Thành viên Mới
Em có 1 đoạn code VB Script Lưu trữ dữ liệu từ WinCC Flex vào tập tin Excel bằng cách sử dụng VBScript : em đã chép nó vào Script WinCC Flex và đưa nó vào Event của IO Field, sau đó e tìm cái file Excel nó suất ra ở đâu em không biết.
Em cũng mới tìm hiểu VB Script nên đọc code chưa được hiểu rõ mong các anh trong diễn đàn giúp đỡ em.
Code VB Script: http://stackoverflow.com/questions/1...using-vbscript
Dim wsh, XLSrunning, TargetBookrunning, objExcelApp, objWorkbook, TheTargetBook, TheTargetBookName
Dim TheCount, theSheet, theCell, theLastCell, theLastRow
Dim objFSO
Const OverwriteExisting = 1
Set wsh = CreateObject("WScript.Shell")
'TheTargetBookName = "report.xls"
'TheTargetBook = "D:\Out\" & TheTargetBookName
TheTargetBookName = "report.xls"
TheTargetBook = "f:\work\plc\" & TheTargetBookName
TheTargetBookName = "c:\" & TheTargetBookName
'---------------[Modification#1_Begin]-------------------------------------------
Set objFSO = CreateObject("Scripting.FileSystemObject")
If Not objFSO.FileExists(TheTargetBook) Then
objFSO.CopyFile TheTargetBookName, TheTargetBook, OverwriteExisting
'HMIRuntime.Trace "The file," & TheTargetBook & ", does not exist." & vbCrLf & "I've just created one for you!"
End If
Set objFSO = Nothing
'---------------[Modification#1_End]--------------------------------------------
TheCount = GetObject("winmgmts:root\CIMV2").ExecQuery("SELECT * FROM Win32_Process WHERE Name='EXCEL.EXE'").Count
'While TheCount is bigger than 0, it means the Excel Application is running..., but doesn't mean the workbook is open for sure!
If TheCount > 0 Then
Set objExcelApp = GetObject(,"Excel.Application")
' Using GetObject(,"Excel.Application") to point to the running Excel Application.
TargetBookrunning = 0
For Each XLSrunning In objExcelApp.Workbooks
If XLSrunning.name = TheTargetBookName Then
TargetBookrunning = 1
End If
Next
If TargetBookrunning = 1 Then
Set objWorkbook = GetObject(TheTargetBook)
Else
Set objWorkbook = objExcelApp.Workbooks.Open(TheTargetBook)
End If
Else
Set objExcelApp = CreateObject("Excel.Application")
Set objWorkbook = objExcelApp.Workbooks.Open(TheTargetBook)
End If
objExcelApp.Visible = True
objExcelApp.ScreenUpdating = True
objExcelApp.DisplayAlerts = True
Dim TheTargetRow ' <------[Modification#2]-------
Set theSheet = objWorkbook.ActiveSheet
With theSheet
Set theCell = theSheet.Cells(65535,2)
Set theLastCell = theCell.end(-4162)
theLastRow = theLastCell.row
.cells(theLastRow + 1, 1) = formatdatetime( now,vbShortDate) & ", " & formatdatetime( now,vbLongTime)
.cells(theLastRow + 1, 2) = SmartTags("Tag_1")
.cells(theLastRow + 1, 3) = SmartTags("Tag_2")
.cells(theLastRow + 1, 4) = SmartTags("Tag_3")
End With
objWorkbook.Save
'objWorkbook.Close
Set objWorkbook = Nothing
'objExcelApp.Quit
Set objExcelApp = Nothing
'MsgBox "Done"
Set wsh = Nothing
Lần sửa cuối bởi vanviet, ngày 08-13-2014 lúc 11:54 PM.
-
Tag của Chủ đề này
Quyền viết bài
- Bạn Không thể gửi Chủ đề mới
- Bạn Không thể Gửi trả lời
- Bạn Không thể Gửi file đính kèm
- Bạn Không thể Sửa bài viết của mình
-
Nội quy - Quy định
Back to Top
Múi giờ GMT +7. Bây giờ là 04:23 AM.
Phiên bản 4.2.5
Phát triển bởi thành viên PLCVietNam®.
Đánh dấu