سلام؛ من ازهمین روش شما استفاده کردم چند وقت پیش به وسیله bookmark:
private Document oDoc;
public void createdoc1()
{
var realpath="~/template";
var filePath = Path.Combine(HttpContext.Current.Server.MapPath("~/template"), Lcourseid.Text + ".doc");
var oWordApplication = new Application();
DirectoryInfo dir = new DirectoryInfo(Server.MapPath(realpath));
foreach (FileInfo files in dir.GetFiles())
{
files.Delete();
}
// To invoke MyMethod with the default argument value, pass
// Missing.Value for the optional parameter.
object missing = System.Reflection.Missing.Value;
//object fileName = ConfigurationManager.AppSettings["DocxPath"];@"C:\DocXExample.docx";
string fileName = @"D:\template1.dot";
//string fileName1 = @"D:\sss.doc";
object newTemplate = false;
object docType = 0;
object isVisible = true;
//System.Reflection.Missing.Value is used here for telling that method to use default parameter values when method execution
oDoc = oWordApplication.Documents.Open(fileName, newTemplate, docType, isVisible, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
// usable in earlier versions of Microsoft Word v2003 v11
// if(Convert.ToInt16(oWordApplication.Version) >=11)
{
//Sets or returns a Boolean that represents whether a document is being viewed in reading layout view.
oDoc.ActiveWindow.View.ReadingLayout = false;
}
//The active window is the window that currently has focus.If there are no windows open, an exception is thrown.
//microsoft.office.tools.word.
oDoc.Activate();
if (oDoc.Bookmarks.Exists("Title"))
{
oDoc.Bookmarks["Title"].Range.Text = "Test Field Entry from webform";
oDoc.Bookmarks["Address"].Range.Text = "Address Field Entry from webform";
}
oDoc.SaveAs(filePath, ref missing);
oWordApplication.Documents.Close(ref missing, ref missing, ref missing);
//oWordApplication.Quit(ref SaveChanges, ref missing, ref missing, ref missing);
ProcessRequest(filePath, Lcourseid.Text);
ولی این روش مشکلاتی هم داره. اول اینکه باید روی سرور تنظیمات خاصی رو انجام بدی. البته از تنظیمات منظور تنظیمات دسترسی کاربران هست. ولی استفاده از داک ایکس بیشتر استقبال میشه چون دردسرش کمتره.