.Net tutorials: Microsoft Office automation with step
This tutorial explains how to automate interaction with Microsoft Office applications using the Office Interop Assembly provided by Microsoft. This example is extract from our samples repository available on github.
Visual Studio settings
First of all, you need to configure a new C# project as described in the .Net plugin page. The main points will be for you to:
- Choose the proper .Net version .Net Framework 5.0
- Install the StepApiFunction, StepApiKeyword and StepApiReporting nuget packages
You will then need to install the Microsoft.Office.Interop.Outlook and the NUint packages. You dependencies should then look like:
Code
The following code define three keywords allowing to:
- 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<Outlook.MailItem>().
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 can 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 do not need any dependencies, so you can upload your keywords without any dependencies or with just the .pdb of the DLL:
Note that you will have to add the nuint.framework.dll (due to the unit test we added) if you added the unit tests to your keywords.