最后四句在C#里相当于这两句 (Dispose 里会自己调用Close方法)m_WordDoc.Dispose();m_WordApp.Dispose();
codeproject上有很多操作word的C#代码:
顺便给你几个链接:
http://www.codeproject.com/KB/office/Word_Automation.aspx
http://www.codeproject.com/KB/office/Word2007Automation.aspx
http://www.codeproject.com/KB/office/csautomateword.aspx
private void met_word_automation()
{
try
{
// Declaring the object variables we will need later
object varFileName = "c:\\temp\\doc.docx";
object varFalseValue = false;
object varTrueValue = true;
object varMissing = Type.Missing;
string varText;
// Create a reference to Microsoft Word application
Microsoft.Office.Interop.Word.Application varWord =
new Microsoft.Office.Interop.Word.Application();
// Creates a reference to a Word document
Microsoft.Office.Interop.Word.Document varDoc =
varWord.Documents.Open(ref varFileName, ref varMissing,
ref varFalseValue,
ref varMissing, ref varMissing, ref varMissing, ref varMissing,
ref varMissing, ref varMissing, ref varMissing,
ref varMissing, ref varMissing, ref varMissing, ref varMissing,
ref varMissing, ref varMissing);
// Activate the document
varDoc.Activate();
// Change the 1st sentence of the document
varText = "Altered sentence nr. 1";
varDoc.Sentences[0].Text = varText;
// Change the 3rd sentence of the document
varText = "Altered sentence nr. 3";
varDoc.Sentences[2].Text = varText;
// Save the document
varDoc.Save();
// Show the Microsoft Word window with our document on it
varWord.Visible = true;
// Call the print dialog in Word
Microsoft.Office.Interop.Word.Dialog varDlg =
varWord.Application.Dialogs[
Microsoft.Office.Interop.Word.WdWordDialog.wdDialogFilePrint];
varDlg.Show(ref varMissing);
// Print the document
varDoc.PrintOut(ref varTrueValue, ref varFalseValue, ref varMissing,
ref varMissing, ref varMissing, ref varMissing,
ref varMissing, ref varMissing, ref varMissing, ref varMissing,
ref varFalseValue, ref varMissing, ref varMissing,
ref varMissing, ref varMissing, ref varMissing, ref varMissing,
ref varMissing);
// Send mail with this document as an attachment
varDoc.SendMail();
}
catch (Exception varE)
{
MessageBox.Show("Error:\
" + varE.Message, "Error message");
}
}
You need to create references to this .NET namespace in order to use the above code:
Microsoft.Office.Interop.Word