DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • How To Check Office Files for Macros Using Java
  • From Database Changes to Real-Time Actions at Scale: Oracle GoldenGate and Java
  • Java 27 Makes G1 the Default Everywhere: How to Test Your Small Containers Before You Upgrade
  • Stop Writing Database Migration Scripts: Declarative Schema Sync for Java

Trending

  • Building a Grounded Search Agent With Foundry, Azure AI Search, and Cosmos DB, Containerized With Docker and Observed With Grafana
  • Evaluating RAG in Production — What the Numbers Actually Revealed
  • A Reference Architecture for Constrained AI-Assisted Design
  • Post-Quantum TLS for Cloud APIs and Microservices

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Java
  4. How to Get Macro Information From Word and Excel in Java

How to Get Macro Information From Word and Excel in Java

Retrieve information about the macros defined in a Microsoft Word or Excel document.

By 
Brian O'Neill
DZone Core CORE ·
Apr. 11, 21 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
6.1K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

According to the Microsoft website, a macro is a series of commands that you can use to automate a repeated task and can be run when you must perform the task. The use of macro instructions was originally initiated for assembly language programming to perform two main purposes: to reduce the amount of coding that had to be written by generating several assembly language statements from one macro instruction, and to enforce program writing standards.

Nowadays, macros are used for a wide variety of purposes and industries; Microsoft applications currently use Visual Basic for Applications (VBA) programming language to build their macros, and they allow you to choose whether you want to enable or disable existing macros from use. However, 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. These infected macros could potentially run arbitrary commands or delete files on your hard drive, wreaking havoc within your computer.

To assist with the protection of your documents, the following APIs can be used in Java to identify macro information in Word or Excel files and let you decide if the commands are safe to enable.

Before we start on either of the APIs, we will need to install the Maven SDK by adding a reference to the repository:

Java
 




x


 
1
<repositories>
2
    <repository>
3
        <id>jitpack.io</id>
4
        <url>https://jitpack.io</url>
5
    </repository>
6
</repositories>



And add a reference to the dependency:

Java
 




xxxxxxxxxx
1


 
1
<dependencies>
2
<dependency>
3
    <groupId>com.github.Cloudmersive</groupId>
4
    <artifactId>Cloudmersive.APIClient.Java</artifactId>
5
    <version>v3.90</version>
6
</dependency>
7
</dependencies>



Now, we are ready to begin with our first API, which will return VBA information from a DOCX/DOCM document. The only inputs you need to provide for the following code are the file itself and the API key (this can be retrieved for free on the Cloudmersive website if you don’t have one already).

Java
 




xxxxxxxxxx
1
25


 
1
// Import classes:
2
//import com.cloudmersive.client.invoker.ApiClient;
3
//import com.cloudmersive.client.invoker.ApiException;
4
//import com.cloudmersive.client.invoker.Configuration;
5
//import com.cloudmersive.client.invoker.auth.*;
6
//import com.cloudmersive.client.EditDocumentApi;
7

          
8
ApiClient defaultClient = Configuration.getDefaultApiClient();
9

          
10
// Configure API key authorization: Apikey
11
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
12
Apikey.setApiKey("YOUR API KEY");
13
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
14
//Apikey.setApiKeyPrefix("Token");
15

          
16
EditDocumentApi apiInstance = new EditDocumentApi();
17
File inputFile = new File("/path/to/inputfile"); // File | Input file to perform the operation on.
18
try {
19
    GetMacrosResponse result = apiInstance.editDocumentDocxGetMacroInformation(inputFile);
20
    System.out.println(result);
21
} catch (ApiException e) {
22
    System.err.println("Exception when calling EditDocumentApi#editDocumentDocxGetMacroInformation");
23
    e.printStackTrace();
24
}



The result will let you know if the operation was successful, as well as whether or not the document contains VBA macros. To retrieve macro information from an Excel document, it is a similar process and result, with a different function call:

Java
 




xxxxxxxxxx
1
25


 
1
// Import classes:
2
//import com.cloudmersive.client.invoker.ApiClient;
3
//import com.cloudmersive.client.invoker.ApiException;
4
//import com.cloudmersive.client.invoker.Configuration;
5
//import com.cloudmersive.client.invoker.auth.*;
6
//import com.cloudmersive.client.EditDocumentApi;
7

          
8
ApiClient defaultClient = Configuration.getDefaultApiClient();
9

          
10
// Configure API key authorization: Apikey
11
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
12
Apikey.setApiKey("YOUR API KEY");
13
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
14
//Apikey.setApiKeyPrefix("Token");
15

          
16
EditDocumentApi apiInstance = new EditDocumentApi();
17
File inputFile = new File("/path/to/inputfile"); // File | Input file to perform the operation on.
18
try {
19
    GetMacrosResponse result = apiInstance.editDocumentXlsxGetMacroInformation(inputFile);
20
    System.out.println(result);
21
} catch (ApiException e) {
22
    System.err.println("Exception when calling EditDocumentApi#editDocumentXlsxGetMacroInformation");
23
    e.printStackTrace();
24
}



With the returned result, you can eliminate any worries that a macro may be hiding in a document without your knowledge. If the macros detected are safe, you can then choose to enable macros within the file.

Macro (computer science) Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • How To Check Office Files for Macros Using Java
  • From Database Changes to Real-Time Actions at Scale: Oracle GoldenGate and Java
  • Java 27 Makes G1 the Default Everywhere: How to Test Your Small Containers Before You Upgrade
  • Stop Writing Database Migration Scripts: Declarative Schema Sync for Java

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook