CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Automation

  1. #1
    Join Date
    Jan 2001
    Posts
    73

    Automation

    I'm trying to automate Outlook to be able to retreive all the contacts from it.

    In visual Basic this is quite simple!!
    look :

    Sub GetOutlookContacts()
    Dim ol As Object
    Dim olns As Object
    Dim objFolder As Object
    Dim objAllContacts As Object
    Dim Contact As Object
    ' Set the application object
    Set ol = New Outlook.Application
    ' Set the namespace object
    Set olns = ol.GetNamespace("MAPI")
    ' Set the default Contacts folder
    Set objFolder = olns.GetDefaultFolder(olFolderContacts)
    ' Set objAllContacts = the collection of all contacts
    Set objAllContacts = objFolder.Items
    ' Loop through each contact
    For Each Contact In objAllContacts
    ' Display the Fullname field for the contact
    MsgBox Contact.FullName
    'MsgBox Contact.Email1Address

    Next

    End Sub




    But in Visual C++ with MFC this is quite hard!!

    I've figured how to go into the Contact folder...but i'm not able to move in it...see the code :



    // Start Outlook.
    // If it is already running, you'll use the same instance...
    _Application olApp;
    COleException e;
    if(!olApp.CreateDispatch("Outlook.Application", &e)) {
    CString str;
    str.Format("CreateDispatch() failed w/error 0x%08lx", e.m_sc);
    AfxMessageBox(str, MB_SETFOREGROUND);
    return;
    }

    // Logon. Doesn't hurt if you are already running and logged on...
    _NameSpace olNS(olApp.GetNamespace("MAPI"));
    COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
    olNS.Logon(covOptional, covOptional, covOptional, covOptional);



    //Set the default Contacts Folder


    _ContactItem olItem2(olNS.GetDefaultFolder(10));

    CString testing = olItem2.GetFullName();
    AfxMessageBox(testing,MB_SETFOREGROUND);



    AfxMessageBox("All done.", MB_SETFOREGROUND);
    olNS.Logoff();







    My question is simple, how can I retreive every fullname from the contacts....like the VB code is doing.

    I'm able to create a new contact...


    // Create and open a new contact
    _ContactItem olItem(olApp.CreateItem(2));

    // Setup Contact information...
    olItem.SetFullName("James Smith");
    COleDateTime bdDate;
    bdDate.SetDate(1975, 9, 15);
    olItem.SetBirthday(bdDate);
    olItem.SetCompanyName("Microsoft");
    olItem.SetHomeTelephoneNumber("704-555-8888");
    olItem.SetEmail1Address("someone@microsoft.com");
    olItem.SetJobTitle("Developer");
    olItem.SetHomeAddress("111 Main St.\nCharlotte, NC 28226");

    // Save Contact
    olItem.Save();





    please help

    ViNcE

    Sorry for my bad english...speaking french


  2. #2
    Join Date
    Apr 2001
    Location
    Tampa Florida
    Posts
    233

    Re: Automation


    BOOL COutlookProcessing::OnInitDialog()
    {
    CDialog::OnInitDialog();

    // Start Outlook.
    // If it is already running, you'll use the same instance...

    // _Application m_olApp;
    // _NameSpace m_olNs
    // MAPIFolder m_inbox
    // _Items m_Items

    COleException e;

    if(!m_olApp.CreateDispatch("Outlook.Application", &e))
    {
    CString str;
    str.Format("CreateDispatch() failed w/error 0x%08lx", e.m_sc);
    AfxMessageBox(str, MB_SETFOREGROUND);
    return FALSE;
    }

    // extern the app
    g_olApp = m_olApp;
    // Logon. Doesn't hurt if you are already running and logged on...
    m_olNs = m_olApp.GetNamespace("MAPI");
    COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
    m_olNs.Logon(covOptional, covOptional, covOptional, covOptional);

    // six is the inbox
    m_inbox = (MAPIFolder)m_olNs.GetDefaultFolder ( 6 );

    m_Items = m_inbox.GetItems ();
    m_ItemCount = m_Items.GetCount();
    m_ItemUsing = 1;


    if ( m_ItemCount == 0 )
    {
    MessageBox( " No Email to process, Returning ", " User Notice ", MB_OK );
    return FALSE;
    }

    // initialize the member veriant variable and set it to integer type
    VariantInit( &m_variant );
    m_variant.vt = VT_INT ;
    // get the first email item
    m_MailItem = m_Items.GetFirst ();

    SetUsingString();
    ParseDisplay();

    // Prepare a new mail message
    // _MailItem olMail(olApp.CreateItem(0));
    // olMail.SetTo("someone@microsoft.com");
    // olMail.SetSubject("About our meeting...");
    // olMail.SetBody(
    // "Hi James,\n\n"
    // "\tI'll see you in two minutes for our meeting!\n\n"
    // "Btw: I've added you to my contact list!");

    // Send the message!
    // olMail.Send();

    // AfxMessageBox("All done.", MB_SETFOREGROUND);

    // TODO: Add extra initialization here

    return TRUE; // return TRUE unless you set the focus to a control
    // EXCEPTION: OCX Property Pages should return FALSE
    }





    "trampling out the vintage"

  3. #3
    Join Date
    Jan 2001
    Posts
    73

    Re: Automation

    is It easy to port this code to Microsoft exchange ?


  4. #4
    Join Date
    Apr 2001
    Location
    Tampa Florida
    Posts
    233

    Re: Automation

    All I did was what you asked, show you how to get mail items. MS Exchange implies that you are messing with users. If you are at the level that you can not get mail items from outlook, who would want you to program directly to MS Exchange.? Anyway, I have no idea how to access the exchange.

    "trampling out the vintage"

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured