namespace PDFTest{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } object path; //聲明文件路徑變量 string wordstr; //聲明word文檔內(nèi)容 MSWord.Application wordApp; //聲明word應(yīng)用程序變量 MSWord.Document worddoc; //聲明word文檔變量 //點(diǎn)擊'創(chuàng)建'按鈕實(shí)現(xiàn)創(chuàng)建word文件 private void button1_Click(object sender, EventArgs e) { if (textBox1.Text == '' || textBox2.Text == '') { MessageBox.Show('請(qǐng)輸入路徑和文檔名信息'); } else { //初始化變量 object Nothing = Missing.Value; //COM調(diào)用時(shí)用于占位 object format = MSWord.WdSaveFormat.wdFormatDocument; //Word文檔的保存格式 wordApp = new MSWord.ApplicationClass(); //聲明一個(gè)wordAPP對(duì)象 worddoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing); //向文檔中寫入內(nèi)容 wordstr = textBox3.Text; worddoc.Paragraphs.Last.Range.Text = wordstr; //保存文檔 path = textBox2.Text + '\\' + textBox1.Text; //設(shè)置文件保存路勁 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); //關(guān)閉文檔 worddoc.Close(ref Nothing, ref Nothing, ref Nothing); //關(guān)閉worddoc文檔對(duì)象 wordApp.Quit(ref Nothing, ref Nothing, ref Nothing); //關(guān)閉wordApp組對(duì)象 MessageBox.Show('文檔創(chuàng)建成功!'); } } }} |
|