public void RunExample() { // create simple a csv-file as datasource string fileName = string.Format("{0}\\DataSource.csv", HostApplication.RootDirectory); // 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)); // start word and turn off msg boxes Word.Application wordApplication = new Word.Application(); wordApplication.DisplayAlerts = WdAlertLevel.wdAlertsNone; // create a utils instance, not need for but helpful to keep the lines of code low CommonUtils utils = new CommonUtils(wordApplication); // add a new document Word.Document newDocument = wordApplication.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 wordApplication.Selection.TypeText("This test is brought to you by "); newDocument.MailMerge.Fields.Add(wordApplication.Selection.Range, "ProjectName"); wordApplication.Selection.TypeText(" for more information and examples visit "); newDocument.MailMerge.Fields.Add(wordApplication.Selection.Range, "ProjectLink "); wordApplication.Selection.TypeText(" or click "); object adress = newDocument.MailMerge.DataSource.DataFields[2].Value; object screenTip = "click me if you want!"; object displayText = "here"; newDocument.Hyperlinks.Add(wordApplication.Selection.Range, adress, Missing.Value, screenTip, displayText, Missing.Value); // show the contents of the fields int wdToggle = 9999998; newDocument.MailMerge.ViewMailMergeFieldCodes = wdToggle; //do not show the fieldcodes wordApplication.ActiveWindow.View.ShowFieldCodes = false; // save the document string documentFile = utils.File.Combine(HostApplication.RootDirectory, "Example04", Word.Tools.DocumentFormat.Normal); newDocument.SaveAs(documentFile); // close word and dispose reference wordApplication.Quit(); wordApplication.Dispose(); // show dialog for the user(you!) HostApplication.ShowFinishDialog(null, documentFile); }
public void RunExample() { // start word and turn off msg boxes Word.Application wordApplication = new Word.Application(); wordApplication.DisplayAlerts = WdAlertLevel.wdAlertsNone; // create a utils instance, not need for but helpful to keep the lines of code low CommonUtils utils = new CommonUtils(wordApplication); // 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 documentFile = utils.File.Combine(HostApplication.RootDirectory, "Example01", Word.Tools.DocumentFormat.Normal); newDocument.SaveAs(documentFile); // close word and dispose reference wordApplication.Quit(); wordApplication.Dispose(); // show dialog for the user(you!) HostApplication.ShowFinishDialog(null, documentFile); }
public void RunExample() { // start word and turn off msg boxes Word.Application wordApplication = new Word.Application(); wordApplication.DisplayAlerts = WdAlertLevel.wdAlertsNone; // create a utils instance, not need for but helpful to keep the lines of code low CommonUtils utils = new CommonUtils(wordApplication); // add a new document Word.Document newDocument = wordApplication.Documents.Add(); // add a table Word.Table table = newDocument.Tables.Add(wordApplication.Selection.Range, 3, 2); // insert some text into the cells table.Cell(1, 1).Select(); wordApplication.Selection.TypeText("This"); table.Cell(1, 2).Select(); wordApplication.Selection.TypeText("table"); table.Cell(2, 1).Select(); wordApplication.Selection.TypeText("was"); table.Cell(2, 2).Select(); wordApplication.Selection.TypeText("created"); table.Cell(3, 1).Select(); wordApplication.Selection.TypeText("by"); table.Cell(3, 2).Select(); wordApplication.Selection.TypeText("NetOffice"); // save the document string documentFile = utils.File.Combine(HostApplication.RootDirectory, "Example02", Word.Tools.DocumentFormat.Normal); newDocument.SaveAs(documentFile); // close word and dispose reference wordApplication.Quit(); wordApplication.Dispose(); // show dialog for the user(you!) HostApplication.ShowFinishDialog(null, documentFile); }
public void RunExample() { // start word and turn off msg boxes Word.Application wordApplication = new Word.Application(); wordApplication.DisplayAlerts = WdAlertLevel.wdAlertsNone; wordApplication.Visible = true; // create a utils instance, not need for but helpful to keep the lines of code low CommonUtils utils = new CommonUtils(wordApplication); // add a new document Word.Document newDocument = wordApplication.Documents.Add(); // add new module and insert macro // the option "Trust access to Visual Basic Project" must be set VB.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 wordApplication.Run("NetOfficeTestModule!NetOfficeTestMacro"); // save the document string documentFile = utils.File.Combine(HostApplication.RootDirectory, "Example05", Word.Tools.DocumentFormat.Macros); newDocument.SaveAs(documentFile); if(utils.ApplicationIs2007OrHigher) newDocument.SaveAs(documentFile, WdSaveFormat.wdFormatXMLDocumentMacroEnabled); else newDocument.SaveAs(documentFile); //close word and dispose reference wordApplication.Quit(); wordApplication.Dispose(); // show dialog for the user(you!) HostApplication.ShowFinishDialog(null, documentFile); }
/// <summary> /// Creates an instance of the class /// </summary> /// <param name="owner">owner instance</param> protected internal ApplicationUtils(CommonUtils owner) { if (null == owner) throw new ArgumentNullException("owner"); _owner = owner; }