How Do I Change The Line Colour Of A Series In a Line Graph of MS Excel using VB .Net Code

Hi Experts,

I have managed to create an application in VB .Net 2005 that will dynamically create a chart in an Excel file based on the data loaded in the file. I am experiencing difficulties of trying to set the color of the series line in the application. I have been googl-ing the web for few days and still can't find the method. I would appreciate some advice on this if any of you did this kind of application before and the version of MS Excel I am trying to make compatible with my application is 2000, 2003 and 2007. Million thanks. :)
LVL 2
JohnWooAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

omegaomegaDeveloperCommented:
Hello, John,

The code in the attached snippet shows a trivial example of getting and changing the colour of the second series line for a chart named "Chart 1" in Excel 2000.  I expect that the same code may work for the later versions as well, but cannot test this.

Cheers,
Randy

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
 
        Dim xlaTest As Excel.Application = CType(CreateObject("Excel.Application"), Excel.Application)
        Dim wbkTest As Excel.Workbook = xlaTest.Workbooks.Open("D:\Tests\VB Trials\ExcelTests\TestWorkbook.xls")
        Dim wshTest As Excel.Worksheet = DirectCast(wbkTest.Worksheets("Sheet1"), Excel.Worksheet)
 
        xlaTest.Visible = True
 
        wshTest.ChartObjects("Chart 1").Activate()
        xlaTest.ActiveChart.ChartArea.Select()
 
        Dim ChartLine As Excel.Border = wshTest.ChartObjects("Chart 1").Chart.SeriesCollection(2).Border
 
        Dim intColor As Integer = ChartLine.Color
        Dim intColorIndex As Integer = ChartLine.ColorIndex
 
        ChartLine.Color = RGB(100, 100, 100)
 
        wbkTest.Close(SaveChanges:=True)
        xlaTest.Quit()
 
    End Sub

Open in new window

0
omegaomegaDeveloperCommented:
Hello, John,

I see that I was too quick copying and pasting my sample code.  The lines:

        wshTest.ChartObjects("Chart 1").Activate()
        xlaTest.ActiveChart.ChartArea.Select()

can be safely omitted.  As shown, you can get either the "Color" property, or the "ColorIndex".  Although my example shows only setting the "Color" property, you can also set "ColorIndex" if that is your preference.

Cheers,
Randy
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JohnWooAuthor Commented:
Hi Randy,

Thanks for the help, with your hint I managed to do it as shown in the snippet. The reason I felt that I can't set the color for the series is that .Border.Color property was not shown in the property list of xSeries3. However, if I force my way to call the property out from xSeries3, it works. It's weird though, if the property is working why is it not shown in the property list of Excel.Series. Million thanks again Randy. :D



Dim xlchart As Excel.ChartObjects
Dim mychart As Excel.ChartObject
Dim xSeriesCollection As Excel.SeriesCollection
Dim xSeries3 As Excel.Series
 
mychart = xlchart.Add(0, 0, 500, 200)
mychart.Chart.ChartType = Excel.XlChartType.xlLineMarkers
 
xSeriesCollection = mychart.Chart.SeriesCollection
 
xSeries3 = xSeriesCollection.NewSeries
xSeries3.Values = xlSheet1.Range("I8", "I" & intRowCount)
xSeries3.XValues = xlSheet1.Range("C8", "C" & intRowCount)
xSeries3.MarkerStyle = Excel.XlMarkerStyle.xlMarkerStyleNone
xSeries3.Border.Color = RGB(34, 139, 34)
 
mychart.Chart.Refresh()

Open in new window

0
omegaomegaDeveloperCommented:
Hello, John,

I think that the problem is due to the "type" of xSeries3.Border.  Without an explicit cast, IntelliSense sees this as a general COM object.  If you cast it as Excel.Border (as shown in the snippet) then I think that IntelliSense will show you the Color property.

Cheers,
Randy
btw -- just so you know...  This doesn't apply to me, but some of the people who provide answers on this forum would be offended by a "B" grade unless you were to point out the specific deficiency with the solution.   :-)
0
JohnWooAuthor Commented:
Hi Randy,

Oh...I didn't know that this could be an issue for the people here. Thanks for the notice, I'll know how to approach this next time. Thank you :)

Regards,
John
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.