MAPIOBJECT is junk when using MS WORD as Email Editor

  • Thread starter Thread starter thangarasu
  • Start date Start date
T

thangarasu

Hi,
I developing a C# Outlook add-in which sends the mail when the user
clicks my custom button on the Inspector window. I am using extended
MAPI to avoid the security warning in Office XP which is written in
C++.

I am passing the Outlook mailItem object and the MAPI Namespace to the
C++ dll which inturn gets the MAPI session from the Namespace.
Everything works perfect when the Outlook HTML editor is used as Email
editor. But when I set the email editor to MS WORD, the MAPI Object is
junk and I am not able to get the MAPI Session.

Any help is greatly appriciated.

Here is my C# code:
//import
[DllImport("ExtendedMAPIInterface.dll", EntryPoint="SendMailItem",
CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
public static extern uint
SendMailItem([MarshalAs(UnmanagedType.IDispatch)] System.Object
objNameSpace, ref IntPtr lpDispatch);

//Function call
IntPtr lpDispatch = Marshal.GetIDispatchForObject(item);
uint nResult = SendMailItem(applicationObject.GetNamespace("MAPI"),
ref lpDispatch);


//C++ Implementation:
HRESULT SendMailItem(LPDISPATCH pNameSpace, LPDISPATCH* pMailItem)
{
HRESULT hr =S_OK;

LPMAPISESSION pSession = NULL;
hr = GetMapiSession(pNameSpace, &pSession);
........
}


//Get MAPI Session is implemented as
HRESULT GetMapiSession(LPDISPATCH pMapiNameSpace, LPMAPISESSION*
ppMapiSession)
{
HRESULT hr = S_OK;

try
{
Outlook::_NameSpacePtr pMapiNS;

if (pMapiNameSpace == NULL)
return S_FALSE;

*ppMapiSession = NULL;

pMapiNameSpace->AddRef();
pMapiNS = pMapiNameSpace;


IUnknown * pUnk = pMapiNS->MAPIOBJECT;
if (pUnk == NULL)
return S_FALSE;

//Queryinterface will call the AddRef() internally.
hr= pUnk->QueryInterface(IID_IMAPISession, (void**)ppMapiSession);
if (!SUCCEEDED(hr))
{
Log("Failed to get the interface pointer for IMAPISession.\n");
return S_FALSE;
}
pUnk->Release();
}
catch(...)
{
Log("Extended MAPI: Exception occured when getting the MAPI
Session.");
}

return hr;
}


Thanks
Thanga
 
To begin with, sending a message using Extended MAPI while it is still being
displayed is a bad idea. You'll have much better results by simulating the
Send button click using the mouse_event() Windows API function.
What do you mean by "the MAPI Object is junk". Is it NULL? Do you get back
an error?

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
 
Hi Dmitry,
Thanks for your reply. What I mean by Junk is that, the MAPIOBJECT is
not null. But when I try to view value in the debugger(C#) it says it
cannot find the value for all of it member variables. When I looked at
the C++ code, the _vptr is 0x0efeeefe which is garbage.

To use the mouse_event, I not able to get the location of the Send
button when the email editor is set to MS WORD. The find control method
returns null for send button.

Here is my code.
CommandBarButton btnSend =
insp.CommandBars.FindControl(MsoControlType.msoControlButton, 2617,
System.Reflection.Missing.Value, System.Reflection.Missing.Value) as
CommandBarButton;

Appreciate your help.

Thanks
Thanga
 
You cannot see its values in the debugger because it is IUnknown. The
debugger only shows IDispatch-derived COM objects which expose a type info.
None of the MAPI objects are derived from IDispatch or expose any type info.
If you are using the Word editor, look for the "Envelope" control (id =
3746) and assume the Send button is in the top left corner.

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
 
Hi Dmitry,
Thanks for the help. I am able to make the button click to work.

-thanga
 
Back
Top