Guilherme Barbosa
asked on
VBA to export Excel worksheet to PowerPoint slides
VBA to Export Excel sheet to PowerPoint:
Hi experts,
I am using the following VBA to export my worksheet to PowerPoint presentation, however if I change the slicer in my Pivot Chart and export again the worksheet, the code is creating a new presentation, rather than a new slide.
I would like to add the new selection into the same presentation, just adding as a new slide.
I believe I need a loop indexing and counting the slides.
Any suggestions, please.
VBA Code:
Sub ExceltoPowerPoint()
Dim PowerPointApp As Object
Dim myPresentation As Object
Dim wb As Workbook
Dim ws As Worksheet
Dim namecheck As Range
Set PowerPointApp = GetPowerPointApp()
Set myPresentation = PowerPointApp.Presentation s.Add
Call ExportResourcePlanSlide(my Presentati on, ThisWorkbook.ActiveSheet.R ange("a2:m 40"))
PowerPointApp.Visible = True
PowerPointApp.Activate
Application.CutCopyMode = False
End Sub
Function GetPowerPointApp() As Object
On Error Resume Next
Set PowerPointApp = GetObject(class:="PowerPoi nt.Applica tion")
Err.Clear
If PowerPointApp Is Nothing Then Set PowerPointApp = CreateObject(class:="Power Point.Appl ication")
If Err.Number = 429 Then
MsgBox "PowerPoint could not be found, aborting."
Exit Function
End If
Set GetPowerPointApp = PowerPointApp
On Error GoTo 0
End Function
Sub ExportResourcePlanSlide(By Val myPresentation As Object, ByRef rng As Range)
'Create new slide--------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- -
Set myslide = myPresentation.Slides.Add( myPresenta tion.Slide s.Count + 1, 12) '11 = ppLayoutTitleOnly
'Copy range and paste to powerpoint---------------- ---------- ---------- ---------- ---------- ---------- ---------- --------
rng.Copy
myslide.Shapes.PasteSpecia l DataType:=2 '2 = ppPasteEnhancedMetafile
'Add Commentary Text Box----------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- --
Set myTextBox = myslide.Shapes.AddTextbox( 1, Left:=100, Top:=100, Width:=8.19 * 28.3465, Height:=350)
With myTextBox
.TextFrame.TextRange.Text = ""
.TextFrame.TextRange.Font. Size = 10
.Left = 24.9 * 28.3465
.Top = 3.18 * 28.3465
End With
End Sub
Hi experts,
I am using the following VBA to export my worksheet to PowerPoint presentation, however if I change the slicer in my Pivot Chart and export again the worksheet, the code is creating a new presentation, rather than a new slide.
I would like to add the new selection into the same presentation, just adding as a new slide.
I believe I need a loop indexing and counting the slides.
Any suggestions, please.
VBA Code:
Sub ExceltoPowerPoint()
Dim PowerPointApp As Object
Dim myPresentation As Object
Dim wb As Workbook
Dim ws As Worksheet
Dim namecheck As Range
Set PowerPointApp = GetPowerPointApp()
Set myPresentation = PowerPointApp.Presentation
Call ExportResourcePlanSlide(my
PowerPointApp.Visible = True
PowerPointApp.Activate
Application.CutCopyMode = False
End Sub
Function GetPowerPointApp() As Object
On Error Resume Next
Set PowerPointApp = GetObject(class:="PowerPoi
Err.Clear
If PowerPointApp Is Nothing Then Set PowerPointApp = CreateObject(class:="Power
If Err.Number = 429 Then
MsgBox "PowerPoint could not be found, aborting."
Exit Function
End If
Set GetPowerPointApp = PowerPointApp
On Error GoTo 0
End Function
Sub ExportResourcePlanSlide(By
'Create new slide---------------------
Set myslide = myPresentation.Slides.Add(
'Copy range and paste to powerpoint----------------
rng.Copy
myslide.Shapes.PasteSpecia
'Add Commentary Text Box-----------------------
Set myTextBox = myslide.Shapes.AddTextbox(
With myTextBox
.TextFrame.TextRange.Text = ""
.TextFrame.TextRange.Font.
.Left = 24.9 * 28.3465
.Top = 3.18 * 28.3465
End With
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi Ryan,
Thank you for your suggestion, but it didn't worked, can you please tell me in which part of the code I should include your code?
That is the way I did:
************************** ********** ***
Sub ExceltoPowerPoint()
Dim PowerPointApp As Object
Dim myPresentation As Object
Dim wb As Workbook
Dim ws As Worksheet
Dim namecheck As Range
Set PowerPointApp = GetPowerPointApp()
Set myPresentation = PowerPointApp.Presentation s.Add
Call ExportResourcePlanSlide(my Presentati on, ThisWorkbook.ActiveSheet.R ange("a2:m 40"))
PowerPointApp.Visible = True
PowerPointApp.Activate
Application.CutCopyMode = False
End Sub
Function GetPowerPointApp() As Object
On Error Resume Next
Set PowerPointApp = GetObject(class:="PowerPoi nt.Applica tion")
Err.Clear
If PowerPointApp Is Nothing Then Set PowerPointApp = CreateObject(class:="Power Point.Appl ication")
If Err.Number = 429 Then
MsgBox "PowerPoint could not be found, aborting."
Exit Function
End If
Set GetPowerPointApp = PowerPointApp
On Error GoTo 0
End Function
Sub ExportResourcePlanSlide(By Val myPresentation As Object, ByRef rng As Range)
Set myPresentation = PowerPointApp.Presentation s(PowerPoi ntApp.Pres entations. Count)
'Create new slide--------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- -
Set mySlide = myPresentation.Slides.Add( myPresenta tion.Slide s.Count + 1, 12) '11 = ppLayoutTitleOnly
'Copy range and paste to powerpoint---------------- ---------- ---------- ---------- ---------- ---------- ---------- --------
rng.Copy
mySlide.Shapes.PasteSpecia l DataType:=2 '2 = ppPasteEnhancedMetafile
'Add Commentary Text Box----------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- --
Set myTextBox = mySlide.Shapes.AddTextbox( 1, Left:=100, Top:=100, Width:=8.19 * 28.3465, Height:=350)
With myTextBox
.TextFrame.TextRange.Text = ""
.TextFrame.TextRange.Font. Size = 10
.Left = 24.9 * 28.3465
.Top = 3.18 * 28.3465
End With
End Sub
Thank you for your suggestion, but it didn't worked, can you please tell me in which part of the code I should include your code?
That is the way I did:
**************************
Sub ExceltoPowerPoint()
Dim PowerPointApp As Object
Dim myPresentation As Object
Dim wb As Workbook
Dim ws As Worksheet
Dim namecheck As Range
Set PowerPointApp = GetPowerPointApp()
Set myPresentation = PowerPointApp.Presentation
Call ExportResourcePlanSlide(my
PowerPointApp.Visible = True
PowerPointApp.Activate
Application.CutCopyMode = False
End Sub
Function GetPowerPointApp() As Object
On Error Resume Next
Set PowerPointApp = GetObject(class:="PowerPoi
Err.Clear
If PowerPointApp Is Nothing Then Set PowerPointApp = CreateObject(class:="Power
If Err.Number = 429 Then
MsgBox "PowerPoint could not be found, aborting."
Exit Function
End If
Set GetPowerPointApp = PowerPointApp
On Error GoTo 0
End Function
Sub ExportResourcePlanSlide(By
Set myPresentation = PowerPointApp.Presentation
'Create new slide---------------------
Set mySlide = myPresentation.Slides.Add(
'Copy range and paste to powerpoint----------------
rng.Copy
mySlide.Shapes.PasteSpecia
'Add Commentary Text Box-----------------------
Set myTextBox = mySlide.Shapes.AddTextbox(
With myTextBox
.TextFrame.TextRange.Text = ""
.TextFrame.TextRange.Font.
.Left = 24.9 * 28.3465
.Top = 3.18 * 28.3465
End With
End Sub
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Awesome Ryan!! It is working now!! Thanks so much!!
ASKER
Thank you Ryan for your prompt feedback and help!! Really appreciate it!
Cheers,
Gilly
Cheers,
Gilly
Open in new window