Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel VBA CreateObject: How to Use Late Binding

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To create an object in Excel VBA, use the CreateObject function, which allows your code to instantiate a new instance of an external application or library (such as Word, Outlook, or FileSystemObject) without needing to manually set a reference in the Tools menu. This method is known as Late Binding because the specific type of the object is determined at runtime rather than during the compilation of the code.

What You'll Need

  • Microsoft Excel installed on a Windows PC.
  • The Visual Basic for Applications (VBA) editor enabled (Developer tab).
  • The Program ID (ProgID) of the object you wish to create (e.g., "Word.Application" or "Scripting.FileSystemObject").

Step-by-Step

  1. Open the VBA Editor: Open your Excel workbook and press Alt + F11 on your keyboard to launch the Visual Basic Editor.
  2. Insert a New Module: In the Project Explorer on the left, right-click on your workbook name, select Insert, and then click Module. This ensures your code is stored in a standard module rather than a sheet or workbook object.
  3. Declare a Generic Object Variable: Because you are using Late Binding, you cannot declare the variable as a specific type (like Word.Application). Instead, you must declare it as a generic Object. Type the following at the start of your subroutine:
    Dim myApp As Object
    This tells VBA that the variable myApp will hold an object, but the specific kind of object will be defined later.
  4. Instantiate the Object: Use the Set keyword along with the CreateObject function. The Set keyword is mandatory when assigning objects in VBA. For example, to open Microsoft Word, write:
    Set myApp = CreateObject("Word.Application")
    The string inside the parentheses is the ProgID. This is the unique identifier that Windows uses to find the correct application on your system.
  5. Configure the Object Properties: Once the object is created, you can manipulate it. If you created a Word application, you might want to make it visible so you can see what the code is doing:
    myApp.Visible = True
    Note that because you are using Late Binding, you will not see the "IntelliSense" dropdown menu (the list of available properties and methods) as you type. You must know the exact names of the methods you are calling.
  6. Execute Methods: Use the object variable to perform tasks. For instance, to add a new document in Word, you would use:
    myApp.Documents.Add
    You can then interact with the document, insert text, or save the file using the methods provided by that specific application's object model.
  7. Clean Up and Release Memory: It is critical to close the application and release the object from memory once the task is complete. Failing to do so can leave "ghost" processes running in the background, which can slow down your computer or lock files. First, quit the application, then set the object to Nothing:
    myApp.Quit
    Set myApp = Nothing

Common Mistakes

One of the most frequent errors is forgetting the Set keyword. In VBA, simple variables (like Integers or Strings) are assigned using a simple equals sign, but objects require Set. If you write myApp = CreateObject("Word.Application"), VBA will throw an "Object variable or With block variable not set" or a "Type Mismatch" error.

Another common mistake is misspelling the ProgID. The string passed to CreateObject must be exact. For example, using "Word.App" instead of "Word.Application" will result in an "ActiveX component can't create object" error. Always verify the ProgID from official documentation or reliable developer resources.

Developers also frequently forget to close the object. If your code crashes before it reaches the myApp.Quit line, the application (like Word or Outlook) will remain open in the background. You can see these hidden instances by opening the Windows Task Manager and looking for the application under the "Processes" tab.

Lastly, relying on Late Binding for very complex projects can lead to runtime errors that are hard to debug. Since the code isn't checked for correctness until it actually runs, a typo in a method name won't be caught until the program hits that specific line of code.

Tips

To handle the lack of IntelliSense in Late Binding, it is often helpful to use "Early Binding" during the development phase. You can do this by going to Tools > References in the VBA editor and checking the library for the application you are using (e.g., "Microsoft Word 16.0 Object Library"). Once you have written and tested your code with the help of the dropdown menus, you can remove the reference and change your declarations back to As Object. This gives you the best of both worlds: ease of development and the compatibility of Late Binding.

When working with the FileSystemObject (used for manipulating folders and files), remember that you must use CreateObject("Scripting.FileSystemObject"). This is an incredibly powerful tool for renaming files, checking if a folder exists, or reading text files without using the clunky native VBA Dir function.

To make your code more robust, wrap your CreateObject call in a basic error handler. This prevents the entire workbook from crashing if the user does not have the required software installed on their machine. A simple On Error Resume Next followed by a check to see if the object Is Nothing is a practical approach.

Troubleshooting

If you encounter the error "ActiveX component can't create object" (Error 429), the first step is to verify that the software you are trying to automate is actually installed on the computer. CreateObject cannot instantiate an application that doesn't exist. If the software is installed, ensure that the version is compatible with your operating system and that you have the necessary permissions to launch the application via script.

If the code runs but the external application does not appear, check if you have set the .Visible property to True. By default, most objects created via VBA run in the background (headless mode) to improve performance and prevent user interference.

If your Excel workbook becomes sluggish or freezes after running the macro several times, check your Task Manager. If you see multiple instances of the application you were automating, it means your code is not properly closing the objects. Ensure that every CreateObject call is paired with a .Quit or .Close method and a Set object = Nothing statement.

If you receive a "Type Mismatch" error, double-check your variable declarations. Ensure you are using Dim var As Object. If you accidentally declared it as a specific type (like Dim var As Word.Application) but didn't set the proper reference in the Tools menu, the code will fail.

Frequently Asked Questions

What is the difference between CreateObject and New?

CreateObject is used for Late Binding, meaning no reference library is needed and the object is identified at runtime. The New keyword is used for Early Binding, which requires a reference to be set in Tools > References but provides faster execution and IntelliSense support.

Can I use CreateObject for any application?

You can use it for any application or component that exposes a COM (Component Object Model) interface and provides a registered ProgID in the Windows Registry. This includes most Microsoft Office apps and many third-party Windows software packages.

Why is Late Binding preferred for distributing spreadsheets?

Late Binding is preferred because it avoids "Missing Reference" errors. If you use Early Binding and your user has a different version of Office (e.g., you used Office 2019 but they have Office 2016), the macro will crash. Late Binding adapts to whatever version is installed on the user's machine.

Does CreateObject work on Mac?

No, CreateObject relies on the Windows COM architecture, which is not available on macOS. Automating other applications on a Mac requires using AppleScript or the specific Mac-compatible VBA commands provided by Microsoft.

How do I find the ProgID for a specific object?

The easiest way is to search for the application name followed by "ProgID" in a search engine or check the official developer documentation for that software. Common ones include "Excel.Application", "Word.Application", and "Outlook.Application".