2013-05-09 10 views
-1

VB 스크립트를 사용하여 WinCC Flex의 일부 태그 데이터를 Excel 파일에 로깅하려고합니다. 지멘스 포럼에서 검색 한 결과, 필자의 필요에 따라 스크립트를 얻었습니다. 그러나 WinCC Flex에서 스크립트를 실행하는 중에 오류가 있습니다. 오류를 판별 할 수없는VBScript를 사용하여 WinCC Flex에서 Excel 파일로 데이터 저장

TheTargetRow = .Cells(65535, 2).End(-4162).Row 

암 :이 스크립트를 실행하려고하면

Dim wsh, XLSrunning, TargetBookrunning, objExcelApp, objWorkbook, TheTargetBook, TheTargetBookName 
Dim TheCount 
Dim objFSO 
Const OverwriteExisting = 1 


Set wsh = CreateObject("WScript.Shell") 
    TheTargetBookName = "report.xls" 
    TheTargetBook = "D:\Out\" & TheTargetBookName 

'---------------[Modification#1_Begin]------------------------------------------- 
Set objFSO = CreateObject("Scripting.FileSystemObject") 
If Not objFSO.FileExists(TheTargetBook) Then 
     objFSO.CopyFile "D:\Out\Template.xls", 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]------- 
     With objWorkbook.ActiveSheet 

     TheTargetRow = .Cells(65535, 2).End(-4162).Row 
       .cells(TheTargetRow + 1, 2) = SmartTags("Tag_1") 
       .cells(TheTargetRow + 1, 3) = SmartTags("Tag_2") 
       .cells(TheTargetRow + 1, 4) = SmartTags("Tag_3")   

     End With 
     objWorkbook.Save 
    'objWorkbook.Close 

    Set objWorkbook = Nothing 
    'objExcelApp.Quit 
    Set objExcelApp = Nothing 
    'MsgBox "Done" 

Set wsh = Nothing 

이 컴파일러는 다음 줄에 오류를 보여줍니다으로

스크립트입니다. 불필요한 일을하십시오.

답변

1

표시되는 코드는 대부분 정확하지만 실제로 해당 줄의 문제는 WINCC 환경 내의 VBScript 인터페이스 구현과 관련이 있습니다.

Excel 워크 시트에 대한 참조를 유지하기 위해 "theSheet"라는 새 변수를 만든 경우에도 WinCC에서 해당 구문 검사 문제를 피할 수 있습니다.

이 방법을 사용하면 해당 셀 개체에 액세스 할 수 있지만 ".End (xlUp) .Row"속성의 값을 직접 반환 할 분명한 방법은 없다고 생각됩니다.

그러나 "행"값이 갖는 유일한 목적은 태그 값을 인쇄 할 행 번호를 가져 오는 것입니다. 다음 코드를 확인하고 결과에 어떤 느낌이 들었는지 확인하십시오.

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