გახსენით C#-ის პროექტი, შექმენით UserControl სახელად WinWordControl პროექტის Referenceb-ში დაამატეთ:
COM>Microsoft Word Object Library 10 რის მერეც USer CONTROL-ში ჩასვით ეს ტექსტი, და ვსო ყველაფერი მზადაა, გაქვთ თქვენი კომპონენტი რომლის მეშვეობითაც შეგიძლია გამოიყენო ვორდი სადაც ინდა და როცა გინდა.
კიდევ აგერაა მზა ფაილები და სანახავად თუ გინდათ გადაიწერეთ..
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
{
private const int WS_BORDER = 0x00800000;
private const int WS_CAPTION = 0x00C00000;
private const int GWL_STYLE = -16;
[DllImport("User32.dll", CharSet=CharSet.Auto)]
private static extern int GetWindowLong(IntPtr hWnd, int Index);
[DllImport("User32.dll", CharSet=CharSet.Auto)]
private static extern int SetWindowLong(IntPtr hWnd, int Index, int Value);
[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 hWnd,
int X,
int Y,
int nWidth,
int nHeight,
bool bRepaint
);
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
[DllImport("user32.dll")]
public static extern int GetWindowRect(int hwnd, ref RECT rc);
[DllImport("user32.dll")]
public static extern IntPtr PostMessage(
int hWnd,
int msg,
int wParam,
int lParam
);
const int SWP_DRAWFRAME = 0x20;
const int SWP_NOMOVE = 0x2;
const int SWP_NOSIZE = 0x1;
const int SWP_NOZORDER = 0x4;
private Word.Document document;
private static Word.ApplicationClass wd = null;
public static int wordWnd = 0;
private static string filename = null;
private static bool deactivateevents = false;
private System.ComponentModel.Container components = null;
public WinWordControl()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
CloseControl();
if( disposing )
{
if( components != null )
components.Dispose();
}
base.Dispose( disposing );
}
#region Component Designer generated code
private void InitializeComponent()
{
//
// WinWordControl
//
this.Name = "WinWordControl";
this.Size = new System.Drawing.Size(440, 336);
this.Resize += new System.EventHandler(this.OnResize);
}
#endregion
public void PreActivate()
{
if(wd == null) wd = new Word.ApplicationClass();
}
public void CloseControl()
{
try
{
deactivateevents = true;
object dummy=null;
document.Close(ref dummy, ref dummy, ref dummy);
document.Application.Quit(ref dummy, ref dummy, ref dummy);
deactivateevents = false;
}
catch
{
}
}
private void OnClose(Word.Document doc, ref bool chancel)
{
if(!deactivateevents)
{
chancel=true;
}
}
private void OnOpenDoc(Word.Document doc)
{
OnNewDoc(doc);
}
private void OnNewDoc(Word.Document doc)
{
if(!deactivateevents)
{
deactivateevents=true;
object dummy = null;
doc.Close(ref dummy,ref dummy,ref dummy);
deactivateevents=false;
}
}
private void OnQuit()
{
wd=null;
}
public void ActivateWord()
{
deactivateevents = true;
if(wd==null)wd=new Word.ApplicationClass();
try
{
wd.CommandBars.AdaptiveMenus = false;
wd.DocumentBeforeClose += new Word.ApplicationEvents3_DocumentBeforeCloseEventHandler(OnClose);
wd.ApplicationEvents2_Event_NewDocument += new Word.ApplicationEvents2_NewDocumentEventHandler(OnNewDoc);
wd.DocumentOpen+= new Word.ApplicationEvents3_DocumentOpenEventHandler(OnOpenDoc);
wd.ApplicationEvents2_Event_Quit += new Word.ApplicationEvents2_QuitEventHandler(OnQuit);
}
catch{}
if( wordWnd==0 ) wordWnd = FindWindow( "Opusapp", null);
if (wordWnd!=0)
{
SetParent( wordWnd, this.Handle.ToInt32());
object fileName = filename;
object newTemplate = false;
object docType = 0;
object readOnly = true;
object isVisible = true;
object missing = System.Reflection.Missing.Value;
try
{
if( wd == null )
{
throw new WordInstanceException();
}
if( wd.Documents == null )
{
throw new DocumentInstanceException();
}
}
catch
{
}
try
{
wd.ActiveWindow.DisplayRightRuler=false;
wd.ActiveWindow.DisplayScreenTips=false;
wd.ActiveWindow.DisplayVerticalRuler=false;
wd.ActiveWindow.DisplayRightRuler=false;
wd.ActiveWindow.ActivePane.DisplayRulers=false;
wd.ActiveWindow.ActivePane.View.Type = Word.WdViewType.wdWebView; // .wdNormalView;
}
catch
{
}
try
{
wd.Visible = true;
wd.Activate();
SetWindowLong((System.IntPtr)wordWnd, GWL_STYLE, GetWindowLong((System.IntPtr)wordWnd, GWL_STYLE) & (~(WS_CAPTION | WS_BORDER)));
MoveWindow(wordWnd,0,0,this.Bounds.Width+10,this.Bounds.Height+57,true);
SetWindowPos(wordWnd,this.Handle.ToInt32(),0,0,this.Bounds.Width+20,this.Bounds.Height+20, SWP_NOZORDER);
wd.Visible=false;
}
catch
{
MessageBox.Show("Error: do not load the document into the control until the parent window is shown!");
}
this.Parent.Focus();
}
deactivateevents = false;
}
public void LoadDocument(string t_filename)
{
filename = t_filename;
if(document != null)
{
try
{
object dummy=null;
wd.Documents.Close(ref dummy, ref dummy, ref dummy);
}
catch{}
}
object fileName = filename;
object newTemplate = false;
object docType = 0;
object readOnly = true;
object isVisible = true;
document = wd.Documents.Add(ref fileName,ref newTemplate,ref docType,ref isVisible);
int counter = wd.ActiveWindow.Application.CommandBars.Count;
for(int i = 0; i < counter;i++)
{
try
{
wd.ActiveWindow.Application.CommandBars[i].Enabled=false;
System.Windows.Forms.MessageBox.Show(wd.ActiveWindow.Application.CommandBars[i].Name);
}
catch
{
}
}
if(document == null)
{
throw new ValidDocumentException();
}
wd.Visible=true;
}
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{};
}
private void OnResize(object sender, System.EventArgs e)
{
MoveWindow(wordWnd,0,0,this.Bounds.Width+10,this.Bounds.Height+57,true);
}
}
}
მიმაგრებული ფაილი ( Number of downloads: 78 )
Debug.part01.rar