excel data pasting ppt??

shashacool

New Member
hi friends,

i want excel sheet which contains data and another excel sheet which contain data and graphs both to be pasted in power point.
 

VJR

Well-Known Member
Hi shashacool,

This requires a Code stage to be implemented which is explained as below.
Below is a sample working code that copies data from a specified Excel range and works well under general conditions. You might need to make amendments as per your requirement. For example, it assumes that Sheet1 exists so you would need to add code to check the sheet, add Exception Handling, remove hard coded path of PPT, adjust the top and left of the slide content, etc. Also you need to make modifications if you need it in an existing ppt rather than a new one and insert the data on a specified slide number.

You need to create a new action page in the MS Excel VBO. This can be a duplicate of any other existing page after making the necessary additions/deletions to it. So choose a page that closely resembles the diagram explained below. Lets call the new page as 'Copy Range From Excel2PPT' and it looks like 1.jpg in the screenshots.

- 2.jpg: has the Input parameters to the Start stage of the Object
- 3.jpg: has the Input parameters to the Code stage
- 4.jpg: has the Output parameters to the Code stage

The content of the Code stage is as below. Paste the below in the Code tab of the Code stage.

Code:
Dim sw, dw As Object
Dim ss, ds As Object
Dim excel, sheet As Object

Dim PowerPointApp As Object
Dim myPresentation As Object
Dim mySlide As Object
Dim myShape As Object

Try

'Create PowerPoint object
PowerPointApp = CreateObject("PowerPoint.Application")

'Create a New Presentation
myPresentation = PowerPointApp.Presentations.Add

'Add a slide to the Presentation
mySlide = myPresentation.Slides.Add(1, 12) '12 = ppLayoutBlank

'The different slide layout constants available can be found on the below Microsoft link
'https://msdn.microsoft.com/en-us/vba/powerpoint-vba/articles/ppslidelayout-enumeration-powerpoint

'Can be turned to False if you do not want to see the PPT while running the process
PowerPointApp.Visible = True

'Excel related stuff
sw = GetWorkbook(Handle, Source_Workbook)
ss = GetWorksheet(Handle, Source_Workbook, Source_Worksheet)

sw.Activate()
ss.Activate()   'Whichever sheet name was passed as a parameter to this code stage that becomes the active sheet
excel = ss.Application
sheet = excel.ActiveSheet

'Copy the required Excel Range that was passed as a parameter
sheet.Range(cellRef).copy

'Paste to PowerPoint
 mySlide.Shapes.PasteSpecial (DataType:=2)  '2 = ppPasteEnhancedMetafile

'DataType Numeric Constants
'1:=ppPasteBitmap
'0:=ppPasteDefault
'2:=ppPasteEnhancedMetafile
'8:=ppPasteHTML
'4:=ppPasteGIF
'5:=ppPasteJPG
'3:=ppPasteMetafilePicture
'6:=ppPastePNG
'11:=ppPasteShape


myShape = mySlide.Shapes(mySlide.Shapes.Count)
 
'Set position:
myShape.Left = 50
myShape.Top = 50

'Activate Powerpoint
PowerPointApp.Activate

'Removes the moving bordered area of excel that was as a result of the Copy
excel.CutCopyMode = False

'Save and close the PPT.
With myPresentation
    .SaveCopyAs ("C:\MySavedPPT")    'This code can be modified for passing the Path as a parameter to this code stage
    .Close
End With

'Parameters for .SaveCopyAs can be found here
'https://msdn.microsoft.com/en-us/vba/powerpoint-vba/articles/presentation-savecopyas-method-powerpoint

'Quit Powerpoint.
PowerPointApp.Quit

Success = True

Catch e As Exception
    Success = False
    Message = e.Message
Finally
    sw = Nothing
    ss = Nothing
    dw = Nothing
    ds = Nothing
    excel = Nothing
    sheet = Nothing   
    myPresentation = Nothing
    PowerPointApp = Nothing
End Try

- 5.jpg: is how the call to this new action looks like from the Process
- 6.jpg: is the Input Excel sheet containing data and a chart
- 7.jpg: is the Output after running the process

You need to call the same action for both of your requirements (Excel sheet with data & sheet with data and graph) by simply specifying the range in the parameters.
 

Attachments

  • 1. Object Diagram.JPG
    1. Object Diagram.JPG
    45.3 KB · Views: 139
  • 2. Object Start Stage.JPG
    2. Object Start Stage.JPG
    33.7 KB · Views: 133
  • 3. Input parameters Code stage.JPG
    3. Input parameters Code stage.JPG
    31.1 KB · Views: 120
  • 4. Output parameters Code stage.JPG
    4. Output parameters Code stage.JPG
    25.6 KB · Views: 118
  • 5. Call from Process.JPG
    5. Call from Process.JPG
    24.1 KB · Views: 124
  • 6. Input Excel.JPG
    6. Input Excel.JPG
    43.6 KB · Views: 127
  • 7. Output on PPT.JPG
    7. Output on PPT.JPG
    45.8 KB · Views: 114

shashacool

New Member
Hi VJ,

i tried with below code but after pasting one graph into ppt slide the ppt is goint to crashed evrytime.

Sub PushChartsToPPT()

'Set reference to 'Microsoft PowerPoint 12.0 Object Library'
'in the VBE via Tools > References...'
Dim ppt As PowerPoint.Application
Dim pptPres As PowerPoint.Presentation
Dim pptSld As PowerPoint.Slide
Dim pptCL As PowerPoint.CustomLayout
Dim pptShp As PowerPoint.Shape

Dim cht As Chart
Dim ws As Worksheet
Dim i As Long

'Get the PowerPoint Application object:
Set ppt = CreateObject("PowerPoint.Application")
ppt.Visible = msoTrue
Set pptPres = ppt.Presentations.Add

'Get a Custom Layout:
For Each pptCL In pptPres.SlideMaster.CustomLayouts
If pptCL.Name = "Title and Content" Then Exit For
Next pptCL

'Copy ALL charts in Chart Sheets:
For Each cht In ActiveWorkbook.Charts
Set pptSld = pptPres.Slides.AddSlide(pptPres.Slides.Count + 1, pptCL)
pptSld.Select

For Each pptShp In pptSld.Shapes.Placeholders
If pptShp.PlaceholderFormat.Type = ppPlaceholderObject Then Exit For
Next pptShp
If pptShp Is Nothing Then Stop

cht.ChartArea.Copy
ppt.Activate
pptShp.Select
ppt.Windows(1).View.Paste
Next cht
For Each ws In ActiveWorkbook.Worksheets
For i = 1 To ws.ChartObjects.Count
Set pptSld = pptPres.Slides.AddSlide(pptPres.Slides.Count + 1, pptCL)
pptSld.Select

For Each pptShp In pptSld.Shapes.Placeholders
If pptShp.PlaceholderFormat.Type = ppPlaceholderObject Then Exit For
Next pptShp

Set cht = ws.ChartObjects(i).Chart
cht.ChartArea.Copy
ppt.Activate
pptShp.Select
ppt.Windows(1).View.Paste
Next i
Next ws
End Sub


help me out with this......
 

VJR

Well-Known Member
Hi VJ,

i tried with below code but after pasting one graph into ppt slide the ppt is goint to crashed evrytime.

Sub PushChartsToPPT()

'Set reference to 'Microsoft PowerPoint 12.0 Object Library'
'in the VBE via Tools > References...'
Dim ppt As PowerPoint.Application
Dim pptPres As PowerPoint.Presentation
Dim pptSld As PowerPoint.Slide
Dim pptCL As PowerPoint.CustomLayout
Dim pptShp As PowerPoint.Shape

Dim cht As Chart
Dim ws As Worksheet
Dim i As Long

'Get the PowerPoint Application object:
Set ppt = CreateObject("PowerPoint.Application")
ppt.Visible = msoTrue
Set pptPres = ppt.Presentations.Add

'Get a Custom Layout:
For Each pptCL In pptPres.SlideMaster.CustomLayouts
If pptCL.Name = "Title and Content" Then Exit For
Next pptCL

'Copy ALL charts in Chart Sheets:
For Each cht In ActiveWorkbook.Charts
Set pptSld = pptPres.Slides.AddSlide(pptPres.Slides.Count + 1, pptCL)
pptSld.Select

For Each pptShp In pptSld.Shapes.Placeholders
If pptShp.PlaceholderFormat.Type = ppPlaceholderObject Then Exit For
Next pptShp
If pptShp Is Nothing Then Stop

cht.ChartArea.Copy
ppt.Activate
pptShp.Select
ppt.Windows(1).View.Paste
Next cht
For Each ws In ActiveWorkbook.Worksheets
For i = 1 To ws.ChartObjects.Count
Set pptSld = pptPres.Slides.AddSlide(pptPres.Slides.Count + 1, pptCL)
pptSld.Select

For Each pptShp In pptSld.Shapes.Placeholders
If pptShp.PlaceholderFormat.Type = ppPlaceholderObject Then Exit For
Next pptShp

Set cht = ws.ChartObjects(i).Chart
cht.ChartArea.Copy
ppt.Activate
pptShp.Select
ppt.Windows(1).View.Paste
Next i
Next ws
End Sub


help me out with this......
Hi shashacool, this is a VB/VBA code and will work only in a VB editor. It will not work directly in Blue Prism's code stage. You will need to modify it to accommodate it to BP. Add your code few lines at a time in BP and then use 'Check code' button for errors. Right from your first statement (Dim ppt As PowerPoint.Application) see if BP recognizes Powerpoint Application in that statement. You might need to add references or import namespaces to make it work. If there is an error then you need to modify it to be compatible with VB.NET's semantics after which it will work. Ex: There are lots of Set statements in your code which is no longer supported and not allowed in BP code stage. By the way are you unhappy with the code I have provided you :).
 

shashacool

New Member
No buddy your code is also fine.....but what i am facing the situation is that what ever the code i given above it runs fine on local machine..... absolutely fine....it opens ppt pastes all the graph in diff. Slide.....but the crashing problem arises when i run the same code in VM (virtual machine). After pasting one graph ppt gets crashed.....and what ever this code is i written this code by creating a macro (excel). And in module section of macro.
 

VJR

Well-Known Member
No buddy your code is also fine.....but what i am facing the situation is that what ever the code i given above it runs fine on local machine..... absolutely fine....it opens ppt pastes all the graph in diff. Slide.....but the crashing problem arises when i run the same code in VM (virtual machine). After pasting one graph ppt gets crashed.....and what ever this code is i written this code by creating a macro (excel). And in module section of macro.
To identify the line of code where it is crashing follow the instructions as stated above. Add line by line of your code to BP and then keep checking for errors and run the code stage each time. If it crashes after adding the last line then that is the line causing the issue. There could be difference in your machine with regards to Excel/PPT related registered dlls, files, .net framework, etc.
 

jay0201

New Member
Hi shashacool,

This requires a Code stage to be implemented which is explained as below.
Below is a sample working code that copies data from a specified Excel range and works well under general conditions. You might need to make amendments as per your requirement. For example, it assumes that Sheet1 exists so you would need to add code to check the sheet, add Exception Handling, remove hard coded path of PPT, adjust the top and left of the slide content, etc. Also you need to make modifications if you need it in an existing ppt rather than a new one and insert the data on a specified slide number.

You need to create a new action page in the MS Excel VBO. This can be a duplicate of any other existing page after making the necessary additions/deletions to it. So choose a page that closely resembles the diagram explained below. Lets call the new page as 'Copy Range From Excel2PPT' and it looks like 1.jpg in the screenshots.

- 2.jpg: has the Input parameters to the Start stage of the Object
- 3.jpg: has the Input parameters to the Code stage
- 4.jpg: has the Output parameters to the Code stage

The content of the Code stage is as below. Paste the below in the Code tab of the Code stage.

Code:
Dim sw, dw As Object
Dim ss, ds As Object
Dim excel, sheet As Object

Dim PowerPointApp As Object
Dim myPresentation As Object
Dim mySlide As Object
Dim myShape As Object

Try

'Create PowerPoint object
PowerPointApp = CreateObject("PowerPoint.Application")

'Create a New Presentation
myPresentation = PowerPointApp.Presentations.Add

'Add a slide to the Presentation
mySlide = myPresentation.Slides.Add(1, 12) '12 = ppLayoutBlank

'The different slide layout constants available can be found on the below Microsoft link
'https://msdn.microsoft.com/en-us/vba/powerpoint-vba/articles/ppslidelayout-enumeration-powerpoint

'Can be turned to False if you do not want to see the PPT while running the process
PowerPointApp.Visible = True

'Excel related stuff
sw = GetWorkbook(Handle, Source_Workbook)
ss = GetWorksheet(Handle, Source_Workbook, Source_Worksheet)

sw.Activate()
ss.Activate()   'Whichever sheet name was passed as a parameter to this code stage that becomes the active sheet
excel = ss.Application
sheet = excel.ActiveSheet

'Copy the required Excel Range that was passed as a parameter
sheet.Range(cellRef).copy

'Paste to PowerPoint
mySlide.Shapes.PasteSpecial (DataType:=2)  '2 = ppPasteEnhancedMetafile

'DataType Numeric Constants
'1:=ppPasteBitmap
'0:=ppPasteDefault
'2:=ppPasteEnhancedMetafile
'8:=ppPasteHTML
'4:=ppPasteGIF
'5:=ppPasteJPG
'3:=ppPasteMetafilePicture
'6:=ppPastePNG
'11:=ppPasteShape


myShape = mySlide.Shapes(mySlide.Shapes.Count)

'Set position:
myShape.Left = 50
myShape.Top = 50

'Activate Powerpoint
PowerPointApp.Activate

'Removes the moving bordered area of excel that was as a result of the Copy
excel.CutCopyMode = False

'Save and close the PPT.
With myPresentation
    .SaveCopyAs ("C:\MySavedPPT")    'This code can be modified for passing the Path as a parameter to this code stage
    .Close
End With

'Parameters for .SaveCopyAs can be found here
'https://msdn.microsoft.com/en-us/vba/powerpoint-vba/articles/presentation-savecopyas-method-powerpoint

'Quit Powerpoint.
PowerPointApp.Quit

Success = True

Catch e As Exception
    Success = False
    Message = e.Message
Finally
    sw = Nothing
    ss = Nothing
    dw = Nothing
    ds = Nothing
    excel = Nothing
    sheet = Nothing  
    myPresentation = Nothing
    PowerPointApp = Nothing
End Try

- 5.jpg: is how the call to this new action looks like from the Process
- 6.jpg: is the Input Excel sheet containing data and a chart
- 7.jpg: is the Output after running the process

You need to call the same action for both of your requirements (Excel sheet with data & sheet with data and graph) by simply specifying the range in the parameters.

Hi @VJR ,

Thanks for the code! It was a great help and provided more insight into the automation of Powerpoint which I feel Blue Prism is lacking.

Would like to ask, how can I modify your code such that I am able to copy a Table from Excel, to be pasted exactly over another Table in the Powerpoint? Seems like that copy and pasting of charts works with the Powerpoint VBO, but couldn't figure out how to approach the Tables.


Best Regards,
Jay
 

rashmi123

New Member
1. I am getting error message at code stage in object studio.
2. I have followed the exact steps you have mentioned but when i run process am getting blank word document.The excel chart is not pasting on word document.
May I know why,
 
Last edited:

VJR

Well-Known Member
On which Code stage - Code stage of any of the existing Excel VBO actions or something else?
 

VJR

Well-Known Member
The dictionary key error generally appears when the parameter is not correctly generated from its corresponding actions for eg; handle from Create Instance Or if they are not passed correctly eg; handle is created for one workbook but passed for another.
At first check those first and if the error still persists you will need to identify the line of code that is throwing the error.
Pinpoint the line of error by commenting lines of code from the bottom. Run it and if there is no error then uncomment another line and then again run it and do the same until you find the exact line of error.
 

subhash

Member
Hi Team,

used the above code to copy excel range to ppt...it's working perfectly but the problem is in the ppt some lines are bold in the tables, is there any way to remove unwanted bold lines from the ppt.

Regards
 

pranab

New Member
Hi shashacool,

This requires a Code stage to be implemented which is explained as below.
Below is a sample working code that copies data from a specified Excel range and works well under general conditions. You might need to make amendments as per your requirement. For example, it assumes that Sheet1 exists so you would need to add code to check the sheet, add Exception Handling, remove hard coded path of PPT, adjust the top and left of the slide content, etc. Also you need to make modifications if you need it in an existing ppt rather than a new one and insert the data on a specified slide number.

You need to create a new action page in the MS Excel VBO. This can be a duplicate of any other existing page after making the necessary additions/deletions to it. So choose a page that closely resembles the diagram explained below. Lets call the new page as 'Copy Range From Excel2PPT' and it looks like 1.jpg in the screenshots.

- 2.jpg: has the Input parameters to the Start stage of the Object
- 3.jpg: has the Input parameters to the Code stage
- 4.jpg: has the Output parameters to the Code stage

The content of the Code stage is as below. Paste the below in the Code tab of the Code stage.

Code:
Dim sw, dw As Object
Dim ss, ds As Object
Dim excel, sheet As Object

Dim PowerPointApp As Object
Dim myPresentation As Object
Dim mySlide As Object
Dim myShape As Object

Try

'Create PowerPoint object
PowerPointApp = CreateObject("PowerPoint.Application")

'Create a New Presentation
myPresentation = PowerPointApp.Presentations.Add

'Add a slide to the Presentation
mySlide = myPresentation.Slides.Add(1, 12) '12 = ppLayoutBlank

'The different slide layout constants available can be found on the below Microsoft link
'https://msdn.microsoft.com/en-us/vba/powerpoint-vba/articles/ppslidelayout-enumeration-powerpoint

'Can be turned to False if you do not want to see the PPT while running the process
PowerPointApp.Visible = True

'Excel related stuff
sw = GetWorkbook(Handle, Source_Workbook)
ss = GetWorksheet(Handle, Source_Workbook, Source_Worksheet)

sw.Activate()
ss.Activate()   'Whichever sheet name was passed as a parameter to this code stage that becomes the active sheet
excel = ss.Application
sheet = excel.ActiveSheet

'Copy the required Excel Range that was passed as a parameter
sheet.Range(cellRef).copy

'Paste to PowerPoint
mySlide.Shapes.PasteSpecial (DataType:=2)  '2 = ppPasteEnhancedMetafile

'DataType Numeric Constants
'1:=ppPasteBitmap
'0:=ppPasteDefault
'2:=ppPasteEnhancedMetafile
'8:=ppPasteHTML
'4:=ppPasteGIF
'5:=ppPasteJPG
'3:=ppPasteMetafilePicture
'6:=ppPastePNG
'11:=ppPasteShape


myShape = mySlide.Shapes(mySlide.Shapes.Count)

'Set position:
myShape.Left = 50
myShape.Top = 50

'Activate Powerpoint
PowerPointApp.Activate

'Removes the moving bordered area of excel that was as a result of the Copy
excel.CutCopyMode = False

'Save and close the PPT.
With myPresentation
    .SaveCopyAs ("C:\MySavedPPT")    'This code can be modified for passing the Path as a parameter to this code stage
    .Close
End With

'Parameters for .SaveCopyAs can be found here
'https://msdn.microsoft.com/en-us/vba/powerpoint-vba/articles/presentation-savecopyas-method-powerpoint

'Quit Powerpoint.
PowerPointApp.Quit

Success = True

Catch e As Exception
    Success = False
    Message = e.Message
Finally
    sw = Nothing
    ss = Nothing
    dw = Nothing
    ds = Nothing
    excel = Nothing
    sheet = Nothing  
    myPresentation = Nothing
    PowerPointApp = Nothing
End Try

- 5.jpg: is how the call to this new action looks like from the Process
- 6.jpg: is the Input Excel sheet containing data and a chart
- 7.jpg: is the Output after running the process

You need to call the same action for both of your requirements (Excel sheet with data & sheet with data and graph) by simply specifying the range in the parameters.



Thanks a lot for your help man,
But i need to save data as an editable table only in presentation not as a picture.
Can you help me with that ?
 
Top