How to create a PowerPoint with Excel VBA without seeing the application - vba

I want to know how I can create a new PPT from Excel VBA (I already have the code) but without seeing the app while it is creating. I have found some insights but it only works when it opens an existing PPT file, but I am creating a new file.
Dim pptApp As PowerPoint.Application
Dim pptPres As PowerPoint.Presentation
Dim pptSlide As PowerPoint.Slide
Dim pptShape As PowerPoint.Shape
Dim excelTable As Excel.Range
Dim SlideTitle As String
Dim SlideText As String
Dim SlideObject As Object
Dim pptTextbox As PowerPoint.Shape
Dim SlideNumber As String
On Error Resume Next
Set pptApp = New PowerPoint.Application
Err.Clear
Set pptPres = pptApp.Presentations.Add
pptPres.PageSetup.SlideSize = ppSlideSizeOnScreen

Calling .Active on a PowerPoint.Application does just that - it activates it, which makes the window visible:
Dim ppt As PowerPoint.Application
Set ppt = New PowerPoint.Application
Debug.Print ppt.Visible '<--Prints 0 (msoFalse)
ppt.Activate '<--THIS SHOWS THE WINDOW.
Debug.Print ppt.Visible '<--Prints -1 (msoTrue)
Just remove the pptApp.Activate line completely.
As mentioned in the comments, you also need to fix your error handler. In this case, the best fix is by removing it completely. GetObject returns an existing instance if it exists. I'm assuming that when you say "create a new PPT" that you don't mean "attach to a running PowerPoint instance if it exists, otherwise create a new one". That is what your code currently does.
Also as mentioned in the comments, if you have a reference to Microsoft PowerPoint X.X Object Library (as evidenced by Dim pptApp As PowerPoint.Application), you shouldn't be using CreateObject either. That's for late-binding. If you have a reference, use early-binding.
Finally, when you create a PowerPoint.Application, it's not visible by default. You can "fix" your code by reducing it to this one line:
Set pptApp = New PowerPoint.Application

Related

From MS Word, how can I add a slide in Powerpoint using VBA

I am trying to add a slide in a Presentation but I am having an error.
Context:
I have a word file that contains more than 200 pages. Each page contains an image (a screenshot). I want to create a PowerPoint document and for each images in the MS Word document; I want to paste the picture in a blank layout slides.
Sub transfert_image_from_WORD_to_PowerPoint()
'I added a Reference Object to this Module (PowerPoint)
'Variable creation
Dim pptPres As PowerPoint.Presentation
Dim pptApp As PowerPoint.Application
Set pptApp = CreateObject("PowerPoint.Application")
pptApp.Visible = True
Set pptPres = pptApp.Presentations.Add
'add Slides
Dim pptSlide As Slide
Dim pptLayout As CustomLayout
Set pptLayout = ActivePresentation.Slides(0).CustomLayout
'Set pptSlide = ActivePresentation.Slides.AddSlide(0, pptLayout)
'Word object creation to contains images.
Dim pic As InlineShape
Dim pslides As Slides
'loop through eanch Picutures in MS Word
For Each pic In ActiveDocument.InlineShapes
pic.Select
Selection.Copy
'Selection.PasteAndFormat wdPasteDefault
Next
End Sub
I have an error in the line Set pptLayout
Not sure why you aer using ActivePresentation when you create a Presentation object. Anyway, here is how you can add a slide. You use the Add method of the CustomeLayouts collection, then you can add a slide
Set pptLayout = pptPres.SlideMaster.CustomLayouts.Add(1)
Set pptSlide = pptPres.Slides.AddSlide(1, pptLayout)

Presentations.Open Method failed for MS PowerPoint 15.0 Object Library

I am calling VBA code from an Excel spreadsheet to open an existing PowerPoint file via the Presentations.Open method. In my environment I developed via Early Binding using the MS PowerPoint 14.0 Object Library and the codes run without a problem.
However, when the script was called in another machine that runs MS Office 2013 (i.e. MS PowerPoint 15.0 Object Library), a Run-time error pops up
Method 'Open' of object 'Presentations' failed
Is the Presentations.Open method deprecated in PPT 15.0 Object library? I tried searching Internet but couldn't find documentation on the change.
I also attempted to use Late Binding to see if it works, but received the same error.
Please find below the code snipnets I used (early + late binding).
Thank you very much for the help.
Early Binding Code Snipnet
Sub EarlyBinding()
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet1")
Dim PowerpointApp As PowerPoint.Application
Dim myPresentation As PowerPoint.Presentation
Set PowerpointApp = New PowerPoint.Application
PowerpointApp.Visible = True
Dim myPath As String
myPath = ws.Range("wk_dir").Value & "\" & ws.Range("ppt_name").Value
Set myPresentation = PowerpointApp.presentations.Open(myPath)
myPresentation.SaveAs (ws.Range("wk_dir").Value & "\test_earlybind.pptx")
Set myPresentation = Nothing
Set PowerpointApp = Nothing
End Sub
Late Binding Code Snipnet
Sub LateBinding()
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet1")
Dim PowerpointApp As Object
Dim myPresentation As Object
Set PowerpointApp = CreateObject("Powerpoint.Application")
PowerpointApp.Visible = True
Dim myPath As String
myPath = ws.Range("wk_dir").Value & "\" & ws.Range("ppt_name").Value
Set myPresentation = PowerpointApp.presentations.Open(myPath)
myPresentation.SaveAs (ws.Range("wk_dir").Value & "\test_latebind.pptx")
Set myPresentation = Nothing
Set PowerpointApp = Nothing
End Sub

Excel/PowerPoint VBA and late binding

I've been building a tool to generate PowerPoint slides from data in an excel workbook. I move back and forth between a computer at work and my computer at home. The work computer has Excel 2013 while the home computer has 2016. This generally isn't an issue...when I move from the home computer to the work computer I just have to change the reference from the v16 object library to the v15 object library.
Earlier this week though I ran into an error I couldn't resolve...detailed here One of the suggestions to resolve it was to switch to late binding so I didn't need the reference. That was a pretty easy switch, but it has led to an error in the resulting ppt slide...
In the original (early binding) version I set things up like
Dim PPApp As PowerPoint.Application
Dim PPPres As PowerPoint.Presentation
Dim PPSlide As PowerPoint.Slide
Dim PPLayout As CustomLayout
Dim PPshape As Variant
Dim tBox As PowerPoint.Shape
And then
Set PPApp = New PowerPoint.Application
Set PPPres = PPApp.Presentations.Open(fileName, msoTrue, , msoFalse)
And then part of the slide was built with
wsGenerator.Activate
wsGenerator.Range("B32:L37").Select
Selection.Copy
With PPSlide.Shapes.PasteSpecial(ppPasteMetafilePicture)
.Top = 450
.Left = 50
.Height = 100
.Width = 325
End With
Part of the resulting slide looks like this
The table at the bottom is the part pasted by the above code.
When I change to late binding, I simply make these changes
'Dim PPApp As PowerPoint.Application
Dim PPApp As Object
'Dim PPPres As PowerPoint.Presentation
Dim PPPres As Object
'Dim PPSlide As PowerPoint.Slide
Dim PPSlide As Object
'Dim PPLayout As CustomLayout
Dim PPLayout As Object
'Dim tBox As PowerPoint.Shape
Dim tBox As Object
Dim PPshape As Variant
And then
'Set PPApp = New PowerPoint.Application
Set PPApp = CreateObject("PowerPoint.Application")
Everything else remains the same. The resulting chart now looks like this
Note that it now extends off the bottom of the slide.
Any ideas as to what that's about?
It seems that you've changed your declarations to late-binding, but you may still be using some of the PowerPoint constants (such as ppPasteMetafilePicture), which will no longer resolve to their early-bound values, and instead default to 0.
You'll need to define a local constant for ppPasteMetafilePicture in order for the value to be available.
As an aside, you should always use Option Explicit and then the VBE will automatically spot usages of undeclared constants.

Using VBA to copy data from excel to powerpoint - Error

I'm trying to use VBA to copy data from excel to powerpoint. I've got the following code which I believe should work but it keeps giving me an error even though I've declared and specific all of the variables.
Sub CopyToPPT()
Dim DestinationPPT As String
Dim rng As Range
Dim PowerPointApp As PowerPoint.Application
Dim myPresentation As PowerPoint.Presentation
Dim mySlide As Object
Dim myShape As Object
Dim myShapeRange As Range
DestinationPPT = "C:\powerpoint.pptx"
'Open Powerpoint
Set myPresentation = PowerPointApp.Presentations.Open(DestinationPPT)
Set rng = ThisWorkbook.ActiveSheet.Range("B2:D14")
Set mySlide = myPresentation.Slides(5)
Set myShapeRange = mySlide.Shapes(mySlide.Shapes.Count)
'Copy
rng.copy
'Paste
mySlide.Shapes.PasteSpecial DataType:=2
Set myShape = mySlide.Shapes(mySlide.Shapes.Count)
myShapeRange.Left = 234
myShapeRange.Top = 186
End Sub
It doesn't seem to like the line
Set myPresentation = PowerPointApp.Presentations.Open(DestinationPPT)
Any idea how I can fix this? When I try running it I get the error:
Run-time error '91':
Object variable or With block variable not set
First of all, i'd strongly recommend to read about Early and late binding
You have to create new instance of PowerPoint application before you'll try to open presentation.
This should work:
'your code
Set PowerPointApp = New PowerPoint.Application
'the rest of your code
'Open Powerpoint
Set myPresentation = PowerPointApp.Presentations.Open(DestinationPPT)

VBA gettng data from excel

I currently have a powerpoint with a chart that was generated through an excel.
What I need to do is get the values of the chart (or the excel, doesn't matter) in order to do some animations.
The problem is that I can't seem to get my code to work.
If there is ANY easier way to do this I will be glad to hear it!
Here's my code:
Sub moveRectangle()
Dim pptChart As Chart
Dim pptcd As ChartData
Dim xlWorkbook As Object
Dim PPPres As Presentation
Dim pptShape2 As Shape
Dim wb As Excel.Workbook
Dim ws As Excel.Worksheet
Dim sld As Slide
Dim shp As Shape
Dim PPApp As PowerPoint.Application
'Look for existing instance
On Error Resume Next
Set PPApp = GetObject(, "PowerPoint.Application")
On Error GoTo 0
'Create new instance if no instance exists
Set PPApp = CreateObject("Powerpoint.Application")
Set PPPres = PPApp.ActivePresentation
Set pptShape = PPPres.Slides(1).Shapes("Rectangle 16")
Set pptShape2 = PPPres.Slides(1).Shapes("Chart 3")
Set pptChart = pptShape2.Chart
Set pptcd = pptChart.ChartData
MsgBox (pptShape2.Name)
Set wb = pptcd.Workbook
Set ws = wb.Worksheets(1)
pptShape.Left = pptShape.Left - 40
End Sub
The problem is that I'm getting the following error:
Method 'Workbook' of Object 'ChartData' failed
Any help is greately appreciated!
In order to get it working without "activating" excel (which exists full screen mode, pretty annoying), what must be done is adding
With pptChart.ChartData
...
End With
This allows you to get the same functionalities without having to "activate excel"