Get Macro Info from a DOCX File in C#

Cloudmersive
2 min readMar 23, 2021

--

When Microsoft was first designing macro features for the Microsoft Office suite, they weren’t thinking about potential internet security risks. Current versions of the applications do have some security features, but due to the increased presence of predators in cyberspace and the widespread use of online documents, macros that are already present in a file run the risk of having pre-built malicious macros embedded in their code.

The API detailed below will pull information about macros in a DOCX document to ensure there isn’t a threat that could infect your current and/or future documents.

The first action we need to take is install the .NET Framework SDK by running this command:

Install-Package Cloudmersive.APIClient.NET.DocumentAndDataConvert -Version 3.4.2

Next, we will be creating an instance of the API and calling the function with the following code:

using System;
using System.Diagnostics;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Api;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Client;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Model;
namespace Example
{
public class EditDocumentDocxGetMacroInformationExample
{
public void main()
{
// Configure API key authorization: Apikey
Configuration.Default.AddApiKey("Apikey", "YOUR_API_KEY");


var apiInstance = new EditDocumentApi();
var inputFile = new System.IO.FileStream("C:\\temp\\inputfile", System.IO.FileMode.Open); // System.IO.Stream | Input file to perform the operation on.
try
{
// Get macro information from a Word DOCX/DOCM document
GetMacrosResponse result = apiInstance.EditDocumentDocxGetMacroInformation(inputFile);
Debug.WriteLine(result);
}
catch (Exception e)
{
Debug.Print("Exception when calling EditDocumentApi.EditDocumentDocxGetMacroInformation: " + e.Message );
}
}
}
}

The returned response will indicate if any VBA macros are present. Visit the Cloudmersive website to retrieve your free API key and gain access to 800 calls/month across our library of APIs.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet