FocusInMailHeader and Word Addin

M

Mark Wilson

I've developed a Word COM Addin in C++ and I'm attempting to get the text of
a WordMail message body using the "Text" property. Most of the time this
works but occasionally it fails with a result code of 0x80020009.

The version of Outlook being used is Outlook XP (10.6760.6714) and prior to
accessing the Text property the SetFocus method was called to make sure that
the focus was in the message body.

It has been observed that every time the process fails, the value of
FocusInMailHeader is TRUE and every time the process succeeds the value of
FocusInMailHeader is FALSE.

Calling SetFocus when FocusInMailHeader is TRUE does not set the focus to
the message body even though the SetFocus method returns S_OK for a return
value.

How do you force the focus to be in the body of an e-mail message when Word
is selected as the e-mail editor?

Sample code:
-----------------------------------------------------------------------
hr = GetProperty(vtRange.pdispVal, L"Text", &vtText);
if (FAILED(hr))
{
OutputDebugString("Error: Can't get Text property");
hr = GetProperty(pDoc, L"Application", &vtApplication);
if (!FAILED(hr))
{
hr = GetProperty(vtApplication.pdispVal, L"FocusInMailHeader", &vtFocus);
if (!FAILED(hr))
{
if (vtFocus.boolVal)
{
OutputDebugString("Error: FocusInMailHeader = TRUE");
hr = GetProperty(pDoc,L"ActiveWindow",&vtWindow);
if (!FAILED(hr))
{
hResult = CallMethod(vtWindow.pdispVal, L"SetFocus", NULL, 0, 0);

if (!FAILED(hResult))
{
hr = GetProperty(vtApplication.pdispVal, L"FocusInMailHeader",
&vtFocus);
if (!FAILED(hr))
{
if (vtFocus.boolVal)
OutputDebugString("Error: FocusInMailHeader = TRUE (after
SetFocus)");
}
}
}
}
}
}
}
 
P

Peter Huang [MSFT]

Hi Mark,

Did the problem occur in all scenario.
When you compose a new email or when you reply to an email?

Also based on my research, the SetFocus may not work well.
I think you may try to use the Thisdoucment.Content.Text to get the content.
You may have a try.

If you still have any concern, please feel free to post here.


Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

Mark Wilson

Hi Peter.

The code is called from the Invoke method and when dispIdMember = 0x0000000a
(i.e. WindowActivate)

The failure to get the "Text" property is intermittent. I know it happens
occasionally when composing a new message. I'm not sure if it happens on a
Reply/Forward. I'll check for that.

I've expanded the code a bit so you can better see what is happening. As
far as I can tell I'm trying to access the Text property as you have
suggested by accessing the Content from a pointer to the current ducument and
then trying to get the Text.

STDMETHODIMP WordAppHandler::Invoke(DISPID dispIdMember, REFIID riid, LCID
lcid, WORD wFlags, DISPPARAMS* pDispParams, VARIANT* pVarResult, EXCEPINFO*
pExcepInfo, UINT* puArgErr)

....
pDoc = pDispParams->rgvarg[0].pdispVal;

....
hr = GetProperty(pDoc, L"content", &vtRange);
if (!FAILED(hr))
{
hr = GetProperty(vtRange.pdispVal, L"characters", &vtCharacters);
StringCchPrintf(szBuffer, _countof(szBuffer),"hr=%08X", hr);
MessageBox(GetFocus(),szBuffer,"WORDINSERT characters" , MB_OK);
hr = GetProperty(vtCharacters.pdispVal, L"count", &vtCount);
StringCchPrintf(szBuffer, _countof(szBuffer),"hr=%08X\r\ncount = %d", hr,
vtCount.lVal);
MessageBox(GetFocus(),szBuffer,"WORDINSERT count" , MB_OK);
hr = GetProperty(vtRange.pdispVal, L"Text", &vtText);
if (FAILED(hr))
{
OutputDebugString("Error: Can't get Text property");
hr = GetProperty(pDoc, L"Application", &vtApplication);
if (!FAILED(hr))
{
hr = GetProperty(vtApplication.pdispVal, L"FocusInMailHeader", &vtFocus);
if (!FAILED(hr))
{
if (vtFocus.boolVal)
{
OutputDebugString("Error: FocusInMailHeader = TRUE");
hr = GetProperty(pDoc,L"ActiveWindow",&vtWindow);
if (!FAILED(hr))
{
hResult = CallMethod(vtWindow.pdispVal, L"SetFocus", NULL, 0, 0);

if (!FAILED(hResult))
{
hr = GetProperty(vtApplication.pdispVal, L"FocusInMailHeader",
&vtFocus);
if (!FAILED(hr))
{
if (vtFocus.boolVal)
OutputDebugString("Error: FocusInMailHeader = TRUE (after
SetFocus)");
}
}
}
}
}
}
}
}

I'm always able to get a valid Count value matching the number of characters
in the message body. Its just the actual "Text" property that seems to fail.

I've also tested the value of EnvelopeVisible and it is TRUE when I'm trying
to SetFocus.
 
P

Peter Huang [MSFT]

Hi

Thanks for your reply.
Based on my understanding, you are do the job that get text from the
Windows Activate event.
Because thei windows activate event will fire when the document is going to
open or active so that context may not be ready in time.
So I think you may try write a outlook addin or macro to get the word
document instance and get the text.

Also since the problem did not persist all the time, I think you may try to
contact MSPSS directly to do further troubleshooting.
support.microsoft.com.

if you still have any concern, please feel free to post here.
Thanks for your understanding!

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

Mark Wilson

Thanks Peter.

I already have an Outlook Addin to handle situations where Outlook is the
e-mail editor. I need to develop a Word Addin to handile situations that the
Outlook Addin can't cope with such as when Word is chosen as the e-mail
editor.

You might be correct that the context might not be ready at the time
WindowActivate fires, however the character count is valid and its just the
Text that can't be retrieved.

Since this always fails when FocusInMailHeader is TRUE, is there any way to
force the focus to be in the message body. MSDN documentation says that the
setFocus Method

"Sets the focus of the specified document window to the body of an e-mail
message. If the document isn't an e-mail message, this method has no effect."

So in my case it is a WordMail docunment, EnvelopeVisible = TRUE

Therefore SetFocus should cause FocusInMailHeader to be FALSE.

In my case that isn't working so it appeard to be a bug in the SetFocus
Method.

Before I move this issue on to PSS, do you agree with my summary?
 
P

Peter Huang [MSFT]

Hi

Thanks for your quickly reply!
Partially I agree with your summary.
As I said before, in the WindowActivate event, the context may not be ready
so the problem did not persist all the time per your description.
So I can not confirm there is problem in the SetFocus funtion, because we
did not provide complex project code review. Also for your scenario, we
need low level debugging into the Outlook and Word process to see what is
going on.
That is why I suggest you contact MSPSS directly.

BTW: if the PSS confirmed this is problem in the SetFocus method, the
incident fee will be refund.

Thanks for your understanding!

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top