با سلام
مدت ها بدنبال این بودم که برنامه ورد آفیس رو در داخل فرم برنامه ام بازکنم
خیلی گشتم و راه های مختلف رو امتحان کردم
خیلی اذیت شدم !
تصمصم گرفتم که نتیجه کارم رو به اشتراک بذارم.
البته معما چون حل شود ، آسان شود!

دو روش وجود دارد:

روش اول:

این روش برای اضافه کردن تقریبا هر application می توان استفاده کرد
ایرادش اینه که شما نمی تونید از دستور های خود ورد استفاده کنید . مانند save ،Print و غیره

namespace WinApp
{

public partial class Form1 : Form
{
#region Methods/Consts for Embedding a Window
[DllImport("user32.dll", EntryPoint = "GetWindowThreadProcessId", SetLastError = true,
CharSet = CharSet.Unicode, ExactSpelling = true,
CallingConvention = CallingConvention.StdCall)]
private static extern long GetWindowThreadProcessId(long hWnd, long lpdwProcessId);

[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImport("user32.dll", SetLastError = true)]
private static extern long SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

[DllImport("user32.dll", EntryPoint = "GetWindowLongA", SetLastError = true)]
private static extern long GetWindowLong(IntPtr hwnd, int nIndex);

[DllImport("user32.dll")]
static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

[DllImport("user32.dll", SetLastError = true)]
private static extern long SetWindowPos(IntPtr hwnd, long hWndInsertAfter, long x, long y, long cx, long cy, long wFlags);

[DllImport("user32.dll", SetLastError = true)]
private static extern bool MoveWindow(IntPtr hwnd, int x, int y, int cx, int cy, bool repaint);

[DllImport("user32.dll", EntryPoint = "PostMessageA", SetLastError = true)]
private static extern bool PostMessage(IntPtr hwnd, uint Msg, int wParam, int lParam);

private const int SWP_NOOWNERZORDER = 0x200;
private const int SWP_NOREDRAW = 0x8;
private const int SWP_NOZORDER = 0x4;
private const int SWP_SHOWWINDOW = 0x0040;
private const int WS_EX_MDICHILD = 0x40;
private const int SWP_FRAMECHANGED = 0x20;
private const int SWP_NOACTIVATE = 0x10;
private const int SWP_ASYNCWINDOWPOS = 0x4000;
private const int SWP_NOMOVE = 0x2;
private const int SWP_NOSIZE = 0x1;
private const int GWL_STYLE = (-16);
private const int WS_VISIBLE = 0x10000000;
private const int WM_CLOSE = 0x10;
private const int WS_CHILD = 0x40000000;
private const int WS_MAXIMIZE = 0x01000000;

#endregion

#region Variables


private IntPtr gpsHandle;
private Process gpsProcess = null;
private ProcessStartInfo gpsPSI = new ProcessStartInfo();
private System.Windows.Forms.Panel gpsPanel;

#endregion

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

this.gpsPanel = new System.Windows.Forms.Panel();
//this.gpsPanel.Location = new Point(this.LeftBarRight, this.TopBarBottom);
this.gpsPanel.Size = new System.Drawing.Size(this.Width - 50, this.Height - 50);
this.gpsPanel.Visible = true;
this.Controls.Add(gpsPanel);




gpsPSI.FileName = @"C:\Program Files\Microsoft Office\root\Office16\WINWORD.EXE";
gpsPSI.Arguments = "";
gpsPSI.WindowStyle = ProcessWindowStyle.Minimized;
gpsProcess = System.Diagnostics.Process.Start(gpsPSI);
gpsProcess.WaitForInputIdle();

while (string.IsNullOrEmpty(gpsProcess.MainWindowTitle) || gpsProcess.MainWindowTitle.ToLower().IndexOf("open ing") >= 0)
{
System.Threading.Thread.Sleep(100);
gpsProcess.Refresh();

}


gpsHandle = gpsProcess.MainWindowHandle;
SetParent(gpsHandle, this.gpsPanel.Handle);
SetWindowLong(gpsHandle, GWL_STYLE, WS_VISIBLE + WS_MAXIMIZE);
MoveWindow(gpsHandle, 0, 0, this.gpsPanel.Width, this.gpsPanel.Height, true);



}

}
}

روش دوم :
از interop آفیس استفاده کنید
اشکالش اینه که برای همه application ها نمی تونید استفاده کنید و حتما با طراح application رابط مناسب را داده باشه و راهنمایی کنه



0




namespace Winpp2
{

public partial class Form1 : Form
{
#region Methods/Consts for Embedding a Window
[DllImport("user32.dll", EntryPoint = "GetWindowThreadProcessId", SetLastError = true,
CharSet = CharSet.Unicode, ExactSpelling = true,
CallingConvention = CallingConvention.StdCall)]
private static extern long GetWindowThreadProcessId(long hWnd, long lpdwProcessId);

[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImport("user32.dll", SetLastError = true)]
private static extern long SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

[DllImport("user32.dll", EntryPoint = "GetWindowLongA", SetLastError = true)]
private static extern long GetWindowLong(IntPtr hwnd, int nIndex);

[DllImport("user32.dll")]
static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

[DllImport("user32.dll", SetLastError = true)]
private static extern long SetWindowPos(IntPtr hwnd, long hWndInsertAfter, long x, long y, long cx, long cy, long wFlags);

[DllImport("user32.dll", SetLastError = true)]
private static extern bool MoveWindow(IntPtr hwnd, int x, int y, int cx, int cy, bool repaint);

[DllImport("user32.dll", EntryPoint = "PostMessageA", SetLastError = true)]
private static extern bool PostMessage(IntPtr hwnd, uint Msg, int wParam, int lParam);

private const int SWP_NOOWNERZORDER = 0x200;
private const int SWP_NOREDRAW = 0x8;
private const int SWP_NOZORDER = 0x4;
private const int SWP_SHOWWINDOW = 0x0040;
private const int WS_EX_MDICHILD = 0x40;
private const int SWP_FRAMECHANGED = 0x20;
private const int SWP_NOACTIVATE = 0x10;
private const int SWP_ASYNCWINDOWPOS = 0x4000;
private const int SWP_NOMOVE = 0x2;
private const int SWP_NOSIZE = 0x1;
private const int GWL_STYLE = (-16);
private const int WS_VISIBLE = 0x10000000;
private const int WM_CLOSE = 0x10;
private const int WS_CHILD = 0x40000000;
private const int WS_MAXIMIZE = 0x01000000;

#endregion

#region Variables


private IntPtr gpsHandle;
private Process gpsProcess = null;
private ProcessStartInfo gpsPSI = new ProcessStartInfo();
private System.Windows.Forms.Panel gpsPanel;

#endregion

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
this.gpsPanel = new System.Windows.Forms.Panel();
//this.gpsPanel.Location = new Point(this.LeftBarRight, this.TopBarBottom);
this.gpsPanel.Size = new System.Drawing.Size(this.Width - 50, this.Height - 50);
this.gpsPanel.Visible = true;
this.Controls.Add(gpsPanel);


Microsoft.Office.Interop.Word._Application oWord = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word._Document oWordDoc = new Microsoft.Office.Interop.Word.Document();
object oMissing = System.Reflection.Missing.Value;
object oTrue = true;
object oFalse = false;


object oTempDocPN = (object)"Z:\\Documents\\Markaz2\\100.docx";
oWord.Visible = true;
oWord.WindowState = Microsoft.Office.Interop.Word.WdWindowState.wdWind owStateMaximize;

oWord.Documents.Open(ref oTempDocPN, ref oMissing, ref oMissing, ref oFalse, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);


oWord.ActiveWindow.ActivePane.View.Type = Microsoft.Office.Interop.Word.WdViewType.wdPrintVi ew;
//MessageBox.Show( oWord.ActiveWindow.Hwnd.ToString());
gpsHandle = (IntPtr)oWord.ActiveWindow.Hwnd;
SetParent(gpsHandle, this.gpsPanel.Handle);


SetParent(gpsHandle, this.gpsPanel.Handle);
SetWindowLong(gpsHandle, GWL_STYLE, WS_VISIBLE + WS_MAXIMIZE);
MoveWindow(gpsHandle, 0, 0, this.gpsPanel.Width, this.gpsPanel.Height, true);

//now you can use oWord.Documents[0].SaveAs2 TO SAVE YOUR DOCUMENT
// oWord.Documents[0].PrintOut TO PRINT



}

}
}
/////////////////////////////////////////////////////////////////////

در روش دوم انطوری می تونید از save و print خود [ورد استفاده کنید

try
{

foreach (Microsoft.Office.Interop.Word.Document oTempDoc in oWord.Documents)
{



object oFileFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdForma tXMLDocument;
object oLockcomments = oMissing;
object oPassword = oMissing;
object oAddtorecentFiles = oTrue;
object oWritePassword = oMissing;
object oReadonlyRecommanded = oMissing;
object oEmbedTrueTypeFont = oMissing;
object oSaveNativePictureFormat = oTrue;
object oSaveFormsdata = oMissing;
object oSaveasAOCEletter = oMissing;
object oEncoding = oMissing;
object oInsertlineBreaks = oMissing;
object oAllowSubstition = oMissing;
object oLineEnding = oMissing;
object oAddbidiMarks = oMissing;


object oSaveFileName = (object)(sSaveNamePath + ".docx");
oTempDoc.SaveAs(ref oSaveFileName,
ref oFileFormat,
ref oLockcomments,
ref oPassword,
ref oAddtorecentFiles,
ref oWritePassword,
ref oReadonlyRecommanded,
ref oEmbedTrueTypeFont,
ref oSaveNativePictureFormat,
ref oSaveFormsdata,
ref oSaveasAOCEletter,
ref oEncoding,
ref oInsertlineBreaks,
ref oAllowSubstition,
ref oLineEnding,
ref oAddbidiMarks);





}
}

catch (System.Runtime.InteropServices.COMException ce)
{


MessageBox.Show("مدارک شکست خورد ظبط مدارک شکست خورد" + "\n" + ce.Message);
Application.Exit();

}


امیدوارم موفق باشید



************************************************** ******
این فارسی و انگلیسی نوشتن برای خودش مکافاته
من لینک سایت مایکروسافت که پست رو در اونجا گذاشتم رو واستون می نوسیم
همچنین پیستی که ایده اولیه رو از اون گرفتم

https://social.msdn.microsoft.com/Fo...=csharpgeneral

https://social.msdn.microsoft.com/Fo...trol?forum=wpf