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