Archive | December 2010

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

Try
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,


objPPT.Presentations.Open(,,,,WithWindow:=Microsoft.Office.Core.MsoTriState.msoFalse)

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
Try
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
Try
strName = objPPT.Name
Catch ex As Exception
StartPowerPoint()
End Try
'
'blnAddPresentation is used to ensure there is a presentation loaded
If blnAddPresentation = True Then
Try
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
'presentation
If blnAddSlide Then
Try
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
objPres.SaveAs("c:\MyPresentation.pptx")
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 😉

Advertisements

The Magic of Binding and INotifyPropertyChanged in WPF

Recently I found out that “Any sufficiently advanced technology is indistinguishable from magic” – Clarke’s third law.

I was surfing through internet for some good WPF examples and came to the post of Pete Brown on Puff the Magic POCO Binding and INotifyPropertyChanged in WPF so thaught of trying it out.

I’ll try to explain the same in much simpler way (as I did it) for extremely new developer in WPF.

Here we go…

1. Open Visual Studio 2010 -> File -> New Project -> Visual C# -> WPF Application

2. Paste below XAML CODE in your code.

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ListBox x:Name="PeopleList"
                 Height="287"
                 HorizontalAlignment="Left"
                 DisplayMemberPath="LastName"
                 Margin="12,12,0,0"
                 VerticalAlignment="Top"
                 Width="138" />
        <TextBox Height="23"
                 HorizontalAlignment="Left"
                 Margin="265,24,0,0"
                 x:Name="LastNameField"
                 VerticalAlignment="Top"
                 DataContext="{Binding ElementName=PeopleList, Path=SelectedItem}"
                 Text="{Binding LastName}"
                 Width="120" />
        <TextBlock Height="23"
                   HorizontalAlignment="Left"
                   Margin="178,27,0,0"
                   Text="Last Name"
                   VerticalAlignment="Top" />
    </Grid>
</Window>

3. In the code-behind add a Person Class:

class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}

4. Here’s the rest code-behind’s code :

public partial class MainWindow : Window
{
private ObservableCollection _people;
public MainWindow()
{
InitializeComponent();

_people = new ObservableCollection();

_people.Add(new Person() { LastName = "Brown" });
_people.Add(new Person() { LastName = "Fitz" });
_people.Add(new Person() { LastName = "Hanselman" });
_people.Add(new Person() { LastName = "Heuer" });
_people.Add(new Person() { LastName = "Galloway" });
_people.Add(new Person() { LastName = "Stagner" });
_people.Add(new Person() { LastName = "Liberty" });
_people.Add(new Person() { LastName = "Litwin" });
_people.Add(new Person() { LastName = "Papa" });
_people.Add(new Person() { LastName = "Guthrie" });

PeopleList.ItemsSource = _people;
}
}

That’s it. Now run the appliction (by presssing F5 key), select any value from the List Box and it will appear into the Text Box.

The Magic of Binding and INotifyPropertyChanged in WPF

OK, so what’s the magic over here??!!

Select any value from the List Box, once it appear on Text Box, Change the value into the Text Box and press TAB.

Woha!!!! Magic!!! isn’t it??

As sson as you press the TAB key, the value also got changed into the List Box!!!!!

Hmm….now let’s try to understand what’s just happening over here. That’s calledthe magic of PropertyDescriptor.

The PropertyDescriptor has the AddValueChanged method by which a listener can sign up to get the property change notifications from that descriptor. And to add on, this will only work with binding. If you update the values from code-behind, this magic won’t occur.

I suggest you to read the post of Pete Brown on Puff the Magic POCO Binding and INotifyPropertyChanged in WPF for more in depth details.

A DeepZoom Silverlight Application

A really great feature added by microsoft to interact with images and create amazing zooming applications is Deep Zoom.

In short, Deep Zoom provides the ability to interactively view high-resolution images.

You can zoom in and out of images rapidly without affecting the performance of your application. Deep Zoom enables smooth loading and panning by serving up multi-resolution images and using spring animations.

With its help we can create amazing web experience. Some of the best examples would be Hard Rock Memorablia, Deep Zoom Pix.

How to Create the simplest Deep Zoom application

  1. Install Deep Zoom Composer .
  2. After installation create a new project. Go to File -> New project. A new project will be created with a .dzprj extension.
  3. Collect few good quality pictures.
  4. Start importing it in your project by clicking the add image button.
  5. Prepare a storyboard in your mind. By storyboard I mean how you would like your application to react on mouse events.
  6. Start positioning and composing the images by clicking on the compose tab.
  7. Select an image which would serve as your base image. For example, if I want to make an application similar to Google Earth, then I will take the satellite image of the world as my base image.
  8. Drag and drop the base image into the storyboard area and make it  fit to screen.
  9. Add rest of the images on to the base image and reduce their size, till it looks like small yellow lense on your base image.
  10. Add more images and do the same.
  11. Your deep zoom application is ready to be exported!!
  12. Export it and keep zoomin!!!
     
  13. Add more images and do the same. Your deep zoom application is ready to be exported!!

    Export it and keep zoomin!!!

%d bloggers like this: