Showing posts with label close open office documents. Show all posts
Showing posts with label close open office documents. Show all posts

Wednesday, January 5, 2011

How to close open office documents

In the below code, we have a sample for opening any openoffice document as well as CLOSE method provides closing statements for the documents.

//////////////////////////////////////////////////////////////////////////////////////////////////
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;
      }
    }
  }
}