-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
6
  • 4
    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. Commented Jan 31, 2017 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. Commented Jan 31, 2017 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. Commented Jan 31, 2017 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. Commented Jan 31, 2017 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! Commented Feb 1, 2017 at 13:29

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

3 Comments

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?
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.
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...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.