Working with Macros in Word document
29 Nov 20243 minutes to read
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.
NOTE
Refer to the appropriate tabs in the code snippets section: C# [Cross-platform] for ASP.NET Core, Blazor, Xamarin, UWP, .NET MAUI, and WinUI; C# [Windows-specific] for WinForms and WPF; VB.NET [Windows-specific] for VB.NET applications.
using (FileStream fileStream = new FileStream("Template.dotm", FileMode.Open, FileAccess.ReadWrite))
{
//Opens the template document.
using (WordDocument document = new WordDocument(fileStream, FormatType.Dotm))
{
//Creates file stream.
using (MemoryStream stream = new MemoryStream();)
{
//Saves the Word document to stream.
document.Save(stream, FormatType.Word2013Docm);
}
}
}
}
//Loads the macro-enabled template
WordDocument document = new WordDocument("Template.dotm");
//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")
'Saves and closes the document
document.Save("Sample.docm", FormatType.Word2013Docm)
document.Close()
You can download a complete working sample from GitHub.
The following code example illustrates how to remove the macros present in the document by using RemoveMacros method.
//Loads the document with macros
FileStream fileStreamPath = new FileStream("Template.docm", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
WordDocument document = new WordDocument(fileStreamPath, FormatType.Docx);
//Checks whether the document has macros and then removes them
if (document.HasMacros)
document.RemoveMacros();
//Saves the Word document to MemoryStream
MemoryStream stream = new MemoryStream();
document.Save(stream, FormatType.Docx);
//Closes the document
document.Close();
stream.Position = 0;
//Download Word document in the browser
return File(stream, "application/msword", "Sample.docx");
//Loads the document with macros
WordDocument document = new WordDocument("Template.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("Template.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()
You can download a complete working sample from GitHub.
Online Demo
- Explore how to preserve the macros in macro-enabled documents using the .NET Word Library (DocIO) in a live demo here.