How to create an AI Cover Letter generator with OpenAI APIs and Word
- Mark Stankevicius
- Mar 9, 2024
- 12 min read
Updated: Jan 5

A Step-by-Step tutorial to automating cover letter writing with ChatGPT
As the job market becomes increasingly competitive, finding ways to stand out is crucial. One effective way to do this is by creating a personalized cover letter highlighting your skills and experiences. However, writing cover letters can be time-consuming and repetitive. This is where the innovation of AI cover letter automation comes into play, streamlining the task and saving valuable time.
Using ChatGPT to write a cover letter in an automated approach using generative Artificial Intelligence (AI) provides a quick and efficient method. With the help of ChatGPT, an AI language model developed by OpenAI, you can create compelling cover letters in Microsoft Word with just a few clicks.
There are various alternatives for building a Cover Letter Generator using generative AI. Two alternatives with their Pros and Cons are the following:
Using a GPT builder from ChatGPT
Pros: No programming knowledge is required. You will use natural language to describe the behavior of the GPT.
Cons: Access to the GPT builder requires a ChatGPT Plus or Enterprise account, and the monthly fee is higher than using the OpenAI APIs.
Using OpenAI APIs from within Microsoft Word
Pros: The cost of the OpenAI APIs is charged by the number of tokens used, and the cost per token is relatively inexpensive for something like a cover letter generator.
Cons: Properly invoking the APIs from within a Word document requires programming knowledge.
In this step-by-step tutorial, using alternative 2, I will guide you through setting up a cover letter AI generator with a Microsoft Word macro (VBA – Visual Basic for Applications macro), invoking the OpenAI API, and customizing the generated content to create professional cover letters effortlessly.
If you prefer the non-programming approach of alternative 1 you can find that described in this blog post: How to Build a Custom GPT AI Cover Letter Generator.
If you prefer to use the ChatGPT interface and not set up the automated approach this blog post Using Generative AI Chatbots to Craft Cover Letters: A Step-by-Step Guide provides detailed directions.
Introduction to the OpenAI (ChatGPT) API
The ChatGPT API empowers developers to incorporate the model’s capabilities into their applications, enabling programmatic text generation. Accessing the API is straightforward, allowing for the creation of ChatGPT cover letter prompts that ChatGPT can respond to with precision.
ChatGPT's capabilities are based on its ability to predict the next word or phrase given a prompt. By leveraging this capability, we can provide ChatGPT with a cover letter prompt and let it generate a coherent and personalized response. With a few tweaks, we can transform these responses into professional cover letters tailored to specific job opportunities.
The OpenAI API offers several ways to interact with the model, including the chat/completions API, which suits our cover letter ChatGPT generation needs. By understanding how the OpenAI API works, we can seamlessly integrate the AI capabilities into our Word Macro to create a ChatGPT cover letter generator.
Prerequisites for these Step-by-Step instructions
There are several requirements for you to automate the calling of OpenAI APIs in a Word document. They are the following:
An OpenAI API account: You will need access to the OpenAI APIs to call them directly from the Word Macros, which doesn’t require a ChatGPT Plus account but does necessitate an OpenAI API plan. You can find more details here: https://openai.com/product. The pricing model is quite affordable, as it is based on the number of tokens utilized. Create an account before you begin the steps in this document.
Familiarity with programming with Visual Basic macros in Microsoft Word: The OpenAI API will be called from a macro in the Word document. Therefore, some familiarity with programming would be helpful to ensure you can set up the environment and correctly copy the code for the ChatGPT cover letter generator.
Access to Microsoft Word: I used “Microsoft® Word for Microsoft 365 MSO (Version 2402)” in these step-by-step instructions. Some steps may vary depending on your version, but the general approach will apply.
Macro-enabled Word document: For security reasons, the default Word document type does not allow macros to be invoked. You must use a “Microsoft Word Macro-Enabled Document,” which allows you to create and add macros.
Important Note: Enabling macros in Word documents could introduce malicious code. Some Malware is distributed via VB macros. In addition to the code I have written and provided, I use two other VBA macro libraries: VBA-JSON and VBA-Dictionary. I scanned both libraries with VB malware scanners and found no indication of malicious code.
Step-by-step tutorial on programmatic cover letter generation in Word
Now that we have a basic understanding of automated cover letter writing and the tools involved let's dive into the step-by-step tutorial on how to generate cover letter content programmatically in Word.
In this automated cover letter generator, which leverages resume templates and cover letter examples, I used a resume and a job description as input. Having both as input results in a better overall cover letter since it can generate a cover letter that matches your skills and experience to the requirements of the job posting.
To allow the input for the resume and job description, I had to define a format that the VBA macro would understand and enable the macro to find both inputs. The format that the macro will be looking for is a set of delimiters in the document that indicate where the Resume starts and ends and where the Job description starts and ends.
The basic format is the following:

The VBA code will search and find the Resume start and end markers of “Resume:” and “Resume end”. It will also search and find the Job Description markers of “Job description:” and “Job description end”. Any text between these delimiters will be considered input to the Resume and the Job Description respectively. Replace the text in red with the appropriate input text for the generation. This approach allows you to input your Resume once and effortlessly update the job description for each job application.
Tip: You can add additional skills or experiences not covered in your resume that you want to have considered as part of the cover letter generation. Go ahead and add that additional information before the “Resume end” delimiter. Any text between the start and end delimiters will be used as input for your resume.
Step 1: Setting up Word Macros for calling APIs
We must set up a Word document with macro support to interact with the OpenAI API. A Word Macro is a set of instructions that automates repetitive tasks in Word. In this case, we will use the Macro programmed in Visual Basic to invoke the OpenAI API and retrieve the generated cover letter content.
To set up the Visual Basic Word macro, follow these steps:
1) Open Word and navigate to the "Developer" tab.
2) If you do not see the "Developer" tab in Word, add it to your view. It is not shown by default in the tabs in Word.
3) To add the “Developer” tab, navigate to “File” --> “Options” --> “Customize Ribbon”

4) Check the “Developer” Tab box and click “OK”

5) Now you should see the “developer” tab on your Ribbon

6) The “developer” tab opens the Visual Basic Editor, which allows you to create Visual Basic macros to call OpenAI APIs. The steps to create and invoke the macro in the Word document are below.
With the Developer tab now in place, we're poised to integrate the OpenAI API in the subsequent phases of our setup
Step 2: Setting up the VBA code to Invoke the OpenAI API
1) Open Word and navigate to the "Developer" tab.
2) Click on "Visual Basic” to open the Microsoft Visual Basic for Applications (VBA) editor. This will open the VBA editor and allow you to create the macro needed to call the API.

3) On the top toolbar of the VBA editor select “Insert” --> “Module” to open an empty module creation window. You will want to copy the VBA code shown the the GitHub link below into this empty module window. Copy and paste all the VB code into the new module.
4) Rename the module in the VBA editor window by choosing “View”--> “Properties Window” which will open the properties for this new module. Notice it is initially named “Module1”. In the Properties window, choose a more meaningful name. I have chosen “CoverLetterGen”

5) The “JsonConverter” module is also used in this code and must be inserted and referenced for the macro code to run properly. It is used to parse the response returned from the API. If you do not import this module, you will receive a “Run-time error ‘424’ Object required”. Which indicates that the module cannot be found. The instructions for installing this module can be found on the Readme page of that module's GitHub URL. Import it using “File” --> “Import File” on the VBA editor toolbar.
6) The JsonConverter library also depends on “VBA-dictionary”. I also used the VBA Dictionary and imported it into the VBA editor. Download the zip file and extract it to a local folder. Then, import the “dictionary.cls” file into your VBA project using steps similar to those above. Once you have imported the JsonConverter and the Dictionary file, you will see the following in the VBA editor.

There are a couple of things to note in the VBA macro:
The default model (LLMs - Large Language Models) used in the text generation is “gpt-4o-mini”. You can change this to use other models of your choice. I suggest starting with gpt-4o-mini, and once you have everything set up and working, you can change it to other models that may give you better results in the cover letter generated.
The API called to generate the text is the “chat/completions” API. This API is compatible with a specific list of language models, which changes over time. I recommend checking the OpenAI website to ensure you use a compatible model.
The crucial line of code that instructs OpenAI on the desired behavior one setting the “systemPrompt” variable. This directive is a key part of the ChatGPT cover letter prompt, and I’ve found it to be effective in guiding OpenAI in producing the type of cover letter output I need. While you can modify these instructions, make sure they clearly communicate to OpenAI that both the Resume and Job description are included in the chat input.
There is error handling to ensure the resume and job description text are found. If something is missing in the document and the text delimiters cannot be found, messages will be sent to the Word UI.
Step 3: Setting up the API key to call the OpenAI API
Hang in there; your persistence is about to pay off as you are nearing the final stages before you can create and run the macro. Just a few more steps need to be done, and you will be ready to try it out.
1) Copy your API key from the OpenAI API account site into the code. The Macro will make an API call to the OpenAI endpoint, retrieve the generated content, and insert it into your Word document. Login to your OpenAI API account that you created in the Prereqs step and access the API key tab in the menu or use this API Key link:

2) In the API keys UI, click the “Create new secret key” button to create a new API key for the API call.

3) Once the API key is created, copy the API key into the indicated line of macro code shown below. Carefully replace the placeholder with your unique key to ensure secure access to the OpenAI API from your personal account. Remember, this key is confidential and should not be shared, as it serves as your exclusive access to your account.

Step 4: Running and testing the Macro call to ChatGPT API
1) Now that you have the VBA macro created and using your API key you can test the macro and ensure it is set up properly. Ensure you have copied a resume and job description between the delimiters in the document. If the document is ready, go to the quick access toolbar and click Macros

2) If you do not see the “View Macros” on the quick access toolbar you will need to choose “File” --> “Options” --> “Quick AccessToolbar” and add “Macros”.
3) Once you can view the macros, a window with the macros in your document will open, as indicated below. Select “Run” for the View Macros “GenCoverLetter” (you may have named your macro differently than shown), to run the macro on the currently open document.

4) If the macro call to ChatGPT is successful, a cover letter will be generated and inserted into the Word document after the existing text. I have included a screen shot of a partial example below:

If no cover letter is inserted, check if an error message indicates possible problems. If there are errors, check the steps above to ensure that all actions have been implemented properly or review the Error Resolution section below to help address the problem.
Tip: You can rerun the cover letter generator multiple times and the new text will always be appended to the end of the document. Due to the nature of the generative AI each time you run the generator, it will produce different results. This allows you to run it a few times and select the text you prefer.
Tip: The OpenAI chat/completions request body has a couple of parameters that you can modify to try different outputs to see which you prefer. These are the "temperature" and the "top_p" parameters. A detailed description of these parameters can be found in the OpenAI API documentation for Chat completions API.
Step 5: Customizing and formatting the automated cover letter
While the AI cover letter generator powered by ChatGPT provides a solid foundation, it's important to customize and format it to suit your needs. Here are some tips for optimizing the automated cover letter using ChatGPT:
Add a personal touch: Include a brief introduction at the beginning of the cover letter that describes why this role is important to you and captures your enthusiasm for it.
Personalize the content: Tailor the cover letter to the specific job opportunity by incorporating pertinent skills and experiences that the cover letter generator AI did not include in the generated text.
Edit for clarity and coherence: Review the generated content and make any necessary edits to ensure it flows well and effectively communicates your qualifications.
Format the cover letter: The auto-generated text in this example does not include formatting; therefore, use appropriate headings, bullet points, and font styles to make the cover letter visually appealing and easy to read.
By customizing and formatting the automated cover letter, you can enhance its impact and make it more compelling to potential employers.
Advantages and limitations of automated cover letter writing
Automated cover letter writing offers several advantages, such as saving time and effort, ensuring consistency, and providing a starting point for customization. However, it's important to be aware of its limitations as well.
Advantages:
Time-saving: Automated cover letter writing eliminates the need to start from scratch, allowing you to create cover letters quickly.
Consistency: The OpenAI API can help you ensure that the tone and style of your cover letters remain consistent.
Personalization: Recruiters and hiring managers still look for personalized cover letters, so add your personal touches to the final letter. While the OpenAI API generates the initial content, you should customize it based on your specific skills and experiences.
Limitations:
Lack of human touch: Automated cover letters may lack the personal touch and storytelling elements that can be achieved through manual writing.
Overreliance on AI: Relying solely on automated cover letter writing may result in generic and less impactful cover letters.
Limited context understanding: the OpenAI API may not fully understand the context of a job opportunity, making it important to review and tailor the generated content.
Conclusion and future possibilities for automated cover letter generation
Automated cover letter writing with OpenAI API and Word Macros offers a powerful tool for streamlining the creation process. You can create professional cover letters efficiently and effectively by leveraging the OpenAI API and customizing the generated content.
As AI technology advances, automated cover letter generation possibilities are vast. Future developments may include improved context understanding, enhanced personalization, and integration with other job application platforms.
Automating cover letter creation with the OpenAI API in MS Word allows you to focus on showcasing your skills and experiences while letting AI assist in generating the initial content. By following this step-by-step tutorial, you can harness the power of AI to create compelling cover letters that make a lasting impression on potential employers.
Error resolution
There are a few possible problems you may encounter when running the generator. Below are some error conditions and possible resolutions:
Truncation of the generated text: If the cover letter generated is truncated before it ends properly, it’s likely due to the number of tokens specified in the request. To address this issue, increase the number in the parameter “max_tokens” on the requestData.
Invalid JSON in request: If you encounter the error below, there is a problem with the JSON used as input to the chat/completions request. A possible cause may be that the text extracted from the resume or the job description has hidden ASCII characters. The function VBA “CleanText” removes some hidden ASCII characters such as line feeds, but your text may have other characters causing issues in the JSON string. To determine if this is the cause of the problem, you can display the hidden characters in the document with “File” --> “Options” --> “Display” and then check the “Show all formatting marks”. This will display the formatting in the document. If you have additional formatting characters that are not already handled by the “CleanText” function you will need to add code to remove the additional ASCII characters.

API Key error: If you see the following error, it indicates that you have inserted the incorrect API key or that the API key is missing. Check that the API key is inserted into the proper location as indicated in Step 3.3 above and that it is the exact key from your OpenAI account. The variable “OpenAI_Api_Key” must contain your API key.

Comentarios