Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
I want to do OpenOffice Automation in winform application without using user control using c# .net.
Here is my code ,
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Windows.Forms;
using Microsoft.Win32;
using OOLib;

using System.Diagnostics;
using System.Runtime.InteropServices;

using unoidl.com.sun.star.uno;
using unoidl.com.sun.star.lang;
using unoidl.com.sun.star.frame;

namespace Sample
{
	public partial class Form1 : Form
	{
		private const int SW_SHOWNORMAL = 1;

		[DllImport("user32.dll")]
		public static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);

		[DllImport("user32.dll")]
		static extern int SetParent(int hWndChild, int hWndNewParent);
		
		public ObservableCollection<oolib.officedocument> Docs { get; private set; }
		private OOLib.ServiceManager _ServiceManager = null;
		public OOLib.Desktop Desktop { get; private set; }

		public Form1()
		{
			Docs = new ObservableCollection<oolib.officedocument>();
			InitializeComponent();
		}

		private void Form1_Load(object sender, EventArgs e)
		{
		}

		private void Init()
		{
			if(_ServiceManager == null)
			{
				_ServiceManager = new OOLib.ServiceManager();
				Desktop = _ServiceManager.CreateDesktop();
			}
			if(Desktop != null)
			{
				var doc = Desktop.CreateTextDocument(hidden = true);
				Docs.Add(doc);
			}
		}
		private static void FindOpenOffice()
		{
			try
			{
				//Process currentProcess = Process.GetCurrentProcess();  //in this line getting MainWindowHandle.
				Process[] ps = Process.GetProcessesByName("soffice");
				if(ps != null)
				{
					if(ps.Length > 0)
						foreach(Process p in ps)
						{							
							// get mainwindow handle .														
							IntPtr pFoundWindow = p.MainWindowHandle;

							// Use Handle to view in normal window.
							if(!pFoundWindow.Equals(IntPtr.Zero))
							{
								ShowWindowAsync(pFoundWindow, SW_SHOWNORMAL);
							}
						}
				}
				else
				{
					MessageBox.Show("OpenOffice not found. Is OpenOffice installed?");
				}
			}
			catch(System.Exception Exp)
			{
				MessageBox.Show(Exp.Message);
			}
		}
		public bool hidden { get; set; }

		private void btnOpen_Click(object sender, EventArgs e)
		{
			Init();
			FindOpenOffice();
		}
	}
}



But in above code ,MainWIndowHandle is ZERO

, please help me if any one has idea .
Thanks
Posted
Updated 14-May-14 2:26am
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900