2013-05-09 105 views
-1

正在尝试使用VB脚本将WinCC Flex某些标签的数据记录到Excel文件中。 我在西门子论坛上搜索过,并根据我的需要获得了一个脚本。 但在WinCC Flex中执行脚本时发生错误。使用VBScript将数据从WinCC Flex存储到Excel文件

的脚本如下:

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 

的时候,我试图执行这个脚本,编译器显示以下行错误:

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

我无法判断错误。请只做那些需要的。

回答

1

您显示的代码大部分都是正确的,但实际上该行中的问题与WINCC环境内的VBScript接口的实现有关。

如果您创建了一个名为“theSheet”的新变量来保存对Excel 工作表的引用,那么您仍然可以避免在WinCC中进行语法检查问题。

这种方式可以让你从它访问它的Cells对象,但在我看来,没有明显的方式直接返回“.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 
相关问题