How to save edits on RTFBody/WordEditor of a mail item and meeting item?

Add-in Express™ Support Service
That's what is more important than anything else

How to save edits on RTFBody/WordEditor of a mail item and meeting item?
 
Horatiu Muresan


Guest


Hello,

I've implemented a functionality for parsing phone numbers in mail items and meeting items having the body type either HTML or RTF.

For HTML it works great. For RTF I'm using the inspector's WordEditor to inject my hyperlinks in the found numbers of the meeting item content. It also works, the only problem I have is that in order for the changes to be applied, I need to do this not so nice workaround:

//workaround for force refresh of the item's RtfBody and Body before saving- show it and hide it
item.Display();
item.Close(Outlook.OlInspectorClose.olSave);

item.Save();
where item is a meeting item.


The reason I do this is that if I do not force a display the save does not work, it ignores all my changes. During the current session of outlook if I move to another meeting/mail item in my inbox and then go back to the parsed meeting item, I can see the changes, but if I restart outlook the changes are lost. With the above workaround the changes are preserved.

What I suspect that happens is that the reading pane does not update the content with the changes I did on the WordEditor object, and when loading another mail/meeting item in the reading pane, it saves the current item with the old content unless I do the workaround.

Or even more plausible, when the item.Save() call happens, without the workaround it saves the old content because changes to WordEditor did not propagate.

Can you suggest a more elegant fix for this? As it is a bit disturbing visually for the customer having a window popping up and then closing.


Thank you!

Regards from Romania (GMT+2)
Horatiu Muresan
Posted 13 Oct, 2016 04:34:44 Top
Andrei Smolin


Add-in Express team


Posts: 18793
Joined: 2006-05-11
Hello Horatiu,

I'm not sure that I will be able to help you; we've never met such an issue.

In what event do you modify the item?


Andrei Smolin
Add-in Express Team Leader
Posted 13 Oct, 2016 06:07:52 Top
Horatiu Muresan


Guest


Hello Andrei,

I've sent a sample add-in to the support address. Title: "Save edits on RTFBody/WordEditor?"
Thank you!

Regards from Romania(GMT+2),
Horatiu Muresan
Posted 17 Oct, 2016 07:53:11 Top
Andrei Smolin


Add-in Express team


Posts: 18793
Joined: 2006-05-11
Thank you, Horatiu!

Let's suppose you commented out the code of that "workaround". Let's also suppose that you've selected an item and your code handled it. We know that Outlook doesn't display the changes if the selected item is RTF-formatted (supposedly, this occurs due to some problem in Outlook). I wonder if Outlook displays the item correctly, if - in this very moment - you click the item once again (this triggers another SelectionChange event but your code handles this scenario). If Outlook displays the email correctly, you could try to manipulate the selection in order to let Outlook re-display the item; see e.g. https://www.add-in-express.com/forum/read.php?FID=5&TID=8587.


Andrei Smolin
Add-in Express Team Leader
Posted 17 Oct, 2016 08:58:54 Top
Horatiu Muresan


Guest


Thank you Andrei! I'll try this tomorrow first thing.

Regards from Romania (GMT+2),
Horatiu Muresan
Posted 17 Oct, 2016 09:35:14 Top
Horatiu Muresan


Guest


No luck :( .

I have 2 more ideas to try for which I need your help:
1. Is there a way to seamlessly refresh the preview/reading pane?
2. Simply clearing and re-adding item to the explorer's selection doesn't do the trick - tried this both before calling the save and after calling the save on the meeting item. What might work is if I change the selection to another item and then back to the current item. Not sure on which other item to change though, cause I don't even have the guarantee that there is another select-able item. This might work because it seems that the refresh happens when you change to other item in outlook and then click back to the current item (so that is what I'm trying to simulate)

Thank you!

Regards from Romania (GMT+2),
Horatiu Muresan
Posted 18 Oct, 2016 09:45:55 Top
Andrei Smolin


Add-in Express team


Posts: 18793
Joined: 2006-05-11
Hello Horatiu,

#1. No.

#2.
Horatiu Muresan writes:
Simply clearing and re-adding item to the explorer's selection doesn't do the trick


I would try doing this using two test Ribbon buttons: one clears the selection and another one re-adds an item to the selection. In other words, let Outlook react to this change.


Andrei Smolin
Add-in Express Team Leader
Posted 19 Oct, 2016 06:57:55 Top
Horatiu Muresan


Guest


Hi Andrei,

I changed my approach a bit, instead of using the WordEditor of the inspector item, I'm using the RTFBody property of the meetingItem itself. Got a far better result so far with this new approach, the refresh happens immediately after the processing in the reading pane.

The implementation is like this:
1. I save the RTFBody (casted to the inner object which is a byte array) into a temp file.
2. I instantiate a new Word.Application object, and get my Word.Document object like this(for now I still have hard-coded stuff, I was focusing on having a working POC):
            Word.Application app = new Word.Application();
            Word.Documents docs = app.Documents;
            Word.Document doc = docs.Open(@"C:	mp	estrtf.rtf", false);
            doc.Activate();


3. I do the processing(injecting my hyperlinks) just as before
4. I save and close the doc and quit the app:
            doc.Save();
            doc.Close();
            app.Quit();
            Release(doc);
            Release(docs);
            Release(app);

5. I get my new RTFBody content by loading the content of the temp file which has my changes since I saved the doc object earlier:

            byte[] retByte;
            using (FileStream stream = File.Open(@"C:	mp	estrtf.rtf", FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                using (StreamReader sr = new StreamReader(stream))
                {
                    var ret = sr.ReadToEnd();
                    retByte = Encoding.UTF8.GetBytes(ret);
                }
            }
            return retByte;




What I don't like about this approach is that I have to do disk writes/reads. I tried to find an approach which uses memory exclusively which would have being so much better, but with no success so far.


Thank you!

Regards from Romania (GMT+2),
Horatiu Muresan
Posted 21 Oct, 2016 05:51:10 Top
Andrei Smolin


Add-in Express team


Posts: 18793
Joined: 2006-05-11
Hello Horatiu,

You can create a MailItem or AppoiintmentItem, retrieve GetInspector, and use its WordEditor to perform all the operations which require the Word object model, and finally dispose the item without even showing it.


Andrei Smolin
Add-in Express Team Leader
Posted 24 Oct, 2016 09:29:45 Top
Horatiu Muresan


Guest


Hi Andrei,

What would be the advantage over my approach? I'm afraid that I'll run into my initial problem if using WordEditor object (if I understand correctly, you are suggesting to duplicate the original WordEditor object into a new meeting item's inspector, do the processing on this new temp meeting item and then assign the processed content back on the original inspector's WordEditor?).

Thank you.

Regards from Romania (GMT+2),
Horatiu Muresan
Posted 24 Oct, 2016 09:39:23 Top