Newbie get_CurrentItem() question

  • Thread starter Thread starter R2FicmllbA
  • Start date Start date
Status
Not open for further replies.
R

R2FicmllbA

Hello,

I am still brand new to ATL/COM add-in development so I assume there is an

obvious answer to this. I'm using VC++ 6 and the add-in is for Outlook 2003.

I am attempting to add the body of the currently selected message to a new

message I create. The new message creation/sending part works fine, but I get

a fatal error every time I use the get_CurrentItem() method. Here's the

relevant code (I got almost all of this from tutorials):

CComPtr <Outlook::_MailItem> mailPtr;

m_spApp->CreateItem(Outlook::olMailItem, (IDispatch**)&mailPtr);

CComPtr<Outlook::_Inspector> spInspector;

hr = m_spApp->ActiveInspector(&spInspector);

if (FAILED(hr)){

MessageBox(NULL,"Error retrieving inspector","Inspector",MB_OK);

return;

}

CComPtr<IDispatch> spCurrentItem;

CComPtr<Outlook::_MailItem> mailPtr2;

spInspector->get_CurrentItem(&spCurrentItem);

Any input is greatly appreciated. Let me know if you need any more info.
 
Hi Gabriel,


> I am attempting to add the body of the currently selected message to a new
> message I create. The new message creation/sending part works fine, but I
> get
> a fatal error every time I use the get_CurrentItem() method. Here's the
> relevant code (I got almost all of this from tutorials):

> CComPtr <Outlook::_MailItem> mailPtr;
> m_spApp->CreateItem(Outlook::olMailItem, (IDispatch**)&mailPtr);


Did you try to create the new item *after* retrieving the current item,

just to make sure the CreateItem does not set a new CurrentItem


> CComPtr<Outlook::_Inspector> spInspector;
> hr = m_spApp->ActiveInspector(&spInspector);


Are you sure that spInspector != NULL?


> CComPtr<IDispatch> spCurrentItem;
> CComPtr<Outlook::_MailItem> mailPtr2;

> spInspector->get_CurrentItem(&spCurrentItem);


Does the call fail or crash?

Can you call any other method on the inspector?

SvenC
 
Hi Sven,

"SvenC" wrote:


> Are you sure that spInspector != NULL?
>


That was it. I erroneously made the assumption that since hr wasn't failing,

spInspector was getting set. Which leads me to my next question. I think I'm

confusing the Inspector object and the Selection object. If I have one or

more messages in my inbox selected, I'd access them with

ActiveExplorer->Selection, correct? I tried this instead (with extra checks

to see where things went awry):

CComPtr <Outlook::_Explorer> spExplorer;

m_spApp->ActiveExplorer(&spExplorer);

if (spExplorer == NULL){

MessageBox(NULL,"Explorer not set","No Explorer",MB_OK);

}

CComPtr <Outlook::Selection> spSelection;

spExplorer->get_Selection(&spSelection);

if (spSelection == NULL){

MessageBox(NULL,"Selection not set","No Selection",MB_OK);

}

VARIANT v;

v.intVal = 1;

CComPtr<IDispatch> mailPtr2;

spSelection->Item(v,&mailPtr2);

if (mailPtr2 == NULL){

MessageBox(NULL,"mailPtr2 not set","No MailPtr2",MB_OK);

}

When run, it displays the "mailPtr2 not set" message box. Thanks for your

help!

-Gabriel
 
Hi Gabriel,


> I think I'm confusing the Inspector object and the Selection object.


Inspector gives you access to a single item shown by Outlook in its

own window.


> If I have one or more messages in my inbox selected, I'd access
> them with ActiveExplorer->Selection, correct?


Yes.


> CComPtr <Outlook::Selection> spSelection;
> spExplorer->get_Selection(&spSelection);

> if (spSelection == NULL){
> MessageBox(NULL,"Selection not set","No Selection",MB_OK);
> }

> VARIANT v;
> v.intVal = 1;


You must set the type of the variant.

v.vt == VT_I4.

Alternatively you can use a variant class like CComVariant

which has assignment overloads which set the type

automatically.

CComVariant v = 1L; // use L to indicate long constant


> CComPtr<IDispatch> mailPtr2;
> spSelection->Item(v,&mailPtr2);


Get the returned HRESULT as well. I expect that you should

get E_INVALIDARG when passing an incorrect VARIANT.

SvenC
 
Thanks again, Sven!

I actually caught the VARIANT issue. The HRESULT returned from

spSelection->Item() doesn't fail and mailPtr2 is set now, however I run into

issues when calling mailPtr2's methods. I DID change the declaration to

CComPtr<Outlook::_MailItem> from CComPtr<IDispatch> because I couldn't figure

out how to convert between the two afterward. Here's what I'm currently

working with:

CComVariant nItem = 1L;

CComPtr<Outlook::_MailItem> mailPtr2;

hr = spSelection->Item(nItem,(IDispatch**)&mailPtr2);

if (FAILED(hr)){

char buf[30];

sprintf(buf,"HResult: %d",hr);

MessageBox(NULL,buf,"HResult",MB_OK);

}

if (mailPtr2 == NULL){

MessageBox(NULL,"mailPtr2 not set","No MailPtr2",MB_OK);

}

OlObjectClass cls;

mailPtr2->get_Class(&cls);

char buf[30];

sprintf(buf, "mailPtr2's class is: %d",cls);

MessageBox(NULL,buf,"Class",MB_OK);

CComPtr <Outlook::_MailItem> mailPtr;

m_spApp->CreateItem(Outlook::olMailItem, (IDispatch**)&mailPtr);

long size = 0;

mailPtr2->get_Size(&size);

sprintf(buf,"mailPtr2 Size: %d",size);

MessageBox(NULL,buf,"Size", MB_OK);

BSTR vSubject = ::SysAllocString(OLESTR("test"));

BSTR vBody = ::SysAllocStringLen(NULL,size);

get_Class() seems to work fine (returns 43, which I looked up in the

ITypeLib Viewer as being olMail). get_Size() doesn't set size though, and

get_Body() crashes Outlook. Any ideas?

-Gabriel
 
Hi Gabriel,


> I DID change the declaration to
> CComPtr<Outlook::_MailItem> from CComPtr<IDispatch> because I couldn't
> figure
> out how to convert between.


Use QueryInterface to "cast" between two COM interfaces:

CComPtr<IDispatch> disp;
> CComVariant nItem = 1L;



> CComPtr<Outlook::_MailItem> mailPtr2;
> hr = spSelection->Item(nItem, &disp);


if(disp != NULL)

hr = disp->QueryInterface(&mailPtr2);

Also be sure to release the smart pointers before

you use their address operator to assign a new

object to them. So if you did another:

hr = spSelection->Item(nItem, &disp);

you would leak the first interface pointer.

This would be the correct way:

disp = NULL;

hr = spSelection->Item(nItem, &disp);


> long size = 0;
> mailPtr2->get_Size(&size);
> get_Size() doesn't set size though


What is the value of size? Please retry with the

mailPtr2 retrieved through QueryInterface


> BSTR vSubject = ::SysAllocString(OLESTR("test"));
> BSTR vBody = ::SysAllocStringLen(NULL,size);


If you use hungarian notation you should use helpful

prefixes. v for BSTRs is confusing, looks more like VARIANT.

Why do you allocate them at all? Are they relevant for

your problem?


> get_Body() crashes Outlook. Any ideas?


how do you call it?

SvenC
 
Hi Sven,

Thanks so much, I just needed to use QueryInterface to cast between the two

interfaces. Works like a charm now. I'll also implement your other "pointers"

to avoid issues in the future.

Thanks again. You've gotten me well on my way!

-Gabriel
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
O Newbie question: how to sync two Outlook -Exchange and IMAP- calendars? Using Outlook 4
C Newbie needs help with Outlook Macro Outlook VBA and Custom Forms 3
J Newbie confusion Using Outlook 0
E Newbie, desperately seeking help Using Outlook 2
S Seperating email accounts, Syncronizing, and a few newbie questions Using Outlook 5
C Newbie import issues Using Outlook 3
J Newbie needs help Outlook VBA and Custom Forms 2
A General, how to get started? [kwrds begin start newbie] Outlook VBA and Custom Forms 3
R Newbie Redemption ISafeMailItemPtr question Outlook VBA and Custom Forms 6
Y Newbie : What is http://schemas.microsoft.com all about? Outlook VBA and Custom Forms 2
S HTML Code Embedded in String Within Open Outlook Email Preventing Replace(Application.ActiveInspector.CurrentItem.HTMLBody From Working Outlook VBA and Custom Forms 4
D Application.ActiveInspector().CurrentItem Returns Wrong(Old) Value Outlook VBA and Custom Forms 3
Z Inspector.CurrentItem causes Shared Calendar Issue Outlook VBA and Custom Forms 5
e_a_g_l_e_p_i Question about Outlook 2021 and Gmail Using Outlook 8
G Question marks in messages Using Outlook 2
e_a_g_l_e_p_i Question about calendar Using Outlook 5
e_a_g_l_e_p_i Question about installing my Gmail account on my iPhone but still getting messages downloaded to my desktop Outlook. Using Outlook 3
e_a_g_l_e_p_i Question about reinstalling Outlook 2021 Using Outlook 5
e_a_g_l_e_p_i question about 2-Step Verification with my gmail Using Outlook 0
D Question on removing an alias Using Outlook 1
D a general question re how backup programs handle pst files ... I have no problems, just curious Using Outlook 1
e_a_g_l_e_p_i A few question before I decide to switch to Pop from imap Using Outlook 9
D.Moore SendAndReceive question Outlook VBA and Custom Forms 2
J Transport Rule to detect Keyword question.. Exchange Server Administration 2
N Question Using Outlook 8
D Shared Mailbox question Exchange Server Administration 1
CWM030 Another Quarantine question Exchange Server Administration 0
E Outlook 2010 Can somebody tell me , the question associated with OST format Using Outlook 1
CWM030 A quick question for Diane about Exchange Exchange Server Administration 2
CWM030 Email Catagorties question Using Outlook 1
MahdeeyaAbdulla Friend's outlook question Using Outlook 1
e_a_g_l_e_p_i Question about address book in Outlook 2010 Using Outlook 9
M Question about nested distribution lists Outlook VBA and Custom Forms 3
e_a_g_l_e_p_i A question about installing office 2013 Pro and using my .pst from office 2010 Using Outlook 12
rerun101 Question about message options Outlook VBA and Custom Forms 4
M Question: Is there a rule that will save email in Windows Explorer Outlook VBA and Custom Forms 3
M Question on address book Using Outlook 1
e_a_g_l_e_p_i question about saving my .pst so I can import it to my Outlook after I build a new system Using Outlook 10
V Question on pop email Using Outlook 2
D a general question regarding data files Using Outlook 3
R Outlook 2007 - Email Question - POP3 to IMAP Outlook VBA and Custom Forms 11
D Advanced Search Question Using Outlook 1
adaminaus Quick question if i may Using Outlook 4
T Business Projects question BCM (Business Contact Manager) 0
V question about personal and shared calendars Exchange Server Administration 1
A Basic BCM question about sync to Outlook 2013 BCM (Business Contact Manager) 1
Jeff Rott Diane Question on "Use in a Run a Script Rule" Outlook VBA and Custom Forms 1
C Custom Forms: Question about retaining form information throughout the entire conversation Outlook VBA and Custom Forms 2
D OST question Exchange Server Administration 5
Mr Mayor Another recurring meeting question Using Outlook 1

Similar threads

Back
Top