/* create a document write text and save with Word taken from Example01.cs*/private void Example01Main(){ // start word and turn off msg boxes Word.Application wordApplication = new Word.Application(); wordApplication.DisplayAlerts = WdAlertLevel.wdAlertsNone; // add a new document Word.Document newDocument = wordApplication.Documents.Add(); // insert some text wordApplication.Selection.TypeText("This text is written by NetOffice"); wordApplication.Selection.HomeKey(WdUnits.wdLine, WdMovementType.wdExtend); wordApplication.Selection.Font.Color = WdColor.wdColorSeaGreen; wordApplication.Selection.Font.Bold = 1; wordApplication.Selection.Font.Size = 18; // save the document string fileExtension = GetDefaultExtension(wordApplication); object documentFile =
string.Format("{0}\\Example01{1}", Application.StartupPath, fileExtension); newDocument.SaveAs(documentFile); // close word and dispose reference wordApplication.Quit(); wordApplication.Dispose(); FinishDialog fDialog = new FinishDialog("Document saved.", documentFile.ToString()); fDialog.ShowDialog(this);}#region Helper/// <summary>/// returns the valid file extension for the instance. for example ".doc" or ".docx"/// </summary>/// <param name="application">the instance</param>/// <returns>the extension</returns>private static string GetDefaultExtension(Word.Application application){ double version = Convert.ToDouble(application.Version, CultureInfo.InvariantCulture); if (version >= 12.00) return ".docx"; else return ".doc";}#endregion
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請
點(diǎn)擊舉報。