/// <summary>
/// Loads a document into the control
/// </summary>
/// <param name="t_filename">path to the file (every type word can handle)</param>
public void LoadDocument(string t_filename)
{
deactivateevents = true;
filename = t_filename;
if(wd == null) wd = new Word.ApplicationClass();
try
{
wd.CommandBars.AdaptiveMenus = false;
wd.DocumentBeforeClose += new Word.ApplicationEvents2_DocumentBeforeCloseEventHandler(OnClose);
wd.NewDocument += new Word.ApplicationEvents2_NewDocumentEventHandler(OnNewDoc);
wd.DocumentOpen+= new Word.ApplicationEvents2_DocumentOpenEventHandler(OnOpenDoc);
wd.ApplicationEvents2_Event_Quit += new Word.ApplicationEvents2_QuitEventHandler(OnQuit);
}
catch{}
int counter = wd.ActiveWindow.Application.CommandBars.Count;
for(int i = 0; i < counter;i++)
{
try
{
wd.ActiveWindow.Application.CommandBars[i].Enabled=false;
}
catch
{
}
}
try
{
wd.Visible = true;
wd.Activate();
SetWindowPos(wordWnd,this.Handle.ToInt32(),0,0,this.Bounds.Width+20,this.Bounds.Height+20, SWP_NOZORDER | SWP_NOMOVE | SWP_DRAWFRAME);
MoveWindow(wordWnd,-5,-33,this.Bounds.Width+10,this.Bounds.Height+57,true);
}
catch
{
MessageBox.Show("Error: do not load the document into the control until the parent window is shown!");
}
this.Parent.Focus();
}
deactivateevents = false;
}
/// <summary>
/// restores Word.
/// If the program crashed somehow.
/// Sometimes Word saves it's temporary settings :(
/// </summary>
public void RestoreWord()
{
try
{
int counter = wd.ActiveWindow.Application.CommandBars.Count;
for(int i = 0; i < counter;i++)
{
try
{
wd.ActiveWindow.Application.CommandBars[i].Enabled=true;
}
catch
{
}
}
}
catch{};
}
/// <summary>
/// internal resize function
/// utilizes the size of the surrounding control
///
/// optimzed for Word2000 but it works pretty good with WordXP too.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnResize(object sender, System.EventArgs e)
{
MoveWindow(wordWnd,-5,-33,this.Bounds.Width+10,this.Bounds.Height+57,true);
}
}
}
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WinWordControl
{
public class DocumentInstanceException : Exception
{}
public class ValidDocumentException : Exception
{}
public class WordInstanceException : Exception
{}
public class WinWordControl : System.Windows.Forms.UserControl
{
[DllImport("user32.dll")]
public static extern int FindWindow(string strclassName, string strWindowName);
[DllImport("user32.dll")]
static extern int SetParent( int hWndChild, int hWndNewParent);
[DllImport("user32.dll", EntryPoint="SetWindowPos")]
static extern bool SetWindowPos(
int hWnd, // handle to window
int hWndInsertAfter, // placement-order handle
int X, // horizontal position
int Y, // vertical position
int cx, // width
int cy, // height
uint uFlags // window-positioning options
);
[DllImport("user32.dll", EntryPoint="MoveWindow")]
static extern bool MoveWindow(
int爃Wnd,
int X,
int Y,
int爊Width,
int爊Height,
bool燽Repaint
);
const int SWP_DRAWFRAME = 0x20;
const int SWP_NOMOVE = 0x2;
const int SWP_NOSIZE = 0x1;
const int SWP_NOZORDER = 0x4;
#region Component Designer generated code
/// <summary>
/// !do not alter this code! It's designer code
/// </summary>
private void InitializeComponent()
{
//
// WinWordControl
//
this.Name = "WinWordControl";
this.Size = new System.Drawing.Size(440, 336);
this.Resize += new System.EventHandler(this.OnResize);
}
#endregion
/// <summary>
/// Preactivation
/// It's usefull, if you need more speed in the main Program
/// so you can preload Word.
/// </summary>
public void PreActivate()
{
if(wd == null) wd = new Word.ApplicationClass();
}
/// <summary>
/// Close the current Document in the control --> you can
/// load a new one with LoadDocument
/// </summary>
public void CloseControl()
{
/*
* this code is to reopen Word.
*/
/// <summary>
/// catches Word's quit event
/// normally it should not fire, but just to be shure
/// safely release the internal Word Instance
/// </summary>
private void OnQuit()
{
//wd=null;
}