Working with Macros

Macro is a way to automate the tasks that you perform repeatedly. It is a saved sequence of commands or keyboard strokes that can be recalled with a single command or keyboard stroke.

The following link shows how to create a macro in the Word document.

https://support.office.com/en-in/article/Create-or-run-a-macro-c6b99036-905c-49a6-818a-dfb98b7c3c9c

The following code illustrates how to load and save a macro enabled document.

// Loads the macro-enabled template.

WordDocument document = new WordDocument("Template.dotm");

// Gets the table

DataTable table = GetDataTable();

// Executes Mail Merge with groups.

document.MailMerge.ExecuteGroup(table);

//Saves and closes the document

document.Save("Sample.docm", FormatType.Word2013Docm);

document.Close();
'Loads the macro-enabled template.

Dim document As New WordDocument("Template.dotm")

'Gets the table

Dim table As DataTable = GetDataTable()

'Executes Mail Merge with groups.

document.MailMerge.ExecuteGroup(table)

'Saves and closes the document

document.Save("Sample.docm", FormatType.Word2013Docm)

document.Close()

The following code example illustrates the method used to get the tables from data set.

private DataTable GetDataTable()

{

// List of syncfusion products name.

string[] products = { "DocIO", "PDF", "XlsIO" };

// Adds new Tables to the data set.

DataRow row;

DataTable table = new DataTable();

// Adds fields to the Products table.

table.TableName = "Products";

table.Columns.Add("ProductName");

table.Columns.Add("Binary");

table.Columns.Add("Source");

// Inserts values to the tables.

foreach (string product in products)

{

row = table.NewRow();

row["ProductName"] = string.Concat("Essential ", product);

row["Binary"] = "$895.00";

row["Source"] = "$1,295.00";

table.Rows.Add(row);

}

return table;

}
Private Function GetDataTable() As DataTable

'List of syncfusion products name.

Dim products As String() = {"DocIO", "PDF", "XlsIO"}

'Adds new Tables to the data set.

Dim row As DataRow

Dim table As New DataTable()

'Adds fields to the Products table.

table.TableName = "Products"

table.Columns.Add("ProductName")

table.Columns.Add("Binary")

table.Columns.Add("Source")

'Inserts values to the tables.

For Each product As String In products

row = table.NewRow()

row("ProductName") = String.Concat("Essential ", product)

row("Binary") = "$895.00"

row("Source") = "$1,295.00"

table.Rows.Add(row)

Next

Return table

End Function

The following code example illustrates how to remove the macros present in the document by using RemoveMacros method.

//Loads the document with macros

WordDocument document = new WordDocument("Sample.docm");

//Checks whether the document has macros and then removes them

if (document.HasMacros)

document.RemoveMacros();

//Saves the document

document.Save("Sample.docx", FormatType.Docx);

//Closes the document

document.Close();
'Loads the document with macros

Dim document As New WordDocument("Sample.docm")

'Checks whether the document has macros and then removes them

If document.HasMacros Then

document.RemoveMacros()

End If

'Saves the document

document.Save("Sample.docx", FormatType.Docx)

'Closes the document

document.Close()