Problem With Automating Outlook

I was having a problem, but I am wondering if there is a an alternative to CreateItem...

I should tell you that I am using the 2003.net Standard Edition which may not have all the bells and whistles of the professional...

Now here is what I want to do...

Public Class Form1

Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()

MyBase.New()

'This call is required by the Windows Form Designer.

InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

If disposing Then

If Not (components Is Nothing) Then

components.Dispose()

End If

End If

MyBase.Dispose(disposing)

End Sub

'Required by the Windows Form Designer

Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer

'It can be modified using the Windows Form Designer.

'Do not modify it using the code editor.

Friend WithEvents Button1 As System.Windows.Forms.Button

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

Me.Button1 = New System.Windows.Forms.Button

Me.SuspendLayout()

'

'Button1

'

Me.Button1.Location = New System.Drawing.Point(80, 80)

Me.Button1.Name = "Button1"

Me.Button1.TabIndex = 0

Me.Button1.Text = "Button1"

'

'Form1

'

Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)

Me.ClientSize = New System.Drawing.Size(292, 266)

Me.Controls.Add(Me.Button1)

Me.Name = "Form1"

Me.Text = "Form1"

Me.ResumeLayout(False)

End Sub

#End Region

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

AddContact()

End Sub

Private Sub AddContact()

Dim newContact As Outlook.ContactItem = Me.CreateItem(Outlook. _ ' The problem is in the CreateItem, I getting an a message that CreateItem is not a member of the Form1'

OlItemType.olContactItem)

Try

With newContact

.FirstName = "Jo"

.LastName = "Berry"

.Email1Address = "somebody@example.com"

.CustomerID = "123456"

.PrimaryTelephoneNumber = "(425)555-0111"

.MailingAddressStreet = "123 Main St."

.MailingAddressCity = "Redmond"

.MailingAddressState = "WA"

.Save()

.Display(True)

End With

Catch

MessageBox.Show("The new contact was not saved.")

End Try

End Sub

End Class

" I want to Send E-mail at the click of a button and also add contacts that way as well. I plan on making a program that can do MailMerge but with E-mail through Outlook.

If anyone can make it easy for me to get through this problem, I would appreciate it, just tell me what Extra code I need or what step...I will also try to look into resources that are available if I get the Time to.

Have a Fantastic Day!



Answer this question

Problem With Automating Outlook

  • Scotty12105

    ActiveExplorer is a member of the Outlook application object - not a memeber of Form1... You are using Me.ActiveExplorer which is looking for an ActiveExplorer property on the form - you should be using oOutlook.ActiveExplorer becuase oOutlook is the instance variable for the Outlook application that you created.



  • piknik

    The _ is a line continuation character make sure there are no blank lines between them - sometimes cut/paste inserts additional blanks lines in.

    Almost certain thats you problem - you can actually remove the _ if you want and make a single line

    example

    Dim sentContacts As Outlook.MAPIFolder = Me.ActiveExplorer() _
    .Session.GetDefaultFolder(Outlook _

    .OlDefaultFolders.olFolderContacts)

    becomes

    Dim sentContacts As Outlook.MAPIFolder = Me.ActiveExplorer().Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts)


  • Markus Sch.

    Contains is a .NET 2.0 Framework method and will only work in 2005 and not 2003.    You can use the instr function to achieve the same result

    Example

    This will return Not Found

    Dim strContents As String = "this is a test"
    If InStr(strContents, "id") Then
       MsgBox("found")
    else
       MsgBox("Not found")
    End If

    This will return "Found"

    Dim strContents As String = "this is a test"
    If InStr(strContents, "test") Then
       MsgBox("found")
    else
       MsgBox("Not found")
    End If

     

    Which other error are you actually referring to

     


  • kkarrancsu

    Dim s As String = "this is an example.com"
    If s.Contains("example.com") Then
    msgbox("Found")
    End If

    it is giving the same error, huh, Does it work on vb.net 2003

    I tried it on vb.net 2005 and it works but it wont in 2003 and 2003 is what I am familar with and have on my own PC.


  • anu_ooo

    Okay I did what you said and replaced the Me with oOutlook and here is what it looks like, Now I have No Idea what this error means and what to do about it. Other than that One Line everthing else is working fine.

    Dim sentContacts As Outlook.MAPIFolder = oOutlook.ActiveExplorer.Session.GetDefaultFolder ' The error on this line is: Argument not specified for parameter 'FolderType' of 'Public Function GetDefaultFolder(FolderType As Microsoft.Office.Interop.Outlook.OlDefaultFolders) As Microsoft.Office.Interop.OUtlook.MAPIFolder'.


  • scottsignalscape

    Imports Otl = Microsoft.Office.Interop.Outlook

    Dim oOutlook As New Otl.Application

    Dim newContact As Otl.ContactItem = CType(oOutlook.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olContactItem), Otl.ContactItem)



  • Bazzer

    The error ActiveExplorer is not a member of form1 is telling you that you have a form, of type form1. This code is contained within theis form and ME is referring to the content instance of this form1 (as form1 is a class and not an instance).

    It is looking for a control called ActiveExplorer on this form - which I would guess you either dont have on the form or you have called something else.

    The second error would is a little strange as this error would normally occur if you are using the method on a datatype that doesnt exist. In this case its telling you contains isnt a valid member on the string type - but I know that it is. I'd verify the type of Contact.Email1Address to verify that this is indeed of string type.

    Just humour me and try the following lines of code to ensure they work

    Dim s As String = "this is an example.com"
    If s.Contains("example.com") Then
    msgbox("Found")
    End If


  • Nick Sheng

    It sounds as though you are trying to create an VB solution with has outlook automation.

    Two article which cover VB.NET / Outlook automation are as follows

    http://articles.techrepublic.com.com/5100-3513_11-5850937.html

    http://msdn.microsoft.com/library/default.asp url=/library/en-us/odc_ol2003_ta/html/odc_OLOMwVBNET.asp

    With the documentation regarding the Outlook Object Model here - which you should be able to use to refer to the correct objects for the tasks your trying to achieve.
    http://msdn.microsoft.com/library/default.asp url=/library/en-us/vbaol11/html/outlookvbawelcome_hv01136199.asp

    These should work just fine with 2003.


  • PMBT

    The error says it all: You need to provide an argument to the GetDefaultFolder method and that argument should be of the type Microsoft.Office.Interop.outlook.OlDefaultFolders

    You want the user's default contact folder, so that's what you should specify:

    oOutlook.ActiveExplorer.Session.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderContacts)



  • Leon Mayne

    Thanks For the Code!

    Imports Otl = Microsoft.Office.Interop.Outlook

    Dim oOutlook As New Otl.Application

    Dim newContact As Otl.ContactItem = CType(oOutlook.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olContactItem), Otl.ContactItem)

    It worked and I can add lots of Contacts but I am now having problems with this code I got from the MSDN library Which Should allow me to mail all the Contacts at once:

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

    Dim subjectEmail As String = "Meeting has been rescheduled."

    Dim bodyEmail As String = "Meeting is one hour later."

    Dim sentContacts As Outlook.MAPIFolder = Me.ActiveExplorer() _

    .Session.GetDefaultFolder(Outlook _ ' The error on this line is End of Statement Expected

    .OlDefaultFolders.olFolderContacts) ' The error on this line is End of Statement Expected

    For Each contact As Outlook.ContactItem In sentContacts.Items() ' The error on this line is 'SentContacts' is not declared

    If contact.Email1Address.Contains("example.com") Then

    CreateEmailItem(subjectEmail, contact _ ' The error on this line is Comma, ')', or a valid expression expected.

    .Email1Address, bodyEmail)

    End If

    Next

    End Sub


  • Steve9753

    Thanks again but now I am getting a diffrent Error Message

    here is the code now:

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

    Dim subjectEmail As String = "Meeting has been rescheduled."

    Dim bodyEmail As String = "Meeting is one hour later."

    Dim sentContacts As Outlook.MAPIFolder = Me.ActiveExplorer().Session.GetDefaultFolder ' The Error in this line is: 'ActivateExplorer' is not a member of form1

    For Each contact As Outlook.ContactItem In sentContacts.Items()

    If contact.Email1Address.Contains("example.com") Then ' The Error in this line is: 'Contains' is not a member of 'string'

    CreateEmailItem(subjectEmail, contact.Email1Address, bodyEmail)

    End If

    Next

     End Sub

    Other than thoese two lines  everything else looks like it should work

     


  • Jocker23

    Anways about the other error, do you know how to write it better   You should me that other code on how to write around the CreateItem could that be done for the ActivateExplorer


  • stephane - Montpellier

    Anyone
  • Problem With Automating Outlook