+ Reply to Thread
Results 1 to 2 of 2

When do I use the NEW keyword

  1. #1
    Registered User
    Join Date
    02-15-2008
    Posts
    3

    When do I use the NEW keyword

    Hi everyone,
    I'm new to the forum and fairly new to VBA. I am confused as to when and why I should use the NEW keyword. Most example of VBA code on the net show the use of the keyword. However I have written examples without using it and they seem to work. For example this code works:
    ***********************
    'Load Outlook if it's running
    Set OutlookApp = GetObject(, "Outlook.Application")
    If Err <> 0 Then
    'Outlook wasn't running, start it from code
    Set OutlookApp = CreateObject("Outlook.Application")
    VarStarted = True
    End If
    If VARStarted Then
    'If Outlook started from code, then close it
    OutlookApp.Quit
    End If
    ************************

    but, if I look at samples from forums etc they all seem to use the new keyword e..g.
    Set OutlookApp = New Outlook.Application
    Can anyone explain why and which method is preferable
    Many thanks
    Ken

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678
    1. Please edit your post to add code tags. If you don't know how, please consult the forum rules.

    2. Your example is late binding: the object variable isn't set until run time. It's almost always preferable to use early binding, where the variable type is set at compile time. Among other things, it allows Intellisense to work, so that you can see the object methods and properties. The downside is that you need to set a reference to the object's library beforehand (Tools > References, (in this case) Microsoft Outlook xx.x Object Library).

    Here's an example of early binding:
    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1