Modifying a WordMail Message Subject

M

Mark Wilson

I'm developing a Word COM Addin in C++ and I need to be able to read and
write the message subject. I'm testing with Office 2003 and when I execute
the code below I get a return value of E_NOTIMPL on the call to "Mailer"

//--------------------------------------------
HRESULT hr;
LPDISPATCH pDoc;
VARIANT vtMailer; VariantInit(&vtMailer);
VARIANT vtSubject; VariantInit(&vtSubject);

hr = GetProperty(pDoc, L"Mailer", &vtMailer);
if (!FAILED(hr))
hr = GetProperty(vtMailer.pdispVal, L"Subject", &vtSubject);
//--------------------------------------------

What is the preferred method of modifying a message subject from a Word COM
Addin?
 
P

Peter Huang [MSFT]

Hi

Based on my research, the Word Object Modal did not have the Mailer
Property, although the intellisense has one.
Office 2003 VBA Language References
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odc_2003_ta
/html/odc_landoffice03_vba.asp

Based on my research, it seems that only the Excel OM documented that the
is used only on Macintosh.
Mailer Property [Excel 2003 VBA Language Reference]
You have requested Help for a Visual Basic keyword used only on the
Macintosh. For information about this keyword, consult the language
reference Help included with Microsoft Office Macintosh Edition.
Applies to | Workbook Object

So in what platform do you run the code, a x86 machine or Macintosh.

Also if you means the Mail created with Outlook 2003 but use the Word as
the editor, I think if you want set/get the subject, you need use Outlook
Object Modal.
Sub Test3()
Dim mi As MailItem
Set mi = Application.ActiveInspector.CurrentItem
mi.Subject = "Testt"
Debug.Print mi.Subject
End Sub

MailItem Object [Outlook 2003 VBA Language Reference]
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbaol11/htm
l/olobjMailItem_HV05247822.asp

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 checked the url you gave me
http://msdn.microsoft.com/library/d...-us/vbaol11/html/olobjMailItem_HV05247822.asp

and there is no indication on that page that this is a Macintosh only object
so perhaps the docunemtation needs improvement :(

In any case I'm working on an x86 platform with Outlook 2000, Outlook XP and
Outlook 2003 as targets.

My code is based upon the sample in KB230689
http://support.microsoft.com/default.aspx?scid=kb;en-us;230689

I'll add the following two lines to my code and see if I can get acccess to
Outlook objects.

#import "C:\Program Files\Microsoft Office\Office\mso9.dll"
#import "C:\Program Files\Microsoft Office\Office\msoutl9.olb"
 
M

Mark Wilson

Hi Peter.

I added the following to my project:

#import "C:\Program Files\Microsoft Office\Office\mso9.dll"
rename("DocumentProperties", "DocProps") rename("RGB", "_RGB")
#import "C:\Program Files\Microsoft Office\Office\msoutl9.olb"

However I can't seem to assign anything so that I can access Outlook
objects. Intelisense is working so I defined an _ApplicationPtr and a
_DocumentItemPtr. I have values for trhe Word application and the document.
However when I try to assign them to Outlook items they end up as NULL. Any
idea what would be wrong with this code?

----------------------------------------------------------------
hr = GetProperty(pDoc, L"Application", &vtApplication);
if (!FAILED(hr))
{
IDispatch *pApplicationPtr = NULL;
Outlook::_ApplicationPtr pApp = NULL;
pApplicationPtr = vtApplication.pdispVal;
pApp = pApplicationPtr;
// pApp is still NULL

Outlook::_DocumentItemPtr pDocument = NULL;
pDocument = pDoc;
// pDocument is still NULL
 
P

Peter Huang [MSFT]

Hi

Based on my knowledge, the Mailer property is for the Excel OM based on the
document, the Word OM did not have the Mailer Property.
Also the link I provide is for the Outlook OM's MailItem Object.
They are three different OMs.

Also for your scenario, what you need to do is to create an Outlook 2003
Addin but not a Word 2003 Addin, because the MailItem belongs to the
Outlook OM.

BTW: OM = Object Modal

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

Peter, thanks for the reply.

I already have an add-in for Outlook that processes the information in the
subject line and the message body when I click a button I installed on the
command bar. However when WordMail is being used as the e-mail editor I
needed to create a Word COM addin to access and process information in the
message body when I click its button on the command bar.

Your sample macro works great if you are using Outlook as the e-mail editor.
I can create your macro and drag it to the toolbar. However if I configure
Outlook to use Word as the e-mail editor, then your Outlook Macro does not
show on the toolbar when composing a message.

I really need a solution that will work when WordMail is editing the message.

I know in Outlook you can access the Word Object Model by using the
WordEditor Object as follows:

IDispatch *pEditor = NULL;
if(m_pInsp->get_WordEditor(&pEditor) == S_OK)
{
Word::_DocumentPtr pWordDoc = NULL;
pWordDoc = pEditor;
if (pWordDoc != NULL)
{
Word::_DocumentPtr docPtr = pWordDoc;

How do I access the Outlook OM from Word? Please see my other post of
October 24.
 
P

Peter Huang [MSFT]

Hi

Commonly Word/Outlook all have their OM, they can not be exchanged between
each other.
But in Outlook, we have a WordEditor to get the Word Document interface,
but in Word there is no such a property to get outlook OM.
Also if we use Word as the Email Editor, the CommandBar is owned by Word
Process, it can not call the code in Outlook Process, they are in the two
different processes.



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

OK. Let me sumarize.

1) The message subject is only accessible from the Outlook OM.
2) When Word is the e-mail editor the command bar is owned by Word and is
accessible from a Word COM addin.
3) The Word COM addin can only access the Word OM.

Conclusion:
When Word is the e-mail editor it is IMPOSSIBLE to modify the message
subject from an Addin.

Can you confirm my conclusion please?

I find that very hard to believe.


Also, using OutlookSpy from www.dimastr.com I'm able to navigate to the
WordEditor object in an inspector.

Then I can drill down to

_Document.CustomDocumentProperties.Parent.MailEnvelope.Item.Subject

and see the text string that is in the subject field in the current message
when Word is editing the message.

Would you have any comments on using that technique?
 
P

Peter Huang [MSFT]

Hi

Here is a KB for your reference.

Considerations When You Use Microsoft Word as the E-Mail Editor
When you are designing an Outlook CommandBar solution for mail items, it is
important that you understand that different CommandBars are used when
Microsoft Word is set as the e-mail editor. Some key points that you must
consider when you use Word as the e-mail editor are:
- CommandBars are part of Word, not Outlook.
- Different CommandBars are available based on the message editor and
format.
- Custom buttons may disappear when you change message editor or format.
- The special Envelope CommandBar is not designed to be customized.
- There is no Inspector object for the HTML and Plain Text formats with
Office 2000 if Word is set as the e-mail editor.

201095 OL: How to Use CommandBars in Outlook Solutions
http://support.microsoft.com/?id=201095

1 We can change the subject from Word/Outlook Process
[Outlook: the Application.ActiveInspector.CurrentItem is the MailItem]
Dim ip As Inspector
Set ip = Application.ActiveInspector
Dim mi As MailItem
Set mi = ip.CurrentItem
mi.Subject="dfdf"

[Word]
Sub Test()
Dim ol As Outlook.MailItem
Set ol = ThisDocument.CustomDocumentProperties.Parent.MailEnvelope.Item
ol.Subject = "dfdffasdfasdf"
End Sub

2. - CommandBars are part of Word, not Outlook. ReadAbove KB.
We can add a commandBar from Outlook/Word.
Because we can get the Word interface, so we can add commandbar via Word's
OM or Outlook'OM.
Now the problem is that if we do that in Outlook, the Button_click's code
is in the Outlook's process, but the button is owned by Word, when we click
the button, the button will try to call the code in another process(Outlook)
e.g.
Set cbb = ip.CommandBars("Standard").Controls.Add(msoControlButton, , , ,
True)
cbb.FaceId = 14
cbb.Caption = "afasdf"

3. As in 1. in Outlook, we can use CurrentItem to get the MailItem object,
so we can get the MailItem.Subject, MailItem.BCC......

4. We can access to the Word's OM from outlook, once we get the word's
reference, WordEditor. But now in outlook we add button to Word's
CommandBars, so the CommandBars will be invoked in word's, but we add event
(Button_click) in Outlook, so the Click code is in the Outlook's process.


In Summary, we can modified the WordMail's content, subject ... from
Outlook Addin, the problem is cross process fire the Button_Click. If you
just wants to change the Subject, we can just do that in the Outlook's
Addin.

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.
 
K

Ken Slovak - [MVP - Outlook]

Pardon me for jumping in, but I create buttons in WordMail all the time and
handle them within my Outlook COM addin code. I just handle NewInspector in
the Inspectors collection in the Outlook addin and add my buttons to the
Standard toolbar or to Menu Bar. I can then handle the Click event within my
Outlook addin. The only limitation I see is that some properties and methods
of the button object aren't available unless I re-instantiate the button
within the click event handler. If I do that I can use PasteFace or other
methods that otherwise would be unavailable. Just handling the click event
and doing something like changing Inspector.CurrentItem.Subject doesn't
require re-instantiating the button.




"Peter Huang" said:
Hi

Here is a KB for your reference.

Considerations When You Use Microsoft Word as the E-Mail Editor
When you are designing an Outlook CommandBar solution for mail items, it
is
important that you understand that different CommandBars are used when
Microsoft Word is set as the e-mail editor. Some key points that you must
consider when you use Word as the e-mail editor are:
- CommandBars are part of Word, not Outlook.
- Different CommandBars are available based on the message editor and
format.
- Custom buttons may disappear when you change message editor or format.
- The special Envelope CommandBar is not designed to be customized.
- There is no Inspector object for the HTML and Plain Text formats with
Office 2000 if Word is set as the e-mail editor.

201095 OL: How to Use CommandBars in Outlook Solutions
http://support.microsoft.com/?id=201095

1 We can change the subject from Word/Outlook Process
[Outlook: the Application.ActiveInspector.CurrentItem is the MailItem]
Dim ip As Inspector
Set ip = Application.ActiveInspector
Dim mi As MailItem
Set mi = ip.CurrentItem
mi.Subject="dfdf"

[Word]
Sub Test()
Dim ol As Outlook.MailItem
Set ol = ThisDocument.CustomDocumentProperties.Parent.MailEnvelope.Item
ol.Subject = "dfdffasdfasdf"
End Sub

2. - CommandBars are part of Word, not Outlook. ReadAbove KB.
We can add a commandBar from Outlook/Word.
Because we can get the Word interface, so we can add commandbar via Word's
OM or Outlook'OM.
Now the problem is that if we do that in Outlook, the Button_click's code
is in the Outlook's process, but the button is owned by Word, when we
click
the button, the button will try to call the code in another
process(Outlook)
e.g.
Set cbb = ip.CommandBars("Standard").Controls.Add(msoControlButton, , , ,
True)
cbb.FaceId = 14
cbb.Caption = "afasdf"

3. As in 1. in Outlook, we can use CurrentItem to get the MailItem object,
so we can get the MailItem.Subject, MailItem.BCC......

4. We can access to the Word's OM from outlook, once we get the word's
reference, WordEditor. But now in outlook we add button to Word's
CommandBars, so the CommandBars will be invoked in word's, but we add
event
(Button_click) in Outlook, so the Click code is in the Outlook's process.


In Summary, we can modified the WordMail's content, subject ... from
Outlook Addin, the problem is cross process fire the Button_Click. If you
just wants to change the Subject, we can just do that in the Outlook's
Addin.

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

Ken and Peter:

I finally figured out how to solve my problem "in theory" since my addin is
in C++ and this is a Word macro.

Sub SetWordMailSubject()
Dim ObjEnvelope As MsoEnvelope
Set ObjEnvelope = ActiveDocument.MailEnvelope
ObjEnvelope.Item.Subject = "This is the subject"
End Sub

Therefore, in my Word COM addin, if I'm reading a e-mail message and Word is
selected as the editor, I'll have a button on the command bar. When I click
the button I can call SetWordMailSubject and change the e-mail subject.

The part that I was missing was the MailEnvelope object. From a Word
document object it allows you the get back to the Outlook properties that are
specific to e-mail such as the message "subject".

Thanks.

--
Mark Wilson



Ken Slovak - said:
Pardon me for jumping in, but I create buttons in WordMail all the time and
handle them within my Outlook COM addin code. I just handle NewInspector in
the Inspectors collection in the Outlook addin and add my buttons to the
Standard toolbar or to Menu Bar. I can then handle the Click event within my
Outlook addin. The only limitation I see is that some properties and methods
of the button object aren't available unless I re-instantiate the button
within the click event handler. If I do that I can use PasteFace or other
methods that otherwise would be unavailable. Just handling the click event
and doing something like changing Inspector.CurrentItem.Subject doesn't
require re-instantiating the button.




"Peter Huang" said:
Hi

Here is a KB for your reference.

Considerations When You Use Microsoft Word as the E-Mail Editor
When you are designing an Outlook CommandBar solution for mail items, it
is
important that you understand that different CommandBars are used when
Microsoft Word is set as the e-mail editor. Some key points that you must
consider when you use Word as the e-mail editor are:
- CommandBars are part of Word, not Outlook.
- Different CommandBars are available based on the message editor and
format.
- Custom buttons may disappear when you change message editor or format.
- The special Envelope CommandBar is not designed to be customized.
- There is no Inspector object for the HTML and Plain Text formats with
Office 2000 if Word is set as the e-mail editor.

201095 OL: How to Use CommandBars in Outlook Solutions
http://support.microsoft.com/?id=201095

1 We can change the subject from Word/Outlook Process
[Outlook: the Application.ActiveInspector.CurrentItem is the MailItem]
Dim ip As Inspector
Set ip = Application.ActiveInspector
Dim mi As MailItem
Set mi = ip.CurrentItem
mi.Subject="dfdf"

[Word]
Sub Test()
Dim ol As Outlook.MailItem
Set ol = ThisDocument.CustomDocumentProperties.Parent.MailEnvelope.Item
ol.Subject = "dfdffasdfasdf"
End Sub

2. - CommandBars are part of Word, not Outlook. ReadAbove KB.
We can add a commandBar from Outlook/Word.
Because we can get the Word interface, so we can add commandbar via Word's
OM or Outlook'OM.
Now the problem is that if we do that in Outlook, the Button_click's code
is in the Outlook's process, but the button is owned by Word, when we
click
the button, the button will try to call the code in another
process(Outlook)
e.g.
Set cbb = ip.CommandBars("Standard").Controls.Add(msoControlButton, , , ,
True)
cbb.FaceId = 14
cbb.Caption = "afasdf"

3. As in 1. in Outlook, we can use CurrentItem to get the MailItem object,
so we can get the MailItem.Subject, MailItem.BCC......

4. We can access to the Word's OM from outlook, once we get the word's
reference, WordEditor. But now in outlook we add button to Word's
CommandBars, so the CommandBars will be invoked in word's, but we add
event
(Button_click) in Outlook, so the Click code is in the Outlook's process.


In Summary, we can modified the WordMail's content, subject ... from
Outlook Addin, the problem is cross process fire the Button_Click. If you
just wants to change the Subject, we can just do that in the Outlook's
Addin.

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.
 
P

Peter Huang [MSFT]

Hi

Thanks for Mark and Ken's knowledge sharing in the community.
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.
 

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