I am trying to get this code to work.
I want to select multiple emails from my inbox and send a auto reply using a template.
I am getting a run-time error: Object variable or With Block variable not set.
Any help would be appreciated. Also I would like to add a msg box telling me how many items were sent.
Option Explicit
Sub ReplywithTemplate()
Dim Item As Outlook.MailItem
Dim oRespond As Outlook.MailItem
For Each Item In ActiveExplorer.Selection
' This sends a response back using a template
Set oRespond = Application.CreateItemFromTemplate("C:\Users\Accounting\AppData\Roaming\Microsoft\Templates\scautoreply.oft")
With oRespond
.Recipients.Add Item.SenderEmailAddress
.Subject = Item.Subject
' includes the original message as an attachment
.Attachments.Add Item
' use this for testing, change to .send once you have it working as desired
.Display
End With
On Error Resume Next
Next
Set oRespond = Nothing
End Sub
I have noticed the following lines of code:
For Each oRespond In ActiveExplorer.Selection
' This sends a response back using a template
Set oRespond = Application.CreateItemFromTemplate("C:\Users\Accounting\AppData\Roaming\Microsoft\Templates\scautoreply.oft")
With oRespond
You need to use a new variable for creating an auto-reply email from a template because the selected Outlook item is missed (replaced with a newly created one).
So, basically you can create an item from a template, add recipients from the selected Outlook item and call the Send method. Or you can use the Reply method of the selected item in Outlook, copy the required properties from a template and call the Send method. It is up to you which way is to choose.
Finally, you may find the Getting Started with VBA in Outlook 2010 article helpful.
Related
I am trying to get a vba in outlook to recall the message currently selected.
the code I found is.
Option Explicit
Sub Recall()
Dim SendItem As Object
Dim olItem As Outlook.MailItem
Dim olInsp As Outlook.Inspector
'// Selected item in Sent Items folder
Set SendItem = ActiveExplorer.Selection.Item(1)
If TypeName(SendItem) = "MailItem" Then
Set olItem = SendItem
Set olInsp = olItem.GetInspector
'// Execute Recall command button
With olInsp
.Display
.CommandBars.FindControl(, 2511).Execute
.Close olDiscard
End With
End If
End Sub
I am getting an error on the line of .CommandBars.FindControl(, 2511).Execute.
How should the code be modified?
Command bars were deprecated and not used any longer. The Fluent UI (aka Ribbon UI) is used, so any old code may not work any longer. Only some methods are supported such as ExecuteMso which allows to execute buult-in ribbon controls. The CommandBars.ExecuteMso methods accepts a string which represents the identifier for the control. You need to pass the ribbon idMso value of the control instead.
CommandBars.ExecuteMso("RecallThisMessage")
But you need to make sure that such controls are available in Outlook. Message recall is available after you click Send and is available only if both you and the recipient have a Microsoft 365 or Microsoft Exchange email account in the same organization. So, for example, a message sent to or from a Hotmail, Gmail, or live.com account can't be recalled.
I am completely clueless about VBA for outlook. I was hoping that I could record macros, but I see that its not possible.
So the issue I am having is that I receive close to 100 emails daily that arrive in plain text, and I always need to convert to HTML so that I can select a Quick Parts, then send the email.
Can this be automated with VBA?
The MailItem.BodyFormat property allows setting an OlBodyFormat constant indicating the format of the body text. For example:
Sub CreateHTMLMail()
'Creates a new email item and modifies its properties.
Dim objMail As MailItem
'Create mail item
Set objMail = Application.CreateItem(olMailItem)
With objMail
'Set body format to HTML
.BodyFormat = olFormatHTML
.HTMLBody = "<HTML><H2>The body of this message will appear in HTML.</H2><BODY>Type the message text here. </BODY></HTML>"
.Display
End With
End Sub
To handle incoming emails you may use the NewMailEx event of the Application class. The NewMailEx event fires when a new message arrives in the Inbox and before client rule processing occurs. You can use the Entry ID returned in the EntryIDCollection array to call the NameSpace.GetItemFromID method and process the item.
Can anyone help me with editing the VBA-Code for the following Problem:
I want to forward e-mails with a specific subject to an specific E-Mail. In this process i want to add a text to the forwarded body.
Thank's for your help!
edit.
I have the code now, but it doesn't work properly. It sends the last E-Mail clicked on :(.
Sub Test(oMail As MailItem)
Dim MyItem As Outlook.MailItem
Dim obj_curitem As MailItem
Dim obj_newitem
Dim obj_Selection
Dim obj_curfolder
Dim obj_msgitems
Dim Forward As Object
If Err.Number = 0 Then
Set obj_Selection = Outlook.ActiveExplorer.Selection
If obj_Selection.Count > 0 Then
For Each obj_curitem In obj_Selection
strID = obj_curitem.EntryID
Set olNS = Application.GetNamespace("MAPI")
'Object auf einem neuen Item erstellen
Set obj_newitem = obj_curitem.Forward
With obj_curitem.Forward
.Forward = True
.SentOnBehalfOfName = "###" 'Deine Mailadresse
.Subject = "WG" & .Subject 'Betreff
.To = "###" 'Empfängermail
.BODY = "geprüft" & .BODY 'E-Mail Inhalt
.Send
End With
Next
End If
End If
End Sub
In general you will need to handle the NewMailEx event of the Application class where you can check out the Subject property and decide whether to forward the email or not. The Forward method of the Application class allows you doing so - it executes the Forward action for an item and returns the resulting copy as a MailItem object.
This NewMailEx event fires once for every received item that is processed by Microsoft Outlook. The item can be one of several different item types, for example, MailItem, MeetingItem, or SharingItem. The EntryIDsCollection string contains the Entry ID that corresponds to that item. You can use the Entry ID returned in the EntryIDCollection array to call the NameSpace.GetItemFromID method and process the item.
The Outlook object model provides three main ways for working with item bodies:
Body.
HTMLBody.
The Word editor. The WordEditor property of the Inspector class returns an instance of the Word Document which represents the message body. So, you can use the Word object model do whatever you need with the message body.
See Chapter 17: Working with Item Bodies for more information.
I am looking for a VB Script that will "phone home" if the document is open. I have created an empty macro named AutoOpen which executes properly when the document is open.
I would like to collect the time, current user logged in, and computer name and then automatically send an email address with that information. Basically to see who is opening that document.
Is there a way to do that with VB in word?
I haven't seen any php like function calls that send an email out for example mail("blahblah#mail.com", "mysubject", "my text"); That is kind of what I am looking for but in VB
Here is my email code that I use ALL the time.
Sub EmailCopy()
Dim oApp, oMail As Object
On Error Resume Next
Set oApp = CreateObject("Outlook.Application")
Set oMail = oApp.CreateItem(0)
With oMail
.To = "Someone#Somewhere.com.au"
.Subject = "My Subject Title"
.Body = "Here is the information you asked for"
.Send
End With
Set oMail = Nothing
Set oApp = Nothing
End Sub
Try to implement it into your code and post back if you get stuck. You will need a reference to outlook (I am assuming you are sending via outlook? if not you need to use the CDO method posted by Mat's Mug)
Also maybe remove the vbscript tag from your question, this is VBA :)
I found the below code from a very helpful post by user thommck. It forwards selected items as attachments in separate emails to a specified recipient.
When I use the code, the forward symbol does not appear on the email icon of the email I just forwarded. If I use the regular Outlook method for "Forward as Attachment," the symbol is added to the envelope icon in my viewing pane.
Any ideas on how to get this forward symbol to appear when using this code?
Sub ForwardSelectedItems()
On Error Resume Next
Dim objItem As Outlook.MailItem
If Application.ActiveExplorer.Selection.Count = 0 Then
MsgBox ("No item selected")
Exit Sub
End If
For Each objItem In Application.ActiveExplorer.Selection
Set objMsg = Application.CreateItem(olMailItem)
With objMsg
.Attachments.Add objItem, olEmbeddeditem
.Subject = "enter text"
.To = "example#example.com"
.Send
End With
Next
Set objItem = Nothing
Set objMsg = Nothing
End Sub
You need to use the Forward method of the MailItem class instead of creating a new mail item in the code:
Application.CreateItem(olMailItem)
should be replaced with:
objItem.Foward()
Note, you need to clear the message body if you don't want to see the content of the attached item.
You may find the Getting Started with VBA in Outlook 2010 article helpful.
You can simulate pressing a button with ExecuteMso.
https://msdn.microsoft.com/en-us/library/office/ff862419%28v=office.15%29.aspx
expression.ExecuteMso(idMso)
expression An expression that returns a CommandBars object.
To find the idMso, go through the process of adding a button to the Quick Access Toolbar or a ribbon. Once you find the control in the list, hover over it until the tooltip appears. The idMso is the name in brackets.
In this case it is ForwardAsAttachment