NET tutorials: Microsoft Office automation with Step
This tutorial demonstrates how to automate interaction with Microsoft Office applications using the Office Interop Assembly.
![Illustration for NET tutorials: Microsoft Office automation with Step](/hugo/resources/tutorials/net-tutorials-microsoft-office-automation-with-step.jpg)
This tutorial demonstrates how to automate interaction with Microsoft Office applications using the Office Interop Assembly provided by Microsoft. This example is extracted from our samples repository available on GitHub.
Visual Studio settings
First, you need to configure a new C# project as described in the .NET plugin page. The main points are to
- Choose the proper .NET version, .NET Framework 5.0
- Install the StepApiFunction, StepApiKeyword, and StepApiReporting nuget packages
Next, you will need to install the Microsoft.Office.Interop.Outlook and the NUint packages. Your dependencies should look like the image below:
![](0.png)
Code
The following code defines three Keywords allowing for:
- Start and display Outlook (StartOutlook)
- Send an email (SendEmail) with the input “subject” as a subject
- Read all the unread emails containing the input “search” in the subject (ReadEmails) and flag them with the Blue category
public class Keywords : AbstractKeyword
{
[Keyword]
public void StartOutlook()
{
if (Process.GetProcessesByName("OUTLOOK").Count() == 0)
{
Type outlookType = Type.GetTypeFromProgID("Outlook.Application");
Outlook.Application outlook = (Outlook.Application)Activator.CreateInstance(outlookType);
Outlook.MAPIFolder inbox =
outlook.GetNamespace("MAPI").GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
inbox.Display();
}
}
bool received = false;
private void MailReceived()
{
received = true;
}
[Keyword]
public void SendEmail()
{
Outlook.Application app = Marshal.GetActiveObject("Outlook.Application") as Outlook.Application;
app.NewMail += new Outlook.ApplicationEvents_11_NewMailEventHandler(MailReceived);
received = false;
try
{
Outlook.MailItem mail = app.CreateItem(Outlook.OlItemType.olMailItem);
mail.Display();
mail.To = "email@example.com";
mail.Subject = input["subject"].ToString();
mail.Body = "This is a test";
mail.Send();
while (!received) Thread.Sleep(500);
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
[Keyword]
public void ReadEmails()
{
string search = input["search"].ToString();
Type outlookType = Type.GetTypeFromProgID("Outlook.Application");
Outlook.Application outlook = (Outlook.Application)Activator.CreateInstance(outlookType);
Outlook.MAPIFolder inbox =
outlook.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
foreach (Outlook.MailItem item in inbox.Items.Restrict("[Unread]=true").OfType().
Where(m=>m.Subject.Contains(search)).OrderByDescending(m => m.CreationTime))
{
item.Display();
item.UnRead = false;
item.FlagIcon = Outlook.OlFlagIcon.olBlueFlagIcon;
item.Categories = "Blue Category";
item.Close(Outlook.OlInspectorClose.olSave);
}
}
}
Unit Test
If you added the ScriptDev package to your project, you may copy this example to test your Keyword in local. Executing it will send, read, and flag three emails:
public class AutoItKeywordsTests
{
ScriptRunner runner;
[SetUp]
public void Init()
{
runner = new ScriptRunner(typeof(Keywords));
}
[TearDown]
public void tearDown()
{
runner.close();
}
[TestCase()]
public void SendEmailTest()
{
output = runner.run("StartOutlook");
if (output.error != null)
Console.WriteLine(output.error.msg);
output = runner.run("SendEmail", "{subject:'This is a test - email 1'}");
if (output.error != null)
Console.WriteLine(output.error.msg);
output = runner.run("SendEmail", "{subject:'This is a test - email 2'}");
if (output.error != null)
Console.WriteLine(output.error.msg);
output = runner.run("SendEmail", "{subject:'This is a test - email 3'}");
if (output.error != null)
Console.WriteLine(output.error.msg);
output = runner.run("ReadEmails", "{search:'This is a test'}");
if (output.error != null)
Console.WriteLine(output.error.msg);
}
}
Keyword configuration
The Office Interop framework does not need any dependencies, so you may upload your Keywords without any dependencies or with just the .pdb of the DLL:
![](1.png)
Note, you will have to add the nuint.framework.dll if you added the unit test to your Keywords. This is due to the unit test we added.
Summary: This tutorial demonstrates how to automate interaction with Microsoft Office applications using the Office Interop Assembly provided by Microsoft
![Illustration for Using Step with Grafana](/hugo/resources/tutorials/using-step-with-grafana.jpg)
This article demonstrates how to connect Grafana to data generated by Step.
![Illustration for Setting up system monitoring with a Step agent](/hugo/resources/tutorials/setting-up-system-monitoring-with-a-step-agent.jpg)
This article demonstrates how to set up distributed system monitoring using Keyword executions, and analyze the results as measurements.
![Illustration for JUnit Plan Runner](/hugo/resources/tutorials/junit-plan-runner.jpg)
This article provides documentation for how to integrate JUnit tests into Step.
![Illustration for How to monitor services availability and performance](/hugo/resources/tutorials/how-to-monitor-services-availability-and-performance.jpg)
This tutorial demonstrates how Step can be used to monitor services, availability and performance metrics.
![Illustration for .NET tutorials: AutoIt with Step](/hugo/resources/tutorials/net-tutorials-autoit-with-step.jpg)
This tutorial demonstrates how to utilize the AutoIt C# binding to automate interactions with Windows applications.
![Illustration for Android Testing using Step and Appium](/hugo/resources/tutorials/android-testing-using-step-and-appium.jpg)
This article demonstrates the automation of mobile applications on Android using the Appium framework.
![Illustration for Browser-based automation with Step and Selenium](/hugo/resources/tutorials/browser-based-automation-with-step-and-selenium.jpg)
This article defines three Keywords which will be used in browser-based automation scenarios, using Step and Selenium, as general drivers.
![Illustration for Load Testing with Cypress](/hugo/resources/tutorials/load-testing-with-cypress.png)
This tutorial shows you how to efficiently set up a browser-based load test using existing Cypress tests in the Step automation platform.
![Illustration for Adding and Configuring New Agents](/hugo/resources/tutorials/adding-and-configuring-new-agents.jpg)
In this short tutorial, we show how to quickly implement a simple browser-based load test based on Cypress scripts in Step.
![Illustration for Load Testing with Playwright](/hugo/resources/tutorials/load-testing-with-playwright.png)
This tutorial shows you how to efficiently set up a browser-based load test using existing Playwright tests in the Step automation platform.
![Illustration for Basic Keyword Development](/hugo/resources/tutorials/basic-keyword-development.jpg)
This article explains Keywords in Step and demonstrates how to create simple ones.
![Illustration for Designing functional tests](/hugo/resources/tutorials/designing-functional-tests.jpg)
This tutorial demonstrates the design, execution, and analysis of functional tests using the web interface of Step.
![Illustration for Robotic Process Automation (RPA) with Selenium](/hugo/resources/tutorials/robotic-process-automation-with-selenium.jpg)
This tutorial will demonstrate how to use Step and Selenium to automate various browser tasks.
![Illustration for Robotic Process Automation (RPA) with Cypress](/hugo/resources/tutorials/robotic-process-automation-rpa-with-cypress.jpg)
This tutorial demonstrates how to use Step and Cypress to automate various browser tasks.
![Illustration for Synthetic Monitoring with Selenium](/hugo/resources/tutorials/synthetic-monitoring-with-selenium.png)
This tutorial demonstrates how Selenium automation tests can be turned into full synthetic monitoring using Step.
![Illustration for Load Testing with Cypress](/hugo/resources/tutorials/load-testing-with-cypress.png)
In this tutorial, we'll show you how to easily set up and run a browser-based load test with Step's wizard, using your existing Cypress tests, on the Step automation platform.
![Illustration for Load Testing with Selenium](/hugo/resources/tutorials/load-testing-with-selenium.png)
This tutorial shows you how to efficiently set up a browser-based load test using existing Selenium tests in the Step automation platform.
![Illustration for Synthetic Monitoring with Playwright](/hugo/resources/tutorials/synthetic-monitoring-with-playwright.png)
This tutorial demonstrates how Playwright automation tests can be turned into full synthetic monitoring using Step.
![Illustration for Synthetic Monitoring with Cypress](/hugo/resources/tutorials/synthetic-monitoring-with-cypress.png)
This tutorial demonstrates how Cypress automation tests can be turned into full synthetic monitoring using Step.
![Illustration for Robotic Process Automation (RPA) with Playwright](/hugo/resources/tutorials/robotic-process-automation-rpa-with-playwright.jpg)
This tutorial will demonstrate how to use Step and Playwright to automate various browser tasks.
![Illustration for Load Testing with Playwright](/hugo/resources/tutorials/load-testing-with-playwright.png)
In this tutorial, we'll show you how to easily set up and run a browser-based load test with Step's wizard, using your existing Playwright tests, on the Step automation platform.
![Illustration Cypress devops tutorial](/hugo/resources/tutorials/load-testing-devops-cypress.png)
Learn how to quickly set up continuous browser-based load testing using Cypress tests in your DevOps pipeline
![Illustration grafana devops tutorial](/hugo/resources/tutorials/load-testing-devops-k6.png)
Quickly integrate K6 based load-tests in your DevOps workflow
![Illustration for playwright synthetic monitoring in a devops workflow](/hugo/resources/tutorials/synthetic-monitoring-with-playwright.png)
This tutorial demonstrates how Playwright tests can be reused for synthetic monitoring of a productive environment in a DevOps workflow
![Illustration for playwright synthetic monitoring in a devops workflow](/hugo/resources/tutorials/synthetic-monitoring-with-playwright.png)
This tutorial demonstrates how Playwright tests can be reused for synthetic monitoring of a productive environment in a DevOps workflow
![Illustration for okhttp devops](/hugo/resources/tutorials/load-testing-devops-okhttp.png)
In this tutorial you'll learn how to quickly set up a protocol-based load test with okhttp
![Illustration for playwright devops](/hugo/resources/tutorials/load-testing-devops-playwright.png)
Learn how to set up continuous end-to-end testing across several applications based on Playwright tests in your DevOps pipeline using Step
![Illustration for playwright devops](/hugo/resources/tutorials/load-testing-devops-playwright.png)
Learn how to quickly set up continuous browser-based load testing using Playwright tests in your DevOps pipeline
Want to hear our latest updates about automation?
Don't miss out on our regular blog posts - Subscribe now!