Writing data from RSView SE to an XLS file using VBA

C

Thread Starter

Carlos Cordova

I'm trying to make a VBA code capable of taking a tag from RSView SE and then write it into a XLS file. The problem is that I've been looking for the correct way to manipulate Excel objects in RSView, But I can't find so much information about it. Can sombody tell me how to create workbooks, sheets and write to them and save a XLS file using VBA in RSView SE???

Thanks for the help!
 
I think the easiest thing to do is have Excel installed on the PC, then write the VB to open excel (in the background) modifie the cells then close it again. This way you wont have to worry about excel's file format.

Kevin
 
K
Hi,
Reading tags from RSView is not the easiest SCADA, but the VBA part to write to Excel and other objects is the same as any other. The key is to use Set ObjExcelApp = CreateObject("Excel.Application") to create an object instance of excel. Try embedding this into a vba action behind a button press;

private sub Button_press()

on error goto ErrorHandler

'Best to define variable first but not nessecary
Dim ObjExcelApp as object
Dim Fname as string

Fname = "C:\Test.xls" 'name of already created excel project

Set ObjExcelApp = CreateObject("Excel.Application")
ObjExcelApp.visible = false 'make run in background


ObjExcelApp.Workbooks.Open(Fname)

ObjExcelApp.Worksheets("Worksheet1").cells(1,"A").value = "123" 'Write date to cell A1

'you can read the propities of an object on a screen without reading from a tag
'ObjExcelApp.Worksheets("Worksheet1").cells(1,"A").value = Me.elements("Numeric_input1").value

'use this to run an excel macro
'ObjExcelApp.run("Macro_Name")

'save and exit excel
'I have had problems with silenty saving and closing Excel, this seems to vary from application to another, try;
ObjExcelApp.ActiveWorkbook.Save
ObjExcelApp.Workbooks.Close
ObjExcelApp.quit
Set ObjExcelApp = Nothing

exit sub

Error_handler
' write error to factory talk
LogDiagnosticsMessage Err.Number & " " & Err.Description, ftDiagSeverityError

end sub

private sub Button2_press()
'open an new excel project


'Best to define variable first but not nessecary
Dim ObjExcelApp as object
Dim Fname as string

Set objExcel = CreateObject("Excel.Application")

objExcel.Visible = True
objExcel.Workbooks.Add
objExcel.Cells(1, 1).Value = "Test value"

ObjExcelApp.ActiveWorkbook.SaveAs(Fname)
ObjExcelApp.WorkBooks(Fname).close(False)
ObjExcelApp.quit
Set ObjExcelApp = Nothing

end sub

I think the syntax is correct, if not very close (I do not have RSview installed at the moment). A good trick is to use VBscript and try it out on a pc first (you will not be able to access any RSView objects). Create a file with a .vbs extention and run the program (with out the 1st and last line and the dims).
If you need any other help let me know.

Kevin
 
Hi Kevin,
Do you have any code to take the data from an rslinx cell in excel and save that data every time the value changes to a column of data for unattended data collection. Source data is a test result integer file in a SLC500. I know how to get the data into excel, But I dont know how to save the data when it changes on the next test. Let me know PJ

[email protected]-lex.com
 
hi
I have tried this one by configuring DDE client that comes along with RsLinx long ago.I dont have rslinx now. But u will get commands to be entered from Excel's DDE help.
 
Top