Tuesday, December 21, 2010

Open and run Word, Excel, PowerPoint Applications

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using Microsoft.Office.Core;
using Microsoft.Office.Interop.Excel;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using TTA.Util;
using System.Diagnostics;
using System.Runtime.InteropServices;



namespace TTA
{
    public partial class MicrosoftOffice : Form
    {
        public static PowerPoint.Application objApp;
        public static PowerPoint.Presentations objPresSet;
        public static PowerPoint._Presentation objPres;
        public static PowerPoint.Slides objSlides;
        public static PowerPoint._Slide objSlide;
        public static string ProcessName = null;

        public MicrosoftOffice()
        {
            InitializeComponent();
        }

        [DllImport("user32.dll", SetLastError = true)]
        static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);


        static void PowerpointWindow()
        {
           
          
            //Create a new presentation based on a template.
            objApp = new PowerPoint.Application();
            objApp.Visible = MsoTriState.msoTrue;
            objPresSet = objApp.Presentations;
            objPres = objPresSet.Open("D:\\Temp\\GDR.ppt", MsoTriState.msoCTrue, MsoTriState.msoTrue, MsoTriState.msoTrue);
            objSlides = objPres.Slides;

            //Build Slide 1:
            objSlide = objSlides.Add(1, PowerPoint.PpSlideLayout.ppLayoutTitleOnly);
            objPres.SaveAs("D:\\Temp\\GDR.ppt", Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsDefault, MsoTriState.msoTrue);
            ProcessName = "POWERPNT";
         
        }
        public bool CloseWindow(PowerPoint.Application App)
        {
            return true;
           
           
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (GlobalData.GetMicrosoftOfficeWindowState())
            {
                timer1.Enabled = false;
                timer1.Enabled = false;

                //if (objApp.Windows.Count == 0)
                //{
                   
                   
                //    MessageBox.Show("Powerpoint closed from outside.");
                //    System.Windows.Forms.Application.Exit();
                //}
                Process[] processes = System.Diagnostics.Process.GetProcessesByName(ProcessName, ".");
                foreach (Process p1 in processes)
                {
                    p1.Kill();
                }
               
               
               
                ImpressResult result = new ImpressResult();
                result.MdiParent = this.MdiParent;
                result.Show();
                this.Close();

            }
        }

        private void MicrosoftOffice_Load(object sender, EventArgs e)
        {
            //PowerpointWindow();
            //OpenExcel();
            word();
            CountDownTimer ct = new CountDownTimer();
            ct.Show();
            timer1.Interval = 1000;
            timer1.Enabled = true;

         
          
           
           
        }

        static void OpenExcel()
        {
            object missing = System.Reflection.Missing.Value;
            Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
            excelApp.Visible = true;
            Microsoft.Office.Interop.Excel.Workbook newWorkbook = excelApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
            string workbookPath = "D:\\Temp\\SomeWorkBook2.xlsx";
            newWorkbook.SaveAs(workbookPath, missing, missing, missing, true, missing, XlSaveAsAccessMode.xlNoChange, missing, missing, missing, missing, missing);
            excelApp.Quit();
           
            excelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
            excelApp.Visible = true;
            Microsoft.Office.Interop.Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(workbookPath,
                0, false, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
            ProcessName = "EXCEL";
        }

        static void word()
        {
            Microsoft.Office.Interop.Word.ApplicationClass WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
            object fileName = @"D:\Temp\sam.doc";
            object readOnly = false;
            object isVisible = true;
            // Here is the way to handle parameters you don't care about in .NET
            object missing = System.Reflection.Missing.Value;
            // Make word visible, so you can see what's happening
            WordApp.Visible = true;
            // Open the document that was chosen by the dialog
            Microsoft.Office.Interop.Word.Document aDoc = WordApp.Documents.Open(ref fileName, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing);
            // Activate the document so it shows up in front
            aDoc.Activate();
            ProcessName = "WINWORD";
           
           
        }



    }
}

No comments:

Post a Comment