Tuesday, December 21, 2010

Run Open Office Applications

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TTA.Util;
using unoidl.com.sun.star.frame;
using unoidl.com.sun.star.lang;
using unoidl.com.sun.star.beans;

namespace TTA.Util
{
  public class OpenOffice
  {
    XComponent xComponent;
    XComponentLoader componentLoader;
    unoidl.com.sun.star.lang.XMultiServiceFactory multiServiceFactory;
    unoidl.com.sun.star.uno.XComponentContext localContext;

    public OpenOffice(string FileName, string fileType)
    {
      localContext = uno.util.Bootstrap.bootstrap();
      multiServiceFactory = (unoidl.com.sun.star.lang.XMultiServiceFactory)localContext.getServiceManager();
      componentLoader = (XComponentLoader)multiServiceFactory.createInstance("com.sun.star.frame.Desktop");
      PropertyValue[] loadDesc = new PropertyValue[1];
      loadDesc[0] = new PropertyValue();

      loadDesc[0].Name = "Hidden";
      loadDesc[0].Value = new uno.Any(true);
      xComponent = componentLoader.loadComponentFromURL(fileType, "_blank", 0, loadDesc);
      ((XStorable)xComponent).storeToURL(General.PathConverter(FileName), new unoidl.com.sun.star.beans.PropertyValue[0]);
      xComponent.dispose();
      xComponent = componentLoader.loadComponentFromURL(General.PathConverter(FileName), "_blank", 0, new unoidl.com.sun.star.beans.PropertyValue[0]);
    }

    public bool Close()
    {
      try
      {
        ((XStorable)xComponent).store();
        xComponent.dispose();
        return true;
      }
      catch
      {
        return false;
      }
    }
  }
}



///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Now to open the application
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
private void Impress_Load(object sender, EventArgs e)
    {
      string FileName = General.GetAppString("UserDocuments") + GlobalData.GetStudentInfo()[0].ToString().Trim() + "_" + GlobalData.GetUserName() + General.GetFileExtension(General.GetAppString("OpenOfficeApp1"));
      GlobalData.SetFileName(FileName);
      op = new OpenOffice(FileName, "private:factory/" + General.GetAppName(General.GetAppString("OpenOfficeApp1")));
      CountDownTimer ct = new CountDownTimer();
      ct.Show();
      timer1.Interval = 1000;
      timer1.Enabled = true;
      Process[] procs = System.Diagnostics.Process.GetProcessesByName("SOFFICE", ".");
      IntPtr hWnd = procs[0].MainWindowHandle;
      SwitchToThisWindow(hWnd, false);
    }

No comments:

Post a Comment