-1

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
SandPiper
  • 2,816
  • 5
  • 30
  • 52
thePB
  • 41
  • 2
  • 9
  • 1
    `On Error Resume Next` is a particularly bad idea here IMO. If you can't create the PowerPoint app instance, your code should bail out, not keep running as if nothing happened. – Mathieu Guindon Jan 31 '17 at 21:43
  • 1
    Can you [edit] your question to clarify exactly what that code snippet is? Is it your attempt at making an invisible `pptApp`? Or an excerpt from the code that opens an existing ppt file? Because that code doesn't open anything at all, and seems to specify `.Visible = False`, so it's not clear how it connects with the actual question. – Mathieu Guindon Jan 31 '17 at 21:45
  • Also... you seem to have the PowerPoint library referenced (`As PowerPoint.Application` *does* compile, right?) - so why then are you late-binding the creation of the application instance? Just do `Set pptApp = New PowerPoint.Application`. – Mathieu Guindon Jan 31 '17 at 21:48
  • To clarify, `Err.Clear` is ***not*** the same thing as `On Error GoTo 0`. The first just wipes the logged error - the second one actually re-enables error trapping. – Comintern Jan 31 '17 at 21:51
  • Thanks. I edited my code. I need to have the On error Resume Next because i paste some pictures on the slides and it gives me an error without that code, but wth it it pastes perfectly. Now, with the edited code, the presentation is still visible! – thePB Feb 01 '17 at 13:29
  • Also, i made the question really fast (sorry about that) and forgot to erase the .Visible = False, because it doesnt do anything. It even gives me an error saying i cant force the app to not be visible with that code. thanks – thePB Feb 01 '17 at 13:45

1 Answers1

2

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
Comintern
  • 21,855
  • 5
  • 33
  • 80
  • Thanks. Ok, i edited my code according to your post and the other comments. But it is still visible, maybe because of my pptpresestation.add code? – thePB Feb 01 '17 at 13:28
  • 2
    Presentations.Add takes an optional parameter, WithWindow. Set pptPres = Presentations.Add(msoFalse) will give you a reference to a windowless (invisible) presentation that you can do most, if not all, things with. – Steve Rindsberg Feb 01 '17 at 17:11
  • I'm using .Add to open an empty presentation file, then inserting files from an external file. When visible, this works fine however PPT crashes when I try to use invisible presentations... – Ted Fitzpatrick May 07 '18 at 22:09