|
Christian Fasold
Posts: 45
Joined: 2013-11-27
|
Hello everybody,
we have a problem again and would be really happy if you could us to solve it :)
I would like to do the following:
If the user has an email opened he should be able to click a button. That button takes the written email
and saves it into a custom folder below "Inbox". There it should be saved as a "template" for future reuse.
My plugin already checks if the custom folder exists and creates it if it doesn't exist yet.
But my problem is now when I try to "save" the current open mail:
....
mail = folderItems.Add(Outlook.OlItemType.olMailItem)
movedMail = mail.Move(targetFolder)
Dim mailItem As Outlook.MailItem = TryCast(objItem, Outlook.MailItem)
movedMail.Subject = mailItem.Subject
' The following statement raises a security warning:
movedMail.HTMLBody = mailItem.HTMLBody
movedMail.Save()
movedMail.Display(False)
The problem here is when the plugin tries to copy the current (HTML) body into the already saved (empty) "template" mail.
Or is there an easier way to take the "whole" mail and put it into the folder?
The reason why I don't want to save the template mail into the normal computer file system is, that I would like to have it to be backed up automatically to Exchange, that's why I try to use a "normal" email folder...
Thanks a lot in advance!
Best regards,
Christian |
|
Posted 18 Feb, 2015 04:13:07
|
|
Top
|
|
Andrei Smolin
Add-in Express team
Posts: 18821
Joined: 2006-05-11
|
Hello Christian,
If your code is run by an Outlook add-in on Outlook 2007+, then the only way for you to get that security warning is to get mailItem via a chain of calls originating from an "unsafe" Outlook.Application. The "safe" Outlook.Application is declared on your add-in module, see the OutlookApp property; outside of the add-in module, you access this property using {add-in project name e.g. MyAddin1}.AddinModule.CurrentInstance.OutlookApp. A couple examples of getting an "unsafe" Outlook.Application:
- new Outlook.Application()
- {add-in module}.OutlookApp.Application
Andrei Smolin
Add-in Express Team Leader |
|
Posted 18 Feb, 2015 07:08:28
|
|
Top
|
|
Christian Fasold
Posts: 45
Joined: 2013-11-27
|
Hello Andrei,
would you please be so kind as to help me out a little bit more? :-)
Here is the sub which takes the current mail and saves it as a template into an Exchange folder:
Private Sub templatePersonalSaveCurrentMailAsTemplateInExchangeFolder()
' This sub takes the current mail edit window content and saves it as a template in an Exchange folder
Dim myItem As Outlook.Inspector
Dim objItem As Object
Dim myOlApp = CreateObject("Outlook.Application")
Dim nSpace As Outlook.NameSpace = Nothing
Dim targetFolder As Outlook.MAPIFolder = Nothing
Dim folderItems As Outlook.Items = Nothing
Dim mail As Outlook.MailItem = Nothing
Dim movedMail As Outlook.MailItem = Nothing
myItem = myOlApp.ActiveInspector
If Not TypeName(myItem) = "Nothing" Then
objItem = myItem.CurrentItem
Dim strname = objItem.Subject
'Prompt the user for confirmation
Dim mapiNameSpace As Outlook.NameSpace = OutlookApp.GetNamespace("MAPI")
Dim folderInbox As Outlook.MAPIFolder = mapiNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
For i = 1 To folderInbox.Folders.Count
' Search for the personal template folder
If folderInbox.Folders.Item(i).Name = templatePersonalFolderName Then
targetFolder = folderInbox.Folders.Item(i) ' Personal template folder found
End If
Next
folderItems = targetFolder.Items
mail = folderItems.Add(Outlook.OlItemType.olMailItem)
' move the item to Inbox and then save it there
movedMail = mail.Move(targetFolder)
Dim mailItem As Outlook.MailItem = TryCast(objItem, Outlook.MailItem)
movedMail.Subject = mailItem.Subject
'This statement raises a security warning
movedMail.HTMLBody = mailItem.HTMLBody
movedMail.Save()
movedMail.Display(False)
End If
End Sub
Could you tell me exactly what I would have to change here to get it working without the warning?
That would really be great :-)
Thanks a lot and best regards,
Chris |
|
Posted 19 Feb, 2015 07:45:34
|
|
Top
|
|
Andrei Smolin
Add-in Express team
Posts: 18821
Joined: 2006-05-11
|
Hello Christian,
If your code is run by an Outlook add-in then instead of CreateObject("Outlook.Application") you need to call {add-in project name e.g. MyAddin1}.AddinModule.CurrentInstance.OutlookApp.
Also I believe Dim myOlApp should be Dim myOlApp As Outlook.Application. The last line requires Import Outlook = Microsoft.Office.Interop.Outlook.
Andrei Smolin
Add-in Express Team Leader |
|
Posted 19 Feb, 2015 10:37:39
|
|
Top
|
|
Christian Fasold
Posts: 45
Joined: 2013-11-27
|
Andrei,
you are my hero, thank you!
I could now get rid of the security warning :-)
Is there an easy way to take the whole mail (which is currently open) including recipient(s), subject, body, etc. and save that into the Exchange folder in the background (so that the user doesn't see the copied mail at all)?
Thanks again and best regards,
Christian |
|
Posted 20 Feb, 2015 02:41:47
|
|
Top
|
|
Andrei Smolin
Add-in Express team
Posts: 18821
Joined: 2006-05-11
|
Hello Christian,
)
The Outlook object model provides the only way: use MailItem.Copy. Alas, the copy will be created in the same folder and the user will see it.
Andrei Smolin
Add-in Express Team Leader |
|
Posted 20 Feb, 2015 03:53:19
|
|
Top
|
|