Change Outlook item programmatically - "Do you want to save change

S

Snixtor

I have developed a very simple, small Outlook 2007 add-in using VSTO3, and am
having issues with "unsaved changes" whenever I make programmatic changes to
the Outlook item.

I have a saved item, say, an email in "Drafts". When the user clicks a
button in the ribbon it changes the subject of the email. The user then
attempts to close the email and appropriately enough they are prompted
whether or not they want to save changes. Fair enough. The issue though is
that when they open the mail item a second time and make NO changes (and
don't click on that button), when they go to close the mail item it *again*
prompts if they want to save changes, though they have made none! Again when
they attempt to close Outlook itself they are prompted whether they want to
save changes.

What's going on here? Why don't my programmatic changes to the mail item
appear to be getting properly discarded until Outlook is completely shut down?
 
R

Roady [MVP]

That's hard to say without knowing what you are doing in your code.

Note that there are separate newsgroups for programming with Outlook down
the hall. They all start with;
outlook.program_
 
S

Snixtor

My code, on a ribbon button-click event is as follows:

Outlook.Inspector inspector = (Outlook.Inspector)this.Context;
Outlook.MailItem mailItem = (Outlook.MailItem)inspector.CurrentItem;
mailItem.Subject = "hello";
 
S

Snixtor

This seems to be about all I really needed. Releasing the reference to the
mail item after changing its property
(System.Runtime.InteropServices.Marshal.ReleaseComObject(mailItem);) did the
job. Of course, in an add-in environment I believe there's scenarios where
one has to be careful about clearing an object reference (synchronous event
arguments I believe?), but this is an issue for another day and another
thread.

Thanks Roady.



:

....
And at the end dispose of your declarations.
....
 
R

Roady [MVP]

You're welcome! :)

I didn't mean fully disposing though but setting all your values back to
Nothing at the end of your code.



-----
 
S

Snixtor

My tests show that re-setting the values doesn't work, Outlook will still
consider the item to be "dirty" and prompt to save changes. Nor does simply
nulling the object reference work. As an interop / COM object, the reference
has to be explicitly released.
 

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