Writeable active inline response in outlook - vba

I have the following code from some blog that inserts HTML from the clipboard into an outlook email.
Sub PrependClipboardHTML()
Dim email As Outlook.MailItem
Dim cBoard As DataObject
Set email = Application.ActiveInspector.CurrentItem
Set cBoard = New DataObject
cBoard.GetFromClipboard
email.HTMLBody = cBoard.GetText + email.HTMLBody
Set cBoard = Nothing
Set email = Nothing
End Sub
It works great except that the email has be in its own window (i.e. popped-out) otherwise it will fail.
I was looking around on the documentation and found Application.ActiveExplorer.ActiveInlineResponse here.
However the documentations says that it is read-only, and indeed it does not work. Is there way to get a writable version of the inline response?

It works great except that the email has be in its own window (i.e. popped-out) otherwise it will fail.
That is because you have the following statement in the code:
Set email = Application.ActiveInspector.CurrentItem
However the documentations says that it is read-only, and indeed it does not work.
Try to use the following code instead:
Set email = Application.ActiveExplorer.ActiveInlineResponse
The ActiveInlineResponse property is read-only, but not the object's properties you are going to use. That means you can't set another mail item to the inline response, but will be able to set up properties of the retrieved item.

Maybe you're trying to work with ActiveExplorer + Selection.Item Method (Outlook)
Example
Option Explicit
Public Sub Example()
Dim email As Outlook.MailItem
Set email = Application.ActiveExplorer.Selection.Item(1)
Debug.Print email.Subject ' print on immediate window
End Sub
Or Work with both opened and selected items
Function GetCurrentItem() As Object
Dim objApp As Outlook.Application
Set objApp = Application
On Error Resume Next
Select Case TypeName(objApp.ActiveWindow)
Case "Explorer"
Set GetCurrentItem = objApp.ActiveExplorer.Selection.Item(1)
Case "Inspector"
Set GetCurrentItem = objApp.ActiveInspector.CurrentItem
End Select
Set objApp = Nothing
End Function

You cannot concatenate two HTML strings and expect a valid HTML back. The two must be merged.
That being said, use Word Object Model to paste from the clipboard:
Application.ActiveEXplorer.ActiveInlineResponseWordEditor.Application.Selection.Paste()

Related

Outlook VBA fails to save changes to MailItem on older items only

I have a macro in Outlook VBA that is designed to set the category of the selected email and send a reply email to the sender when a user clicks a button on the "Ribbon".
The below code will properly set the category on any newer emails, but throws "Run-time error '440': Cannot save this item." when run on emails that have been sitting around for a while. I don't know what causes this, as it runs fine for a while and then no longer works.
I tried restarting Outlook, logging in and logging out, and restarting the computer, and none of these seem to trigger the change in behavior, so I believe it is somehow related to the duration that the email has been sitting. This is rather confusing, as I don't see why length of time sitting in the folder should affect the ability to save, but not the ability to access the properties of the MailItem.
A simplified version of the code (without the email-sending part, which works fine) is as follows:
Public Sub UpdateCategory
Dim objItem as Object
Set objItem = GetCurrentItem()
'Verify that selected item is an email and an engineering request
If TypeName(objItem) = "MailItem" And InStr(LCase(objItem.Subject), "engineering request") > 0 Then
objItem.Categories = "Test"
objItem.Save
End If
Set objItem = Nothing
End Sub
Function GetCurrentItem() As Object
Dim objApp As Outlook.Application
Set objApp = Application
On Error Resume Next
Select Case TypeName(objApp.ActiveWindow)
Case "Explorer"
Set GetCurrentItem = objApp.ActiveExplorer.Selection.Item(1)
Case "Inspector"
Set GetCurrentItem = objApp.ActiveInspector.CurrentItem
End Select
Set objApp = Nothing
End Function
Any thoughts or suggestions would be greatly appreciated!!
Is this is an Exchange mailbox, it is possible that Exchange modified the item on the server side, that change was downloaded to the OST file, but since Outlook Object Model is not aware of the change, when you try to save the (stale) item, you get a conflict error.

Get Current Body via Active Explorer in Currently Sending Email

Been trying various approaches from either experimenting or googling but cant seem to figure this one out.
In this code, when the email is being sent from the explorer via a reply, it only grabs the body from below what is "being sent now" instead of a lof it. I assume there is an object I haven't been able to find intuitively that holds this but I cant seem to figure out.
Anyone know what I might be over looking?
Code used
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim prompt As String
Dim msg As MailItem
Set msg = GetCurrentItem()
prompt = msg.Body
MsgBox prompt
End Sub
Function GetCurrentItem() As Object
Dim objApp As Outlook.Application
Set objApp = Application
On Error Resume Next
Select Case TypeName(objApp.ActiveWindow)
Case "Explorer"
Set GetCurrentItem = objApp.ActiveExplorer.Selection.Item(1)
Case "Inspector"
Set GetCurrentItem = objApp.ActiveInspector.CurrentItem
End Select
Set objApp = Nothing
End Function

Displaying User Defined Property of MailItem in Outlook

I am attempting to add convenience when adding notes to emails in Outlook.
My plan is to take my current procedure, which adds the notes to the selected email (as an attachment), and have it call a procedure which will set a UserProperty on the MailItem object so that I can easily see which emails have notes attached by adding a custom column to my email list view.
From scouring the internet I have pieced together the following.
Option Explicit
Public Sub MarkHasNote()
Dim Selection As Outlook.Selection
Dim UserDefinedFieldName As String
Dim objProperty As Outlook.UserProperty
Dim objItem As MailItem
UserDefinedFieldName = "Note"
Set objItem = GetCurrentItem()
Set objProperty = objItem.UserProperties.Add(UserDefinedFieldName, Outlook.OlUserPropertyType.olYesNo, olFormatYesNoIcon)
objProperty.Value = True
End Sub
Function GetCurrentItem() As Object
Dim objApp As Outlook.Application
Set objApp = Application
On Error Resume Next
Select Case TypeName(objApp.ActiveWindow)
Case "Explorer"
Set GetCurrentItem = objApp.ActiveExplorer.Selection.Item(1)
Case "Inspector"
Set GetCurrentItem = objApp.ActiveInspector.CurrentItem
End Select
Set objApp = Nothing
End Function
I have set a breakpoint and checked the UserProperties of the MailItem. I see that the details are there and the value is set to "True". However, the email does not show the Yes/No icon in the "Note" column of the email pane of Outlook.
How do I get Outlook to show my user defined property value in the email pane when I add the column to the view?
A save is required for a selection. An Inspector item prompts for a save.
Private Sub MarkHasNote_DisplayTest()
' Add the UserProperty column with Field Chooser
' You can view the value toggling when you run through the code
Dim Selection As Selection
Dim UserDefinedFieldName As String
Dim objProperty As UserProperty
Dim objItem As mailItem
UserDefinedFieldName = "NoteTest"
Set objItem = GetCurrentItem()
Set objProperty = objItem.UserProperties.Add(UserDefinedFieldName, Outlook.OlUserPropertyType.olYesNo, olFormatYesNoIcon)
objProperty.Value = Not objProperty.Value
' Required for an explorer selection
objItem.Save
' For an inspector item there would be a prompt to save
' if not already done in the code
End Sub

How to select all text and change spelling language?

I need to select the entire text in the email I'm typing, and change the spelling language.
The following works in Word but doesn't in Outlook 2013.
I added Microsoft Word 15 Object library, with tools -> references, from the VBA editor window.
Selection.WholeStory
Selection.LanguageID = wdEnglishUK
Selection.NoProofing = False
Application.CheckLanguage = False
Methods and properties specific to one application's VBA language can't be used in other applications like that.
There is a lot of information out there, try a search for "Outlook VBA Change Message Body Language" and variations of that.
Some resources to get you started:
Stack Overflow: Outlook VBA Set language of selection
MSDN: Working with Item Bodies with Outlook/VBA
MSDN: MailItem Object (Outlook)
Stack Overflow: Display email body of selected email in Outlook as a message box in Excel
MSDN: Introduction to Outlook VBA
NoProofing should work as intended.
Option Explicit
Private Sub Proofing_EnglishUK()
Dim oMailItm As Object
Dim oInsp As Object
Dim oMailEd As Object
Dim oWord As Object
Dim Rng As Object
Set oInsp = ActiveInspector
If oInsp.currentItem.Class = olMail Then
Set oMailItm = oInsp.currentItem
If oInsp.EditorType = olEditorWord Then
Set oMailEd = oMailItm.GetInspector.WordEditor
Set oWord = oMailEd.Application
Set Rng = oWord.Selection
Rng.WholeStory
With Rng
.LanguageID = wdEnglishUK
' This should work as intended
'.NoProofing = False
' ******* temporary *************
' Check whether .NoProofing can be set
' with a spelling error somewhere in the mail
.NoProofing = Not .NoProofing
If .NoProofing = False Then
MsgBox "Proofing on. Errors should be found."
Else
MsgBox "Proofing off. The errors will not be found."
End If
' ******* temporary *************
End With
oMailItm.Save
End If
End If
Set Rng = Nothing
Set oWord = Nothing
Set oMailEd = Nothing
Set oMailItm = Nothing
End Sub

Whenever a mail is sent from Outlook 2010 that mail should move to dedicated folder in Outlook, using VBA/Macros

I have already wrote a code where the when ever a email comes from a definite email ID to outlook my ID it gets sent to anothee email address automatically by using one rule.
Now I have to add one feature. This mail which is sent is also saved in a dedicated folder other than sent items. All this in one script.
This my current script where only the mail goes automatically. Now I need help to add the dedicated folder feature.
Sub Project_1()
Dim objMail As Outlook.MailItem
Set objItem = GetCurrentItem()
Set objMail = objItem.Forward
objMail.To = "inbox#email.com"
objMail.Display
objMail.Send
Set objItem = Nothing
Set objMail = Nothing
End Sub
Function GetCurrentItem() As Object
Dim objApp As Outlook.Application
Set objApp = Application
On Error Resume Next
Select Case TypeName(objApp.ActiveWindow)
Case "Explorer"
Set GetCurrentItem = _
objApp.ActiveExplorer.Selection.Item(1)
Case "Inspector"
Set GetCurrentItem = _
objApp.ActiveInspector.CurrentItem
Case Else
End Select
End Function
I am very new to VBA.
As #destination-data stated above, the whole task will be much easier through outlooks rules.
This article is a step-by-step guide on how to set rules for moving certain mails based on conditions. Additionally you can easily add a "forward" rule, since all your desired functionality is already implemented into outlook and one should avoid rewriting existing tools.
According to your description, this should accomplish the exact same thing as intended by your code.