Hide Power Point Application Window in .NET (Office Automation)

Now a days I am working on some Windows Presentation Foundation (WPF) applications with Office Automation. I have created application where it was opening Microsoft Power Point files(I am using Microsoft Office 2007 version, i.e., *.pptx) and adding some data into it (Text, Pictures, Charts, etc.). That’s it!! Pretty simple ha!!!

Not exactly!! The biggest problem I was facing is when I am opening Power Point and adding data into it, it was splashing the screen also. The Power Point application was getting open and anyone can see it. It’s ok, not a big problem, but at the time when I have more than 100 slides!!!! It was not OK to let user see this opening and closing series of Power Point application.

So, what we do exactly when we get caught into something like this!! Aah!! Google πŸ™‚

I went to Google and typed Hide Power Point Application Window in .NET (Office Automation).

You may go ahead and check the above link, where I have surfed almost 4 hours and couldn’t find anything. Any single thing that will help me to achieve my goal. 😦 😦 😦

What to do now!!!! I am one of the person who believe in “Nothing Is Impossible” πŸ˜€

Ok, enough of theory ha?? Let’s Jump Then, I have the solution with me right now and I’m sharing it with you πŸ™‚

First In Short,

You will have to write your code like below,

objPPT = New PowerPoint.Application

objPPT.Visible = MsoTriState.msoFalse
Catch ex As Exception

End Try

You can’t write objPPT.Visible = MsoTriState.msoFalse alone without Try & Catch, because it will throw an error saying : Invalid request. Hiding the application window is not allowed.

So when you write it with Try & Catch block, it will work fine.

If it’s still giving some other error than the next step would be to write code as below,


That’s it.

Ok, now let’s see it into deep through the real code.

I have a demo application ready for you.

Steps :

1. Open Visual Studio 2010.

2. File -> New Project (VB.NET)

3. Go to Form1.vb (Source Code View)

4. Paste the below code as it is:

Imports Microsoft.Office.Interop
Imports Microsoft.Office.Interop.PowerPoint
Imports Microsoft.Office.Core

Public Class Form1
Dim objPPT As PowerPoint.Application
Dim objPres As PowerPoint.Presentation

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim objShape As PowerPoint.Shape
Dim strText As String = "This Has Been Created By : Naimish Pandya"
EnsurePowerPointIsRunning(True, True)

objShape = objPres.Slides(1).Shapes.AddTextbox(MsoTextOrientation.msoTextOrientationHorizontal, 50, 300, 300, 300)
objShape.TextFrame.AutoSize = PowerPoint.PpAutoSize.ppAutoSizeShapeToFitText
objShape.TextFrame.TextRange.Text = strText
objShape.TextEffect.FontSize = 20
objShape.TextEffect.FontBold = MsoTriState.msoTrue
'Clean up
objShape = Nothing
Button2.Enabled = True

End Sub

Sub StartPowerPoint()
objPPT = New PowerPoint.Application
objPPT.Visible = MsoTriState.msoFalse
Catch ex As Exception

End Try
End Sub
Sub EnsurePowerPointIsRunning(Optional ByVal blnAddPresentation As Boolean = False, Optional ByVal blnAddSlide As Boolean = False)
Dim strName As String
'Try accessing the name property. If it causes an exception then
'start a new instance of PowerPoint
strName = objPPT.Name
Catch ex As Exception
End Try
'blnAddPresentation is used to ensure there is a presentation loaded
If blnAddPresentation = True Then
strName = objPres.Name
Catch ex As Exception
objPres = objPPT.Presentations.Add(MsoTriState.msoTrue)
End Try
End If
'BlnAddSlide is used to ensure there is at least one slide in the
If blnAddSlide Then
strName = objPres.Slides(1).Name
Catch ex As Exception
Dim objSlide As PowerPoint.Slide
Dim objCustomLayout As PowerPoint.CustomLayout
objCustomLayout = objPres.SlideMaster.CustomLayouts.Item(1)
objSlide = objPres.Slides.AddSlide(1, objCustomLayout)
objSlide.Layout = PowerPoint.PpSlideLayout.ppLayoutText
objCustomLayout = Nothing
objSlide = Nothing
End Try
End If
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Button2.Enabled = False
End Sub
End Class

Now run the application :

Hide Power Point Window

Hide Power Point Window

Try it out yourself and please let me know in case anything is not correct πŸ˜‰


Tags: , , , ,

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: