What is CreateObject Method in VBA & How to use CreateObject Method in Excel?
Tags: createobjectmethodin, vbamacros
Published: 2021-09-04 01:55:18

---
title: What is CreateObject Method in VBA & How to use CreateObject Method in Excel?
published: true
date: 2021-09-03 14:26:24 UTC
tags: CreateObjectMethodIn,VBAMacros
canonical_url: https://geekexcel.com/what-is-createobject-method-in-vba-how-to-use-createobject-method-in-excel/
---

In this tutorial, we will guide you to learn **What Is CreateObject Method In VBA And How To Use CreateObject Method**  **In Excel**. Let’s get them below!! Get an official version of MS Excel from the following link: **[https://www.microsoft.com/en-in/microsoft-365/excel](https://www.microsoft.com/en-in/microsoft-365/excel)**

## CreateObject Method In VBA

The creation of an object using the CreateObject is called Late Binding. In late binding, the creation of objects happens on the run time. It does not need any references to be added. This makes the VBA code portable.

## CreateObject Function Syntax

```
Set object_name= CreateObject(classname as string,[servername])
```

**Syntax Explanation:**

- **classname as string:** It is a required variable. It is a string that refers to the name of application and object type. The application name and class of the object to be created should be declared in AppName.ObjecType. For example, if I want an object of Word Application, then I would write “Word.Application”. We will see it in detail in examples later.
- **[servername]:** It is an optional variable. It is a string of the name of the network server where the object will be created. If servername is an empty string (“”), the local machine is used.

## Example 1: Open Microsoft Word Application Using Excel VBA

- Firstly, in the Excel Worksheet, you have to go to the  **Developer Tab.**
- Then, you need to ** ** select the  **Visual Basic**  option under the  **Code**  section.

![](https://geekexcel.com/wp-content/uploads/2021/09/Select-Visual-Basic-2-1024x180.png)<figcaption>Select Visual Basics</figcaption>

- Now, you have to  **copy and paste the code**  given below.

```
 Sub OpenWordApp()

    Dim wordApp As New Word.Application

    Dim wordDoc As Document

    wordApp.Visible = True

    wordDoc = wordApp.Documents.Add

End Sub

```

- After that, you need to  **save the code**  by selecting it and then  **close the window**.

![](https://geekexcel.com/wp-content/uploads/2021/09/Save-the-code-3.png)<figcaption>Save the code</figcaption>

- Again, you have to go to the  **Excel Spreadsheet** , and click on the  **Developer Tab**.
- You need to choose the  **Macros option**  in the Code section.

![](https://geekexcel.com/wp-content/uploads/2021/09/Choose-Macro-option-2-1024x184.png)<figcaption>Choose Macro option</figcaption>

- Then, you have to make sure that your  **macro name is selected**  and click the  **Run ** button.

![](https://geekexcel.com/wp-content/uploads/2021/09/Click-on-Run-1.png)<figcaption>Click on the Run option</figcaption>

- Now, you will get **user defined error message** as given below.

![](https://geekexcel.com/wp-content/uploads/2021/09/User-defined-error-1024x703.png)<figcaption>User-defined error</figcaption>

### To avoid this error, use the below code.

- Again, in the Excel Worksheet, you have to go to the  **Developer Tab.**
- Then, you need to ** ** select the  **Visual Basic**  option under the  **Code**  section.
- Now, you have to  **copy and paste the code**  given below.

```
 Sub OpenWordApp()

    Dim wordApp As Object

    Set wordApp = CreateObject("Word.Application")

    Dim wordDoc As Object

    wordApp.Visible = True

    Set wordDoc = wordApp.Documents.Add

End Sub

```

- After that, you need to  **save the code**  by selecting it and then  **close the window**.

![](https://geekexcel.com/wp-content/uploads/2021/09/Avoid-Error-code-1.png)<figcaption>Save the code</figcaption>

- Again, you have to go to the  **Excel Spreadsheet** , and click on the  **Developer Tab**.
- You need to choose the  **Macros option**  in the Code section.
- Now, you have to make sure that your  **macro name is selected**  and click the  **Run ** button.

![](https://geekexcel.com/wp-content/uploads/2021/09/Run-the-code-1.png)<figcaption>Run the code</figcaption>

- Finally, the **Microsoft Word Application** has opened using **Excel VBA**.

![](https://geekexcel.com/wp-content/uploads/2021/09/Open-MS-Word.png)<figcaption>Open MS Word</figcaption>

## Example 2: Create Workbook Object Using CreateObject Function

- Firstly, in the Excel Worksheet, you have to go to the  **Developer Tab.**
- Then, you need to ** ** select the  **Visual Basic**  option under the  **Code**  section.

![](https://geekexcel.com/wp-content/uploads/2021/09/Select-Visual-Basic-3-1024x180.png)<figcaption>Select Visual Basic</figcaption>

- Now, you have to  **copy and paste the code**  given below.

```
 Sub addSheet()

    ' Declare an object variable to hold the object

    ' reference. Dim as Object causes late binding.

    Dim ExcelSheet As Object

    Set ExcelSheet = CreateObject("Excel.Sheet")

    ' Make Excel visible through the Application object.

    ExcelSheet.Application.Visible = True

    ' Place some text in the first cell of the sheet.

    ExcelSheet.Application.Cells(1, 1).Value = "This is column A, row 1"

    ' Save the sheet to C:\test.xls directory.

    ExcelSheet.SaveAs "C:\TEST.XLS"

    ' Close Excel with the Quit method on the Application object.

    ExcelSheet.Application.Quit

    ' Release the object variable.

    Set ExcelSheet = Nothing

End Sub

```

- After that, you need to  **save the code**  by selecting it and then  **close the window**.

![](https://geekexcel.com/wp-content/uploads/2021/09/Save-the-Code-4.png)<figcaption>Save the Code</figcaption>

- Again, you have to go to the  **Excel Spreadsheet** , and click on the  **Developer Tab**.
- You need to choose the  **Macros option**  in the Code section.

![](https://geekexcel.com/wp-content/uploads/2021/09/Choose-Macro-option-3-1024x184.png)<figcaption>Choose Macro option</figcaption>

- Now, you have to make sure that your  **macro name is selected**  and click the  **Run ** button.

![](https://geekexcel.com/wp-content/uploads/2021/09/Run-the-Code.png)<figcaption>Run the code</figcaption>

- Finally, you will receive the result in the **Word Document**.

## Advantages of CreateObject to create Object

- The main **advantage of the CreateObject** is that it makes your **code portable**.
- You can **share the code** to anyone without worrying about if they have **added the reference to the object program** using or not.

**The shortcoming of CreateObject:**

- The **shortcomings of the CreateObject method** are:
- You need to **know the structure of the Class** you are going to **use for object creation**.
- Once you **have created the object** , you are **totally dependent on your memory** for the **methods and properties of objects** , as VBA does not provide any intellisense to help you.

## Verdict

In this tutorial, we guided you to know **What Is CreateObject Method In VBA And How To Use CreateObject Method In Excel**. Leave your **queries/suggestions** in the below comment section. Thanks for visiting **[Geek Excel.](https://geekexcel.com/)** Keep Learning!

**Keep Reading:**

- **[What is a Name Box and Its Features in Microsoft Excel 365?](https://geekexcel.com/what-is-a-name-box-and-its-features-in-microsoft-excel-365/)**
- **[Highlight Unique Values in Selection using Macros in Excel 365!!](https://geekexcel.com/highlight-unique-values-in-selection-using-macros-in-excel-365/)**
- **[{ Save As } Shortcut in Microsoft Excel 365!!! – Quick and Easy Shortcut](https://geekexcel.com/save-as-shortcut-in-microsoft-excel-365/)**
- **[Excel Formulas to Count specific word in a range ~ Easy Tutorial!!](https://geekexcel.com/excel-formulas-to-count-specific-words-in-a-range/)**
- **[Excel Formulas to Get the Last Word of a Text String ~ Quick Method!!](https://geekexcel.com/excel-formulas-to-get-the-last-word-of-the-text-string/)**