private void button1_Click(object sender, EventArgs e) { if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text =="") { MessageBox.Show("请输入创建Word文档的名称、路径和内容", "信息提示", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning); } else { //初始化 wordApp = new MSWord.ApplicationClass(); object Nothing = Missing.Value; worddoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing); object format = MSWord.WdSaveFormat.wdFormatDocument; //定义图片文件的路径 string PicPath = textBox3.Text; //向Word文件中插入图片的位置 Object range = worddoc.Paragraphs.Last.Range; //定义插入图片是否为外部连接 Object linktofile = false; Object savedocument = true; //插入图片 worddoc.InlineShapes.AddPicture(PicPath, ref linktofile, ref savedocument, ref range); //保存文档 path = textBox2.Text+"\\" + textBox1.Text; worddoc.SaveAs(ref path, ref format, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing); //关闭worddoc文档对象 worddoc.Close(ref Nothing, ref Nothing, ref Nothing); //关闭wordApp组对象 wordApp.Quit(ref Nothing, ref Nothing, ref Nothing); MessageBox.Show("Word文档被成功创建", "信息提示", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information); } }
WinForm之创建word并操作文档
最新推荐文章于 2025-07-22 13:46:45 发布
本文介绍了一个使用C#实现的简单方法,通过按钮点击事件来创建Word文档,并向文档中插入指定图片。该过程包括了初始化Word应用程序、检查输入参数、插入图片以及保存和关闭文档等步骤。
176

被折叠的 条评论
为什么被折叠?



