VOOZH about

URL: https://dzone.com/articles/how-to-get-macro-information-from-word-and-excel-i

⇱ How to Get Macro Information From Word and Excel in Java


Related

  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 Apr. 11, 21 · Tutorial
Likes
Comment
Save
5.9K Views

Join the DZone community and get the full member experience.

Join For Free

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
  • Top Java Security Vulnerabilities and How to Prevent Them in Modern Java
  • A Spring Boot App With Half the Startup Time
  • Implementing the Planning Pattern With Java Enterprise and LangChain4j

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

Let's be friends: