VBScript for Excel: How do I choose the source data (.SetSourceData)? -


i've searched answer question on both google , here, without success. if it's been asked before, apologized.

i'm trying automate administration tasks using vbscript. purpose of particular script take usage statistics text file (with text , number columns) , make line chart out of data. creating excel file , loading data works fine, i'm having trouble creating chart: don't understand how choose source data, , keep running syntax errors. there's plenty of info on internet on how in vba, , it's obvious recorded macro. can't vbscript. here's code (some file names , such have been changed privacy reasons):

set objfso = createobject("scripting.filesystemobject")  ' create instance of excel (keep program hidden) , open text data file set objexcel = createobject("excel.application")  objexcel .visible = false .workbooks.opentext("datafile.txt") end  ' name current worksheet set objworksheet = objexcel.worksheets(1)   ' name constants use in chart creation xlline = 4 xlprimary = 1 xlcategory = 1 xlvalue = 2 xlcolumns = 3   ' define chart properties set objchart = objexcel.charts.add() objexcel.activechart     .charttype = 1     .hastitle = true     .charttitle.characters.text = "usage"     .axes(xlcategory, xlprimary).hastitle = true     .axes(xlcategory, xlprimary).axistitle.characters.text = "time"     .axes(xlvalue, xlprimary).hastitle = true     .axes(xlvalue, xlprimary).axistitle.characters.text = "units"     .charttype = xlline     .haslegend = false     .setsourcedata source:="objworksheet".range("b2:b10"), plotby:=xlcolumns  end 

using code manage create excel file data arranged in columns, , separate sheet empty chart in (having above properties).

but ".setsourcedata" line giving me errors. i'm not sure how use in vbscript. i'm new vbscript, please forgive errors in syntax or understanding of code. maybe i'm doing fundamentally wrong?

edit

i'm using excel 2003.

cheers.

as documented in info section tag cannot use named parameters in vbscript. also, put variable name objworksheet in double quotes. make literal string "objworksheet" instead of variable holding worksheet object. change line

.setsourcedata source:="objworksheet".range("b2:b10"), plotby:=xlcolumns 

to this:

.setsourcedata objworksheet.range("b2:b10"), xlcolumns 

on more general note, should use const keyword defining constants:

const xlline     = 4 const xlprimary  = 1 const xlcategory = 1 const xlvalue    = 2 const xlcolumns  = 3 

otherwise they'll regular (mutable) variables.


Comments