工具-录制新宏 用键盘操作后 宏就会用vb记录你的操作记录 停止宏 ,工具-宏-编辑宏 就能看见你收到的代码了 参考这个vb代码 可以写出C#代码 public int GetWord() { int ReInt = 1; ApplicationClass wordApp = new ApplicationClass(); Document MyDoc = null; object missing = System.Reflection.Missing.Value; object tempName = System.Web.HttpContext.Current.Server.MapPath("") + @"/DownLoadWord/Temp.dot"; // 模板名称,本例中的模板如后面的图 int iCounter = 0; GetRandomDefault(ref iCounter);//获取四位随机数 //update zhangshujun string sFileNewName = DateTime.Now.ToString("yyyyMM") + "//" + DateTime.Now.ToString("yyyyMMddhhmmss") + iCounter.ToString().PadLeft(4, '0'); //获取相对目录 string sURL = "DownLoadWord//" + DateTime.Now.ToString("yyyyMM"); //确定是否有文件目录没有就添加 if (!System.IO.Directory.Exists(System.Web.HttpContext.Current.Server.MapPath(sURL))) { // Try to create the directory. System.IO.DirectoryInfo di = System.IO.Directory.CreateDirectory(System.Web.HttpContext.Current.Server.MapPath(sURL)); } string sMapPath = System.Web.HttpContext.Current.Server.MapPath("") + @"/DownLoadWord/" + sFileNewName + ".doc"; object docName = sMapPath; // 生成的word文件,可以不放在web目录下,当然,实际应用中可能会涉及到动态生成文件名,相信这点难不倒你吧 ^_^ object oEndOfDoc = "//endofdoc"; /* /endofdoc is a predefined bookmark */ //update zhangshujun //添加页眉 //wordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView; //wordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader; //wordApp.ActiveWindow.ActivePane.Selection.InsertAfter("[页眉内容]"); // 生成新文档,这里使用了上面提到的模板,如果不想使用指定的模板,可使用missing,这是会使用默认的模板。关于生成word文件的几个方法,参阅MSDN Microsoft.Office.Interop.Word.Paragraph oPara1; wordApp.Visible = true;//隐藏生成WORD文档的过程 try { MyDoc = wordApp.Documents.Add(ref tempName, ref missing, ref missing, ref missing);//生成WORD } catch (Exception ex) { error += ex.Message.ToString().Trim(); //wordApp.Application.Quit(ref oMissing, ref oMissing, ref oMissing); wordApp.Application.Quit(ref missing, ref missing, ref missing); wordApp = null; MyDoc = null; GC.Collect(); ReInt = 0; } try { MyDoc.Activate(); //wordApp.Selection.Font.Size = 30; // 字体大小 //wordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft; // 居中 //wordApp.Selection.Font.Bold = (int)WdConstants.wdToggle; // 黑体 //wordApp.Selection.TypeText(this.TextBox1.Text); // 文字内容,这里我没有从数据库里面读取,只是测试了一下写入的内容 oPara1 = MyDoc.Content.Paragraphs.Add(ref missing); oPara1.Range.Text = routejourney + "出团行程"; oPara1.Range.Font.Bold = 1; oPara1.Format.SpaceAfter = 6; //24 pt spacing after paragraph. oPara1.Range.Font.Size = 22; oPara1.Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter; oPara1.Range.Font.NameFarEast = "楷体_GB2312"; oPara1.Range.InsertParagraphAfter(); //oPara1.Range.Font.Underline = WdUnderline.wdUnderlineSingle; //加下划线 Microsoft.Office.Interop.Word.Paragraph oPara3; oPara3 = MyDoc.Content.Paragraphs.Add(ref missing); oPara3.Range.Font.Bold = 0; oPara3.Format.SpaceAfter = 6; //24 pt spacing after paragraph. oPara3.Range.Font.Size = float.Parse("10.5"); oPara3.Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft; //object count2 = 2; //object WdLine2 = Word.WdUnits.wdLine;//换一行; //wordApp.Selection.MoveDown(ref WdLine2, ref count2, ref missing);//光标向下移1行 Microsoft.Office.Interop.Word.Paragraph oPara2; oPara2 = MyDoc.Content.Paragraphs.Add(ref missing); if (wordtype == "2") { oPara2.Range.Text = "团号:" + teamnum.Trim() + " 出团日期:" + Convert.ToDateTime(routedata).ToString("yyyy年MM月dd日") + " 行程天数:" + routeday; } else { oPara2.Range.Text = "行程天数:" + routeday; } oPara2.Range.Font.Bold = 0; oPara2.Format.SpaceAfter = 6; //24 pt spacing after paragraph. oPara2.Range.Font.Size = float.Parse("10.5"); oPara2.Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft; oPara2.Range.Font.NameFarEast = "楷体_GB2312"; oPara2.Range.Select(); object unit = Microsoft.Office.Interop.Word.WdUnits.wdLine; //光标移动的参数 行单位 object unit1 = Microsoft.Office.Interop.Word.WdUnits.wdCharacter; //光标移动的参数 字符单位 object extend = Microsoft.Office.Interop.Word.WdMovementType.wdExtend; //光标移动时是否选中 选中用wdExtend 不选中 missing if (wordtype == "2") { object count1 = 3; //光标每次移动的字符数 object count2 = 3 + teamnum.Trim().Length + 4; object count3 = 5; object count4 = 3 + teamnum.Trim().Length + 4 + 5 + 11 + 4; wordApp.Selection.HomeKey(ref unit, ref extend); wordApp.Selection.MoveRight(ref unit1, ref count1, ref extend); wordApp.Selection.Font.Bold = 1; wordApp.Selection.HomeKey(ref unit, ref extend); wordApp.Selection.MoveRight(ref unit1, ref count2, ref missing); wordApp.Selection.MoveRight(ref unit1, ref count3, ref extend); wordApp.Selection.Font.Bold = 1; wordApp.Selection.HomeKey(ref unit, ref missing); wordApp.Selection.MoveRight(ref unit1, ref count4, ref