You can try when you do programming with VB.Net and Outlook automation:
- When you start automation code by declaring object variables with a specific object type that represents the top-level object and then declaring any child objects you want to reference.
Dim olApp As Outlook.Application
- You then create an instance of the top-level object by using the Set statement and the New keyword.
Set olApp = New Outlook.Application
Please note: You need to understand that the New keyword can't be used to create a new instance of a child object.
- To create an instance of a child object, use the appropriate method of the parent object along with the Set statement.
- At the end of the procedure, each object variable is destroyed by explicitly setting it equal to the Nothing keyword. If you do this, there is no need to use a Set statement to instantiate the object. However, this technique is not recommended because you have no control over when the object variable is created.
- In detail, if your code must test to see if an object exists by using a statement such as
If olApp Is Nothing Then, this test will return True if you have created an instance of the object in the Dim statement. Additionally, you might not need to use an object except at the user's request.
- If you create an instance of the object by using New in the Dim statement, the object will be created even if it isn't used. To maintain control over when an object is created, don't use the New keyword in the Dim statement, and instantiate the object by using a Set statement at the point in your code where you must use the object.
Set MailMessage = olApp.CreateItem(olMailItem)