namespace PDFTest{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } object path; //声明文件路径变量 string wordstr; //声明word文档内容 MSWord.Application wordApp; //声明word应用程序变量 MSWord.Document worddoc; //声明word文档变量 //点击'创建'按钮实现创建word文件 private void button1_Click(object sender, EventArgs e) { if (textBox1.Text == '' || textBox2.Text == '') { MessageBox.Show('请输入路径和文档名信息'); } else { //初始化变量 object Nothing = Missing.Value; //COM调用时用于占位 object format = MSWord.WdSaveFormat.wdFormatDocument; //Word文档的保存格式 wordApp = new MSWord.ApplicationClass(); //声明一个wordAPP对象 worddoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing); //向文档中写入内容 wordstr = textBox3.Text; worddoc.Paragraphs.Last.Range.Text = wordstr; //保存文档 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); //关闭文档 worddoc.Close(ref Nothing, ref Nothing, ref Nothing); //关闭worddoc文档对象 wordApp.Quit(ref Nothing, ref Nothing, ref Nothing); //关闭wordApp组对象 MessageBox.Show('文档创建成功!'); } } }}
|