ExcelTip.com
ExcelTip.com
Account Icon Account Icon Account Icon
Google Exceltip.com
JOIN OUR NEWSLETTER
  and receive for joining:
Free eBook Learn More!
Free Excel ADD-IN
Free Weekly Excel Tip
4 e-books in cd-rom
F1 Formulas & Functions
F1 Formulas & Functions
F1 EXCEL
F1 eBook (Spanish)
F1 EXCEL
Financial Statements.xls
 

» Basic information about OLE automation using VBA in Microsoft Excel
VBA macro tip contributed by Erlandsen Data Consulting offering Microsoft Excel Application development, template customization, support and training solutions
When you want to use functionality from other applications you have to decide if you want to use
early or late binding of object variables.

Early binding

The binding between the object variable and the object takes place when the application is compiled.
This results in better performance compared to when the binding takes place when the application is run (late binding).
If you want to create an early binding you have to set a reference to the "foreign" object library you want to use.
This is done from the VBE by using the menu Tools, References.... When a VBProject has a reference to an
object library you can declare specific object variables (e.g. Dim oDoc As Word.Document). This will also make it
easier to program the "foreign-objects" since the VBE will display the same programming help regarding properties,
methods and events that it displays for the objects belonging to the application you are working
from (the VBE has automatically added the reference to this application in advance).
This is a general code example:
Sub OLEAutomationEarlyBinding()
' replace xxx with one of the following:
' Access, Excel, Outlook, PowerPoint or Word

Dim oApp As xxx.Application ' early binding
Dim oDoc As xxx.Document 
' Excel.Workbook, Outlook.MailItem, PowerPoint.Presentation, Word.Document
    On Error Resume Next ' ignore errors
    Set oApp = GetObject(, "xxx.Application") 
    ' reference an existing application instance
    If oApp Is Nothing Then ' no existing application is running
        Set oApp = New xxx.Application ' create a new application instance
    End If
    On Error GoTo 0 ' resume normal error handling
    If oApp Is Nothing Then ' not able to create the application
        MsgBox "The application is not available!", vbExclamation
    End If
    With oApp
        .Visible = True ' make the application object visible
        ' at this point the application is visible
        ' do something depending on the application...
        Set oDoc = .Documents.Open("c:\foldername\filename.doc")
        ' open a document
'        ...
        oDoc.Close True ' close and save the document
        .Quit ' close the application
    End With
    Set oDoc = Nothing ' free memory
    Set oApp = Nothing ' free memory
End Sub
Late binding

The binding between the object variable and the object takes place when the application is run.
This results in slower performance compared to when the binding takes place when the application is compiled (early binding).
If you don't add a reference to the objectlibrary belonging to the "foreign" application you have to
declare general object variables (e.g. Dim oDoc As Object). This will make it more difficult to program
the "foreign-objects" since the VBE will not display the same programming help regarding properties,
methods and events that it displays for the objects belonging to the application you are working from.
This is a general code example:
Sub OLEAutomationLateBinding()
' replace xxx with one of the following:
' Access, Excel, Outlook, PowerPoint or Word

Dim oApp As Object ' late binding
Dim oDoc As Object ' late binding
    On Error Resume Next ' ignore errors
    Set oApp = GetObject(, "xxx.Application") 
    ' reference an existing application instance
    If oApp Is Nothing Then ' no existing application is running
        Set oApp = CreateObject("xxx.Application") 
        ' create a new application instance
    End If
    On Error GoTo 0 ' resume normal error handling
    If oApp Is Nothing Then ' not able to create the application
        MsgBox "The application is not available!", vbExclamation
    End If
    With oApp
        .Visible = True ' make the application object visible
        ' at this point the application is visible
        ' do something depending on the application...
        Set oDoc = .Documents.Open("c:\foldername\filename.doc") 
        ' open a document
'        ...
        oDoc.Close True ' close and save the document
        .Quit ' close the application
    End With
    Set oDoc = Nothing ' free memory
    Set oApp = Nothing ' free memory
End Sub


Rate this tip
12 34 5
  RATING: 3.22
  VIEWS: 29462

READER COMMENTS (view all comments)


No comments have been submitted.


REGISTERED USERS - Click here to post comments


GUESTS (Click here to register)
Name
Comment Title
Comments

Financial Statement Analysis with S&P insert card

Microsoft Word Version 2002 Inside Out

Your First Business Plan: A Simple Question and Answer Format Designed to Help You Write Your Own Plan (3rd Ed)

Millionaire Real Estate Mentor : The Secrets of Financial Freedom through Real Estate Investing

The McGraw-Hill Guide to Writing a High-Impact Business Plan: A Proven Blueprint for First-Time Entrepreneurs

Positioning: The Battle for Your Mind

RELATED MICROSOFT EXCEL TIPS


Convert PDF Files to Excel






Excel VBA books
Accounting books
Business Plans
MS Office books
Taxes books

VIEW ALL BOOKS


  Advertise With Us                               

Tips

Add-In in VBA | Applications - Word, Outlook in VBA | Array Formulas | Cells, Ranges, Rows, and Columns in VBA | Counting | Custom Functions | Custom Functions in VBA | Database Formulas | Database in VBA | Date & Time Formulas | Date & Time in VBA | Events in VBA | Excel 2003 | Excel Chart | Excel Consolidating | Excel Counting | Excel Custom Functions using VBA | Excel Customizing | Excel Data | Excel Dates | Excel Editing | Excel Files | Excel Filter | Excel Format | Excel Formula | Excel General | Excel Grouping and Outlining | Excel Importing Text Files | Excel Information | Excel Keyboard Shortcuts | Excel Loan Formulas | Excel Macros - VBA | Excel Pivot Tables | Excel Printing | Excel Range Name | Excel Security - Protection | Excel Sorting | Excel Style | Excel Subtotals | Excel Summing | Excel Text | Excel Time | Excel Tools | Excel Worksheet, Workbook | Files, Workbook, and Worksheets in VBA | Financial Formulas | Formating in VBA | General Topics in VBA | Import and Export in VBA | Information Formulas | Keyboard & Other Shortcuts in VBA | Keyboard Formula Shortcuts | Links between Worksheet and Workbooks | Links in VBA | Logical Formulas | Lookup Formulas | Mail - Send and Receive in VBA | Menus, Toolbars, Status bar in VBA | Modules, Class Modules in VBA | Other Q&A Formulas | Printing in VBA | Protecting in VBA | Summing | Text Formulas | User Forms, Input boxes in VBA | Using Loops | Working with Formulas |

Tips by Version

Microsoft Excel 97 | Microsoft Excel 2000 | Microsoft Excel 2002 | All Microsoft Excel Versions | New in Excel 2002 | New in Excel 2003 - Office 11

Website

Home | Tip of Hour | Recommended Tips | Most Viewed Tips | Tips by Version | Submit a Tip | My Tips
Microsoft Excel Tutorials | Excel Links | Write for Us | About Us | Search Results | Tip Archives | Excel Forum | Excel Forum Archives

Excel Book

Excel 97 Book | Excel 2000 Book | Excel 2002 Book | Excel XP Book | Book Store

Terms and Conditions of use
The applications/code on this site are distributed as is and without warranties or liability. In no event shall the owner of the copyrights, or the authors of the applications/code be liable for any loss of profit, any problems or any damage resulting from the use or evaluation of the applications/code.

Copyright © 2003 ExcelTip.com
Microsoft, Microsoft Excel is a U.S. registered trademark of Microsoft Corporation
Site Developed By: Varien