I'm working on a small macro to send mail from excel 2007 using my Lotus Notes session.
The sending mail part is working fine.
Now I need to send in the body part, a part of a stylesheet (for instance the area from A1:B20). This area has colors, bold font.
To send my email here is the code:
Set oSess = CreateObject("Notes.NotesSession")
Set oDB = oSess.GETDATABASE("", "")
Call oDB.OPENMAIL
flag = True
If Not (oDB.IsOpen) Then flag = oDB.Open("", "")
If Not flag Then
MsgBox "Can't open mail file: " & oDB.SERVER & " " & oDB.FILEPATH
End If
On Error GoTo err_handler
'Building Message
Set oDoc = oDB.CREATEDOCUMENT
Set oItem = oDoc.CREATERICHTEXTITEM("BODY")
oDoc.Form = "Memo"
'mail subject
oDoc.Subject = "subject"
'mail body
oDoc.sendto = "toto#myserver.com"
oDoc.body = "my text"
oDoc.postdate = Date
oDoc.SaveMessageOnSend = True
oDoc.visable = True
'Sending Message
oDoc.SEND False
Does anybody has an idea about how to send a stylesheet ?
You can try the solution described in this SO question : Sending formatted Lotus Notes rich text email from Excel VBA
Regards,
Max
Related
Good day,
I'm trying to automate my outlook.
I have an excel list with mails and a code is composing drafts for me.
But here is a problem when I try to send a mail in web version it states
error: "This message can't be sent right now. Please try again later."
Mails appear correct in desktop version, but not in web.
Sub Box()
Dim objOL As Object
Set objOL = CreateObject("Outlook.Application")
Dim Name As Namespace
Set Name = objOL.GetNamespace("MAPI")
Dim Msg As MailItem
Set Msg = objOL.CreateItemFromTemplate
Msg.To = Cond.Cells(i, 2).Text
Msg.CC = Cond.Cells(i, 3).Text
Msg.Subject = PP.Range("F1").Text
Msg.HTMLBody = "<HTML><BODY>" & Cond.Cells(i, 4) & Cond.Cells(i, 5) & "</BODY></HTML>"
I've tried to. property I've tried Recipient.Add brackets <>.
Can you help me if you know how, please
First, you need to specify the file path of the template:
Set Msg = objOL.CreateItemFromTemplate
The Application.CreateItemFromTemplate method creates a new Microsoft Outlook item from an Outlook template (.oft) and returns the new item. It requires at least one parameter passed - the path and file name of the Outlook template for the new item.
Second, to set up To and CC properties:
Msg.To = Cond.Cells(i, 2).Text
Msg.CC = Cond.Cells(i, 3).Text
These properties expect to be set to a semicolon-delimited string list of display names for the To or CC recipients for the Outlook item. Make sure that you pass a valid string.
A better approach is to use the Recipients property of Outlook items. The Recipients.Add method creates a new recipient in the Recipients collection. Then don't forget to use the Resolve or ResolveAll methods. They attempt to resolve one or all the Recipient objects in the Recipients collection against the Address Book, for example:
' now we add new recipietns to the e-mail
recipientTo = recipients.Add("Eugene Astafiev")
recipientTo.Type = Outlook.OlMailRecipientType.olTo
recipientCC = recipients.Add("Someone Else")
recipientCC.Type = Outlook.OlMailRecipientType.olCC
recipientBCC = recipients.Add("eugene.astafiev#somedomain.com")
recipientBCC.Type = Outlook.OlMailRecipientType.olBCC
retValue = recipients.ResolveAll()
Read more about that in article that I wrote for the technical blog - How To: Fill TO,CC and BCC fields in Outlook programmatically.
I am trying to create a macro which will open a new lotus notes mail, attaches certain files to it and copy and paste the body from a stationery mail.
I am stuck in the last part where I have to copy the BODY of the stationery mail and paste it in the new mail which I have created.
Below is the code which I have tried, but only the files are getting attached but unable to copy the body from the stationery
Any Help is appreciated........
isattached = False
Application.ScreenUpdating = False
'Start Lotus Notes Session
Set nSession = CreateObject("Notes.NotesSession")
On Error GoTo err_send
Set nMailDb = nSession.GetDatabase("", "mailin/GBG180.nsf")
' Open the Stationery View
Set nView = nMailDb.GetView("Stationery")
Set nWorkspace = CreateObject("Notes.NotesUIWorkspace")
Set nCursor = nView.GetFirstDocument
Do While Not nCursor Is Nothing
' get stationery value
StationeryName = nCursor.GetItemValue("MailStationeryName")(0)
' match form template selection versus stationery
If StationeryName = St_name Then
Set nMailDoc = nMailDb.CreateDocument("Memo")
Set AttachME = nMailDoc.CreateRichTextItem("ATTACHMENT")
'Open the PDF folder and attach the files
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(PdfPath)
For Each objFile In objFolder.Files
Set EmbedObj = AttachME.EmbedObject(1454, "", PdfPath & objFile.Name)
isattached = True
Application.Wait (15)
Next
subjectname = policy_num & "-" & aname & "-FoS Invoice & Wording" & " " & Format(DateValue(Now()), "YYYY") & "- Confidential"
If (isattached) Then
'Set nMailDoc = nWorkspace.EDITDOCUMENT(True, nCursor)
nMailDoc.Subject = subjectname
Else
Set nMailDoc = Nothing
Set AttachME = Nothing
Exit Function
End If
nMailDoc.Save True, True, False
Set nMailDoc = Nothing
Set nCursor = Nothing
GoTo nMail_OK
Else
Set nCursor = nView.GetNextDocument(nCursor)
End If
Loop
In Domino there are Default item names to store data in. The Body (inlcuding all attachments) in a mail is always called "Body", and not "ATTACHMENTS" as in your example.
You need to replace these lines
Set AttachME = nMailDoc.CreateRichTextItem("ATTACHMENT")
'Open the PDF folder and attach the files
...
with these
Dim bodyStationary as NotesRichtextItem
'- get the richtext from the stationary
Set bodyStationary = nCursor.GetFirstItem( "Body" )
'- create richtextitem in new document
Set AttachME = nMailDoc.CreateRichTextItem("Body")
'- append the stationary
Call AttachMe.AppendRTItem( bodyStationary )
'- add the attachments
'Open the PDF folder and attach the files
...
If you want to have the attachments above the text of the stationary, just move the AppendRTItem below the Foreach- loop.
Don't forget to add Attachme.AddNewline( 2 ) in that case, as otherwise the text will directly follow the attachments.
I created a macro in Excel to send emails to various users every time a specific file is updated.
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim answer As String
answer = MsgBox("Would you like to save the changes?", vbYesNo, "Save Document")
If answer = vbNo Then Cancel = True
If answer = vbYes Then
'open outlook type stuff
Set OutlookApp = CreateObject("Outlook.Application")
Set OlObjects = OutlookApp.GetNamespace("MAPI")
Set newmsg = OutlookApp.CreateItem(olMailItem)
'add recipients
'newmsg.Recipients.Add ("Name1")
newmsg.Recipients.Add ("email#xxx.com")
'newmsg.Recipients.Add ("Name2")
newmsg.Recipients.Add ("email#xxx.com")
'add subject
newmsg.Subject = "Notification - Update file"
'add body
newmsg.Body = "This is an automated notification." & vbNewLine & vbNewLine & _
"The XXX file has been recently updated" & vbNewLine & vbNewLine & _
"Please do not reply to this email."
newmsg.Display 'display
newmsg.Send 'send message
'give conformation of sent message
MsgBox "Your document has successfully been saved", , "Confirmation"
End If
'save the document
'Me.Worksheets.Save
End Sub
I would like to add a hyperlink to the body text where it says "The XXX file has been recently updated" so that XXX file is a clickable link to a website.
The Outlook object model supports three main ways of customizing the message body:
The Body property returns or sets a string representing the clear-text body of the Outlook item.
The HTMLBody property of the MailItem class returns or sets a string representing the HTML body of the specified item. Setting the HTMLBody property will always update the Body property immediately. For example:
Sub CreateHTMLMail()
'Creates a new e-mail item and modifies its properties.
Dim objMail As Outlook.MailItem
'Create e-mail item
Set objMail = Application.CreateItem(olMailItem)
With objMail
'Set body format to HTML
.BodyFormat = olFormatHTML
.HTMLBody = "<HTML><BODY>Enter the message text here. </BODY></HTML>"
.Display
End With
End Sub
The Word object model can be used for dealing with message bodies. See Chapter 17: Working with Item Bodies for more information.
Note, the MailItem.BodyFormat property allows you to programmatically change the editor that is used for the body of an item.
The last two supports creating a hyperlink in the message body. It is up to you which way is to choose.
If you want to do that, you'll have to write HTML instead of plain text.
This line:
newmsg.Body = "The XXX file has been recently updated"
... would become something like:
newMsg.HTMLBody = "The XXX file has been recently updated".
This is because in Outlook emails with formatting you write HTML text, and a link in HTML is expressed as follows:
your Hyper-text
I send emails via Outlook using ".HTMLBody = ..." to customize body.
I tried to add these lines to add an image that I named "logo" in the worksheet:
With Sheets("Mail - GREF COMMITTEE AGENDA")
.Shapes("logo").Export "C:\Users\Public\Pictures\logo.png"
End With
'...
.HTMLBody = bodyEn & bodyFR & "< img src='C:\Users\Public\Pictures\logo.png'>"
The error is:
438 Object doesn't support this method or property.
Update: Any way to embed the image in the mail is helpful. I can export the image manually.
Use Word's InlineShapes.AddPicture Method
The procedure is described in Working with Item Bodies.
Inserting pictures:
strFile = "C:\Pictures\logo.gif"
Set objInsp = objMsg.GetInspector
Set objDoc = objInsp.WordEditor
Set objSel = objDoc.Windows(1).Selection
If objMsg.BodyFormat <> olFormatPlain Then
objSel.InlineShapes.AddPicture strFile, False, True
End If
I would like to know how to Open an Excel attachment in my inbox Outlook 2010 with VBA code.
I would like the code to:
Check for a specific subject that does not change "Test"
Check if that email is read or unread and if it is unread to use that one
I have a rule in place where the emails are stored in a subfolder based on the subject but I can change it to go back to the main inbox
I would really appreciate if you can explain what the code is doing as I am not familiar with the Outlook connection bit.
This is what I have pulled together from various sites including stackoverflow it does the job.
It seems to run for emails that have RE: in the subject for the 10PM and 5PM emails. I explicitly named subject of the two emails. Can anyone help me?
I am not familiar with declaring variables for Outlook objects. Have I declared the variables correctly?
I would also like to copy the data within each file and paste it into another workbook. I would like the data to be pasted underneath the first data from each work book so it will need to find the last row and paste it 1 row below the last row for each data on each workbook onto the open workbook which is stored in another path.
.
Sub DownloadAttachmentFirstUnreadEmail()
Const olFolderInbox = 6
Const AttachmentPath As String = "C:\My Documents\Outlook Test\"
Dim oOlAtch As Object
Set objOutlook = CreateObject("Outlook.Application")
Set objNamespace = objOutlook.GetNamespace("MAPI")
Set objFolder = objNamespace.GetDefaultFolder(olFolderInbox)
Set objFolder = objFolder.Folders("**CLIENT ISSUES**").Folders("*Daily Reports").Folders("1. Open Trade Report")
Set colItems = objFolder.Items
Set colFilteredItems1 = colItems.Restrict("[Unread] = True AND [Subject] = '10PM FXC Email notification for Martin Currie'")
Set colFilteredItems2 = colItems.Restrict("[Unread] = True AND [Subject] = 'FXC Email notification for Martin Currie Funds'")
'~~> Check if there are any actual unread 10PM FXC emails
If colFilteredItems1.Count = 0 Then
MsgBox "NO Unread 10PM Email In Inbox"
Else
'~~> Extract the attachment from the 1st unread email
For Each colItems In colFilteredItems1
'~~> Check if the email actually has an attachment
If colItems.Attachments.Count <> 0 Then
For Each oOlAtch In colItems.Attachments
'~~> save the attachment and open them
oOlAtch.SaveAsFile AttachmentPath & oOlAtch.Filename
Set wb = Workbooks.Open(Filename:=AttachmentPath & oOlAtch.Filename)
Next oOlAtch
Else
MsgBox "10PM email doesn't have an attachment"
End If
Next colItems
End If
'~~> Check if there are any actual unread FXC Email emails
If colFilteredItems2.Count = 0 Then
MsgBox "NO Unread 5PM Email In Inbox"
Else
'~~> Extract the attachment from the 1st unread email
For Each colItems In colFilteredItems2
'~~> Check if the email actually has an attachment
If colItems.Attachments.Count <> 0 Then
For Each oOlAtch In colItems.Attachments
'~~> save the attachment and open them
oOlAtch.SaveAsFile AttachmentPath & oOlAtch.Filename
Set wb = Workbooks.Open(Filename:=AttachmentPath & oOlAtch.Filename)
Next oOlAtch
Else
MsgBox "5PM email doesn't have an attachment"
End If
Next colItems
End If
End Sub
First of all, I'd suggest starting from the Getting Started with VBA in Outlook 2010 article in MSDN.
You can assign a VBA macro to the Outlook rule, it should look like the following one:
public sub test(mail as MailItem)
'
end sub
where you can check out the mail object. It looks like you need to check out the Subject, UnRead and Attachments properties of the MailItem class.