Skip to content

How to Write Office VBA Code with ChatGPT or any AI

ChatGPT, Microsoft Copilot or any AI can write Office VBA code on demand, giving you working macros for Word, Excel, PowerPoint or Outlook in seconds. Both tools take a plain English request and return real VBA. We tested ChatGPT and Copilot with real prompts across all four Office apps, looked closely at where the AI gets things right and how to ask for fixes or improvements.

ChatGPT or Copilot will take a simple request and returns some code. It’s made coding a lot easier and faster than it was only a few years ago.

Still, there are some traps for the unwary and those who’ve swallowed the proverbial AI Kool-Aid.

Over the years we’ve tested AI to see how it copes with making some Microsoft Office VBA. These days AI can do a remarkably good job writing, changing and fixing code.

Here’s some examples, we tried for Word, Excel, PowerPoint and Outlook back in 2023 and now check out the changes — all for the good.

If nothing else, AI gives you a starting point and shows you the methods and properties to explore.  That especially applies to Outlook classic VBA which can seem like an impenetrable mystery to novices (and some experts).

Modern AI does a great job with coding. For those of us who have labored

For all these examples, we’ll use ChatGPT, but Copilot (aka “ChatGPT lite” 🥸), Claude or Gemini should do a very similar job. All those services have free plans which can do coding tasks.

Write Word VBA to open a document

A basic starting point for any Office VBA is selecting a document then opening it. ChatGPT gives a simple way to do that. Ask “Write Word VBA macro to open a document” and it shows some code to do that …

But ChatGPT goes beyond what was asked for, below the direct answer are better responses.

A slightly safer version checks that the file exists first” and “To open a document selected by the user:

Write Excel VBA to choose sheet then open it

This response isn’t exactly what we wanted. ChatGPT offers code to select a worksheet by number not name.

Write Excel VBA to choose sheet then open it, with an error message if no sheet selected

This is where repeated prompts comes in. Follow up the initial response with prompts asking for changes.

Change to work with sheet names not numbers. Show a list of available sheets.

OR

Show a list of available sheets so the user can click/select a sheet from the list.

That last prompt requires a more complicated solution. AI will explain the UserForm with code required as well as the main code.

PowerPoint VBA to insert a new slide

This is another test prompt from 2023 which now gives a much better answer.

PowerPoint VBA to insert a new slide then add today’s date

The AI made code is now clearer, has more options with better comments.

The text box and date formatting can be adjusted.

Outlook VBA to find email

VBA programming is a lot more complicated for Outlook classic, but ChatGPT handles this request well. A great starter for Outlook classic VBA because it includes the, often assumed, declarations and objects needed before anything is done in Outlook.

Unlike the 2023 response, ChatGPT now offers code that only searches emails and starts from the most recent messages.

Outlook VBA to find the most recent email from “???” and open it

All Office Watch articles about Copilot

Excel merge arrays into a single array with VBA

Inside the Office VBA and MOTW changes

More VBA macro blocks coming to Office, details

Microsoft Dials Back Copilot in Windows, Will Microsoft 365 Get the Same Treatment?

Six More Warnings Hidden in Copilot’s Legal Fine Print

Microsoft Calls Copilot Entertainment Only While Selling It as a Productivity Tool

Microsoft Copilot Researcher Gets a Two Brain Upgrade

Copilot Can Now Edit Your Word Documents Directly on iPhone


About this author

Office-Watch.com

Office Watch is the independent source of Microsoft Office news, tips and help since 1996. Don't miss our famous free newsletter.

Office 2024 - all you need to know. Facts & prices for the new Microsoft Office. Do you need it?

Microsoft Office upcoming support end date checklist.