public TestResult DoTest() { Word.Application application = null; DateTime startTime = DateTime.Now; try { application = new Word.Application(); application.DisplayAlerts = WdAlertLevel.wdAlertsNone; Word.Document newDocument = application.Documents.Add(); application.Selection.TypeText("This text is written by NetOffice"); application.Selection.HomeKey(WdUnits.wdLine, WdMovementType.wdExtend); application.Selection.Font.Color = WdColor.wdColorSeaGreen; application.Selection.Font.Bold = 1; application.Selection.Font.Size = 18; newDocument.Close(false); return(new TestResult(true, DateTime.Now.Subtract(startTime), "", null, "")); } catch (Exception exception) { return(new TestResult(false, DateTime.Now.Subtract(startTime), exception.Message, exception, "")); } finally { if (null != application) { application.Quit(WdSaveOptions.wdDoNotSaveChanges); application.Dispose(); } } }
/// <summary> /// Recupera el texto plano de un documento de word /// </summary> /// <returns></returns> private StringBuilder GetTextFromWord(object path) { StringBuilder text = new StringBuilder(); Word.Application word = new Word.Application(); object miss = System.Reflection.Missing.Value; object readOnly = true; Word.Document docs = null; try { docs = word.Documents.Open(path, miss, readOnly, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss); for (int i = 0; i < docs.Paragraphs.Count; i++) { text.Append(docs.Paragraphs[i + 1].Range.Text.ToString()).Append(" "); } } catch (Exception) { } finally { if (docs != null) { docs.Close(false); } word.Quit(false); } return(text); }
public TestResult DoTest() { Word.Application application = null; DateTime startTime = DateTime.Now; try { application = new Word.Application(); application.DisplayAlerts = WdAlertLevel.wdAlertsNone; Word.Document newDocument = application.Documents.Add(); Word.Table table = newDocument.Tables.Add(application.Selection.Range, 3, 2); // insert some text into the cells table.Cell(1, 1).Select(); application.Selection.TypeText("This"); table.Cell(1, 2).Select(); application.Selection.TypeText("table"); table.Cell(2, 1).Select(); application.Selection.TypeText("was"); table.Cell(2, 2).Select(); application.Selection.TypeText("created"); table.Cell(3, 1).Select(); application.Selection.TypeText("by"); table.Cell(3, 2).Select(); application.Selection.TypeText("NetOffice"); newDocument.Close(false); return(new TestResult(true, DateTime.Now.Subtract(startTime), "", null, "")); } catch (Exception exception) { return(new TestResult(false, DateTime.Now.Subtract(startTime), exception.Message, exception, "")); } finally { if (null != application) { application.Quit(WdSaveOptions.wdDoNotSaveChanges); application.Dispose(); } } }
public TestResult DoTest() { Word.Application application = null; DateTime startTime = DateTime.Now; try { application = COMObject.Create <Word.Application>(COMObjectCreateOptions.CreateNewCore); application.DisplayAlerts = WdAlertLevel.wdAlertsNone; application.Visible = true; Word.Document newDocument = application.Documents.Add(); // add new module and insert macro // the option "Trust access to Visual Basic Project" must be set NetOffice.VBIDEApi.CodeModule module = newDocument.VBProject.VBComponents.Add(vbext_ComponentType.vbext_ct_StdModule).CodeModule; // set the modulename module.Name = "NetOfficeTestModule"; //add the macro string codeLines = string.Format("Public Sub NetOfficeTestMacro()\r\n {0}\r\nEnd Sub", "Selection.TypeText (\"This text is written by a automatic created macro with NetOffice...\")"); module.InsertLines(1, codeLines); //start the macro NetOfficeTestModule application.Run("NetOfficeTestModule!NetOfficeTestMacro"); newDocument.Close(false); return(new TestResult(true, DateTime.Now.Subtract(startTime), "", null, "")); } catch (Exception exception) { return(new TestResult(false, DateTime.Now.Subtract(startTime), exception.Message, exception, "")); } finally { if (null != application) { application.Quit(WdSaveOptions.wdDoNotSaveChanges); application.Dispose(); } } }
private void buttonStartExample_Click(object sender, EventArgs e) { // start word and turn off msg boxes Word.Application wordApplication = new Word.Application(); wordApplication.Visible = true; // we register some events. note: the event trigger was called from word, means an other Thread wordApplication.NewDocumentEvent += new NetOffice.WordApi.Application_NewDocumentEventHandler(wordApplication_NewDocumentEvent); wordApplication.DocumentBeforeCloseEvent += new NetOffice.WordApi.Application_DocumentBeforeCloseEventHandler(wordApplication_DocumentBeforeCloseEvent); // add new document and close Word.Document document = wordApplication.Documents.Add(); document.Close(); // close word and dispose reference wordApplication.Quit(); wordApplication.Dispose(); }
public TestResult DoTest() { Word.Application application = null; DateTime startTime = DateTime.Now; try { application = new Word.Application(); application.DisplayAlerts = WdAlertLevel.wdAlertsNone; application.Visible = true; Word.Document newDocument = application.Documents.Add(); application.NewDocumentEvent += new NetOffice.WordApi.Application_NewDocumentEventHandler(wordApplication_NewDocumentEvent); application.DocumentBeforeCloseEvent += new NetOffice.WordApi.Application_DocumentBeforeCloseEventHandler(wordApplication_DocumentBeforeCloseEvent); // add new document and close Word.Document document = application.Documents.Add(); document.Close(false); if (_beforeCloseCalled && _newDocumentCalled) { return(new TestResult(true, DateTime.Now.Subtract(startTime), "", null, "")); } else { return(new TestResult(false, DateTime.Now.Subtract(startTime), string.Format("DocumentBeforeClose:{0}, NewDocument:{1}", _beforeCloseCalled, _newDocumentCalled), null, "")); } } catch (Exception exception) { return(new TestResult(false, DateTime.Now.Subtract(startTime), exception.Message, exception, "")); } finally { if (null != application) { application.Quit(WdSaveOptions.wdDoNotSaveChanges); application.Dispose(); } } }
public TestResult DoTest() { Word.Application application = null; DateTime startTime = DateTime.Now; try { application = COMObject.Create <Word.Application>(COMObjectCreateOptions.CreateNewCore); application.DisplayAlerts = WdAlertLevel.wdAlertsNone; // create simple a csv-file as datasource string fileName = string.Format("{0}\\DataSource.csv", Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)); // if file exists then delete if (File.Exists(fileName)) { File.Delete(fileName); } File.AppendAllText(fileName, string.Format("{0},{1}{2}", "ProjectName", "ProjectLink", Environment.NewLine)); File.AppendAllText(fileName, string.Format("{0},{1}{2}", "NetOffice", "http://netoffice.codeplex.com/", Environment.NewLine)); // add a new document Word.Document newDocument = application.Documents.Add(); // define the document as mailmerge newDocument.MailMerge.MainDocumentType = WdMailMergeMainDocType.wdFormLetters; // open the datasource newDocument.MailMerge.OpenDataSource(fileName); // insert some text and the mailmergefields defined in the datasource application.Selection.TypeText("This test is brought to you by "); newDocument.MailMerge.Fields.Add(application.Selection.Range, "ProjectName"); application.Selection.TypeText(" for more information and examples visit "); newDocument.MailMerge.Fields.Add(application.Selection.Range, "ProjectLink "); application.Selection.TypeText(" or click "); object adress = newDocument.MailMerge.DataSource.DataFields[2].Value; object screenTip = "click Tooltip"; object displayText = "here"; newDocument.Hyperlinks.Add(application.Selection.Range, adress, Type.Missing, screenTip, displayText, Type.Missing); // show the contents of the fields int wdToggle = 9999998; newDocument.MailMerge.ViewMailMergeFieldCodes = wdToggle; //do not show the fieldcodes application.ActiveWindow.View.ShowFieldCodes = false; newDocument.Close(false); return(new TestResult(true, DateTime.Now.Subtract(startTime), "", null, "")); } catch (Exception exception) { return(new TestResult(false, DateTime.Now.Subtract(startTime), exception.Message, exception, "")); } finally { if (null != application) { application.Quit(WdSaveOptions.wdDoNotSaveChanges); application.Dispose(); } } }
public TestResult DoTest() { Word.Application application = null; DateTime startTime = DateTime.Now; try { application = new Word.Application(); application.DisplayAlerts = WdAlertLevel.wdAlertsNone; // add a new document Word.Document newDocument = application.Documents.Add(); // create a new listtemplate Word.ListTemplate template = newDocument.ListTemplates.Add(true, "NetOfficeListTemplate"); //get the predefined listlevels (9) Word.ListLevels levels = template.ListLevels; // customize the first level of the list levels[1].NumberFormat = "%1."; // tab is used to change the level levels[1].TrailingCharacter = WdTrailingCharacter.wdTrailingTab; levels[1].NumberStyle = WdListNumberStyle.wdListNumberStyleArabic; levels[1].NumberPosition = 0; levels[1].Alignment = WdListLevelAlignment.wdListLevelAlignLeft; levels[1].TextPosition = application.CentimetersToPoints(0.63F); levels[1].TabPosition = application.CentimetersToPoints(0.63F); levels[1].ResetOnHigher = 0; levels[1].StartAt = 1; levels[1].LinkedStyle = ""; levels[1].Font.Bold = 1; // customize the second level of the list levels[2].NumberFormat = "%1.%2."; // tab is used to change the level levels[2].TrailingCharacter = WdTrailingCharacter.wdTrailingTab; levels[2].NumberStyle = WdListNumberStyle.wdListNumberStyleArabic; // we want the numbers to appear under the first letter of the higher level levels[2].NumberPosition = application.CentimetersToPoints(0.63F); levels[2].Alignment = WdListLevelAlignment.wdListLevelAlignLeft; // and the text should indent a tab more on the right levels[2].TextPosition = application.CentimetersToPoints(1.4F); levels[2].TabPosition = application.CentimetersToPoints(1.4F); levels[2].ResetOnHigher = 0; levels[2].StartAt = 1; levels[2].LinkedStyle = ""; levels[2].Font.Italic = 1; // apply the defined listtemplate to the selection application.Selection.Range.ListFormat.ApplyListTemplate(template, false, WdListApplyTo.wdListApplyToWholeList, WdDefaultListBehavior.wdWord9ListBehavior); //create a list application.Selection.TypeText("Welcoming"); application.Selection.TypeParagraph(); application.Selection.TypeText("Introduction"); application.Selection.TypeParagraph(); application.Selection.TypeText("Presentation"); application.Selection.TypeParagraph(); // execute the indent so the second level gets activated application.Selection.Range.ListFormat.ListIndent(); application.Selection.TypeText("Top 1"); application.Selection.TypeParagraph(); application.Selection.TypeText("Top 2"); application.Selection.TypeParagraph(); application.Selection.TypeText("Top 3"); application.Selection.TypeParagraph(); // execute the outdent so the first level gets reactivated application.Selection.Range.ListFormat.ListOutdent(); application.Selection.TypeText("Questions & Answers"); newDocument.Close(false); return(new TestResult(true, DateTime.Now.Subtract(startTime), "", null, "")); } catch (Exception exception) { return(new TestResult(false, DateTime.Now.Subtract(startTime), exception.Message, exception, "")); } finally { if (null != application) { application.Quit(WdSaveOptions.wdDoNotSaveChanges); application.Dispose(); } } }