Free Support Forum - aspose.com

Adding more then one slide with OleObjectFrame

Hello,
When i am trying to add more then one slide using OleObjectFrame it is giving me

Object reference not set to an instance of an object.

at line:
Dim oof As OleObjectFrame = sld.Shapes.AddOleObjectFrame(x, 0, slideWidth, slideHeight, “Excel.Sheet.8”, chartOleData)

i am giving yopu the full code kindly review and let me know the cause

Imports Aspose.Cells
Imports Aspose.Slides
Imports System.IO
Partial Class zRMX_test1
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim license As Aspose.Cells.License = New Aspose.Cells.License()
license.SetLicense(“Aspose.Total.lic”)
Dim license1 As Aspose.Slides.License = New Aspose.Slides.License()
license1.SetLicense(“Aspose.Total.lic”)
Run()
End Sub
Shared Sub Run()

Dim wb As Workbook = New Workbook()

Dim chartRows As Integer = 55

Dim chartCols As Integer = 25

Dim chartSheetIndex As Integer = AddExcelChartInWorkbook(wb, chartRows, chartCols)


wb.Worksheets.SetOleSize(0, chartRows, 0, chartCols)


Dim imgChart As Bitmap = wb.Worksheets(2).Charts(0).ToImage()
Dim imgChart1 As Bitmap = wb.Worksheets(3).Charts(0).ToImage()


'Save the workbook to stream

Dim wbStream As MemoryStream = wb.SaveToStream()
Dim wbStream1 As MemoryStream = wb.SaveToStream()
'Step - 4 AND 5

'-----------------------------------------------------------

'Step - 4: Embed the chart as an OLE object inside .ppt presentation using Aspose.Slides

'-----------------------------------------------------------

'Step - 5: Replace the object changed image with the image obtained in step 3 to cater Object Changed Issue

'-----------------------------------------------------------

'Create a presentation

Dim pres As Presentation = New Presentation()

Dim sld As Slide = pres.GetSlideByPosition(1)

Dim sld1 As Slide = pres.GetSlideByPosition(2)

'Add the workbook on slide

AddExcelChartInPresentation(pres, sld, wbStream, imgChart)
AddExcelChartInPresentation(pres, sld1, wbStream1, imgChart1)

'Step - 6: Write the output presentation on disk

'-----------------------------------------------------------

pres.Write(“c:\test\output2.ppt”)

wb.Save(“c:\test\output2.xls”)
End Sub



Shared Function AddExcelChartInWorkbook(ByVal wb As Workbook, ByVal chartRows As Integer, ByVal chartCols As Integer) As Integer
Dim dataSheetIdx As Integer = wb.Worksheets.Add()

Dim dataSheet As Worksheet = wb.Worksheets(dataSheetIdx)

Dim sheetName As String = “DataSheet”


dataSheet.Name = sheetName
Dim cells As Cells = wb.Worksheets(dataSheetIdx).Cells
Dim style As Aspose.Cells.Style = wb.Styles(wb.Styles.Add())
style.Custom = “0.00%”
cells(0, 0).PutValue(“Male”)
cells(0, 1).PutValue(0.6)
cells(0, 1).SetStyle(Style)
cells(1, 0).PutValue(“Female”)
cells(1, 1).PutValue(0.4)
cells(1, 1).SetStyle(style)

cells(3, 0).PutValue(“Zaid”)
cells(3, 1).PutValue(0.6)
cells(3, 1).SetStyle(style)
cells(4, 0).PutValue(“Obaid”)
cells(4, 1).PutValue(0.4)
cells(4, 1).SetStyle(style)



Dim chartSheetIdx As Integer = wb.Worksheets.Add(SheetType.Chart)

Dim chartSheet As Worksheet = wb.Worksheets(chartSheetIdx)

chartSheet.Name = “ChartSheet1”

'Add a chart in ChartSheet with data series from DataSheet

Dim chartIdx As Integer = chartSheet.Charts.Add(Charts.ChartType.Column, 0, chartRows, 0, chartCols)

Dim _chart As Charts.Chart = chartSheet.Charts(chartIdx)

_chart.NSeries.Add(sheetName + “!B1:B2”, True)
_chart.NSeries.CategoryData = sheetName + “!A1:A2”


Dim chartSheetIdx1 As Integer = wb.Worksheets.Add(SheetType.Chart)

Dim chartSheet1 As Worksheet = wb.Worksheets(chartSheetIdx1)

chartSheet1.Name = “ChartSheet2”

'Add a chart in ChartSheet with data series from DataSheet

Dim chartIdx1 As Integer = chartSheet1.Charts.Add(Charts.ChartType.Column, 0, chartRows, 0, chartCols)

Dim _chart1 As Charts.Chart = chartSheet1.Charts(chartIdx1)

_chart1.NSeries.Add(sheetName + “!B4:B5”, True)
_chart1.NSeries.CategoryData = sheetName + “!A4:A5”





End Function



Shared Sub AddExcelChartInPresentation(ByVal pres As Presentation, ByVal sld As Slide, ByVal wbStream As Stream, ByVal imgChart As Bitmap)

Dim pic As Aspose.Slides.Picture = New Aspose.Slides.Picture(pres, imgChart)

Dim picId As Integer = pres.Pictures.Add(pic)

Dim slideWidth As Integer = pres.SlideSize.Width

Dim slideHeight As Integer = pres.SlideSize.Height

Dim x As Integer = 0

Dim chartOleData(0 To wbStream.Length) As Byte

wbStream.Position = 0

wbStream.Read(chartOleData, 0, chartOleData.Length)

Dim oof As OleObjectFrame = sld.Shapes.AddOleObjectFrame(x, 0, slideWidth, slideHeight, “Excel.Sheet.8”, chartOleData)

oof.PictureId = picId

End Sub
End Class



Hi Zaid,

I have worked with the code snippet shared by you and the only issue lies on the place where you are creating and referencing slide to object. You are actually assigning sld1 to position 2 of presentation which actually does not exists. You need to create an empty slide for this purpose. I have made following modification to your code snippet and things worked perfectly fine.

' Dim sld1 As Slide = pres.GetSlideByPosition(2)

Dim sld1 As Slide = pres.AddEmptySlide()

Thanks and Regards,

Hey Thanks Mudassir it really worked :slight_smile:

Hello Muddasir,

I would like to know some more things in this.

  1. Is it compulsory to make the worksheet as chart sheet like
    Dim chartSheetIdx As Integer = wb.Worksheets.Add(SheetType.Chart)
    or we can even add chart in normal worksheet as get the chart from it using
    Dim imgChart As Bitmap = wb.Worksheets(2).Charts(0).ToImage().
  2. I am having a scenario in which i have many charts and each chart is having the data above that only i am attaching a excel file for your reference as step2.xls this excel i have prepare
    using aspose.cells now i want to export all the charts from this excel to PPT
  3. Also if it is compulsory to make the worksheet as chart sheet then can i add multiple chart is that one worksheet or for different chart i have to create different sheets kindly provide me some code reference for this
Kindly revert me as soon as possible

Thanks & Regards
Lakdawala Zaid



Hi Lakdawala Zaid,

I am a representative of Aspose.Cells team.
For your queries regarding Aspose.Cells:

  • Is it compulsory to make the worksheet as chart sheet like
    Dim chartSheetIdx As Integer = wb.Worksheets.Add(SheetType.Chart)
    or we can even add chart in normal worksheet as get the chart from it using
    Dim imgChart As Bitmap = wb.Worksheets(2).Charts(0).ToImage().
No, it is not compulsory to make a worksheet as chart for chart sheets, you may add charts to normal sheets too.

Check the documents in the section for creating /manipulating charts
http://www.aspose.com/documentation/.net-components/aspose.cells-for-.net/creating-charts.html

  • I
    am having a scenario in which i have many charts and each chart is
    having the data above that only i am attaching a excel file for your
    reference as step2.xls this excel i have prepare using aspose.cells now i want to export all the charts from this excel to PPT
Please check our featured chart demos (if covers almost all the chart types), most of the demos have data above charts in the same worksheet:
http://www.aspose.com/demos/.net-components/aspose.cells/csharp/chart-types/default.aspx

  • Also
    if it is compulsory to make the worksheet as chart sheet then can i add
    multiple chart is that one worksheet or for different chart i have to
    create different sheets kindly provide me some code reference for this
Please check the first and second replies/comments (above).


Thank you.