Đăng Ký
Kết quả 71 đến 80 của 81

Threaded View

  1. #11
    Thành viên Mới
    Ngày tham gia
    Jul 2014
    Bài viết
    5
    Thanks
    0
    0 lượt trong 0 bài viết
    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

Đánh dấu

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
  •  
Back to Top