using System.Reflection;
using Microsoft.Office.Interop.Word;
object missing = System.Reflection.Missing.Value;
object Nothing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.Application wa = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document();
wa.Documents.Add(ref missing, ref missing, ref missing, ref missing);
doc = wa.ActiveDocument;
//向word中导入文件,可以把HTML文件导入到一个word中.
wa.Selection.InsertFile(sFileName, ref missing, ref missing, ref missing, ref missing);
object unit;
unit = Microsoft.Office.Interop.Word.WdUnits.wdStory;
wa.Selection.HomeKey(ref unit, ref missing);
//如果文档中有个书签时,可以直接定向到书签,可以在当前位置,插入文本,图片等,在HTML中,书签指<a name="mypic"></a>
object Bookmark = (int)Microsoft.Office.Interop.Word.WdGoToItem.wdGoToBookmark;
object NameBookMark = "mypicImg";
wa.Selection.GoTo(ref Bookmark, ref missing, ref missing, ref NameBookMark);
//要插入的图片文件
wa.Selection.InlineShapes.AddPicture("C:\\webbg.png", ref missing, ref missing, ref missing);
//插入本文
wa.Selection.TypeText("xcxcxcxxcxccxcxccxcx");
//保存的word
//注意,如果是打开一个HTML文档,而由HTML另存为doc文档时,插入的图片会生成ab.file这样的目录,来存储当前图片文件,如果是新建的空word文档,再把HTML导入到word文档中,再插入图片就不会出现ab.file这样的目录,图片会直接保存的word文档ab.doc中
object s = Server.MapPath("abs.doc");
doc.SaveAs(ref s, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
//关闭网页wordDoc2
doc.Close(ref Nothing, ref Nothing, ref Nothing);
if (doc != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(doc);
doc = null;
}
//关闭wordApp
wa.Quit(ref Nothing, ref Nothing, ref Nothing);
if (wa != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(wa);
wa = null;
}
//public static bool PrintEnvelope(string from, string to)
// {
// //
// //string temp = Properties.Resources.EnvelopeTemplate.Replace("«ReturnAddress»", from).Replace("«ReceiverAddress»", to);
// string temp = Properties.Resources.EnvelopeTemplate;
// //string TempPath = System.Windows.Forms.Application.ExecutablePath.ToString().Trim().Substring(0, System.Windows.Forms.Application.ExecutablePath.ToString().Trim().LastIndexOf(@"\\") + 1) + "\\\\Envelope.xml";
// if (File.Exists(TempPath)) File.Delete(TempPath);
// System.IO.FileStream fs = new System.IO.FileStream(TempPath, System.IO.FileMode.CreateNew);
// System.IO.StreamWriter sw = new System.IO.StreamWriter(fs,Encoding.UTF8);
// sw.Write(temp);
// sw.Close();
// object fileName = TempPath;
// object readOnly = false;
// object isVisible = true;
// object missing = System.Reflection.Missing.Value;
// object range = Microsoft.Office.Interop.Word.WdPrintOutRange.wdPrintCurrentPage;
// Microsoft.Office.Interop.Word.Application wordApp;
// //Document d = new DocumentClass();
// try
//{
// object o = Marshal.GetActiveObject("Word.Application");
// wordApp = o as Microsoft.Office.Interop.Word.Application;
// }
// catch
//{
// wordApp = new ApplicationClass();
// }
// Document d = wordApp.Documents.Open(ref fileName, ref missing, ref missing,
//ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
//ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
// //replace
// d.Envelope.ReturnAddress.Text = from;
// d.Envelope.Address.Text = to;
// //object findText="«ReturnAddress»";
// //object replaceWidth=from;
// //wordApp.Selection.Find.Execute(ref findText, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref replaceWidth, ref missing, ref missing, ref missing, ref missing, ref missing);
// //object findText2 = "«ReceiverAddress»";
// //object replaceWidth2 = to;
// //wordApp.Selection.Find.Execute(ref findText2, ref missing, ref missing, ref missing, ref missing, ref missing, ref readOnly, ref missing, ref missing, ref replaceWidth2, ref missing, ref missing, ref missing, ref missing, ref missing);
// //
// d.Activate();
// d.PrintOut(ref missing, ref missing, ref range, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
// d.Close(ref readOnly, ref missing, ref missing);
// File.Delete(TempPath);
// return true;
// }