I have found this code to download emails and convert them to PDF. It works perfectly but the only question that I have is it possible to remove the save notification and that it saves it automaticly when you trigger the macro?
Below is whole my code:
Set objAttachments = Nothing
Set objMsg = Nothing
Set objSelection = Nothing
Set objOL = Nothing
Set MyOlNamespace = Application.GetNamespace("MAPI")
Set MySelectedItem = ActiveExplorer.Selection.Item(1)
Set fso = CreateObject("Scripting.FileSystemObject")
'tmpFileName = FSO.GetSpecialFolder(2)
tmpFileName = "C:\CRM\Postboek\Ongekoppeld"
strRecieved = MySelectedItem.ReceivedByName
strSender = MySelectedItem.SenderName
strDatum = MySelectedItem.ReceivedTime
strDatum = Replace(strDatum, ":", "-")
strDatum = Replace(strDatum, "/", "-")
strName = "email_temp.mht"
tmpFileName = tmpFileName & "\" & strName
MySelectedItem.SaveAs tmpFileName, 10
On Error Resume Next
' If MySelectedItem.BodyFormat <> olFormatHTML Then
' strDeletedFiles = strDeletedFiles & vbCrLf & "<file://" & strFile & ">"
' Else
' strDeletedFiles = strDeletedFiles & "<br>" & "<a href='file://" & _
'' strFile & "'>" & strFile & "</a>"
' End If
' If MySelectedItem.BodyFormat <> olFormatHTML Then
' MySelectedItem.Body = vbCrLf & "The file(s) were saved to " & strDeletedFiles & vbCrLf & MySelectedItem.Body
' Else
' MySelectedItem.HTMLBody = "<p>" & "The file(s) were saved to " & strDeletedFiles & "</p>" & MySelectedItem.HTMLBody
' End If
Set wrdApp = GetObject(, "Word.Application")
If Err Then
Set wrdApp = CreateObject("Word.Application")
bStarted = True
End If
On Error GoTo 0
Set wrdDoc = wrdApp.Documents.Open(FileName:=tmpFileName, Visible:=False, Format:=7)
Set dlgSaveAs = wrdApp.FileDialog(msoFileDialogSaveAs)
Set fdfs = dlgSaveAs.Filters
i = 0
For Each fdf In fdfs
i = i + 1
If InStr(1, fdf.Extensions, "pdf", vbTextCompare) > 0 Then
Exit For
End If
Next fdf
dlgSaveAs.FilterIndex = i
Set WshShell = CreateObject("WScript.Shell")
'SpecialPath = WshShell.SpecialFolders(16)
SpecialPath = "C:\CRM\Postboek\Ongekoppeld"
msgFileName = MySelectedItem.Subject
msgFileName = Replace(msgFileName, ":", "-")
Set oRegEx = CreateObject("vbscript.regexp")
oRegEx.Global = True
oRegEx.Pattern = "[\/:*?""<>|]"
'msgFileName = Trim(oRegEx.Replace(msgFileName, ""))
'msgFileName = Trim(oRegEx.Replace(msgFileName, ""))
If Len(strRecieved) = 0 Then
dlgSaveAs.InitialFileName = SpecialPath & "\" & msgFileName & " - " & strSender & " - " & strDatum
ElseIf Len(strRecieved) > 0 Then
dlgSaveAs.InitialFileName = SpecialPath & "\" & msgFileName & " - " & strRecieved & " - " & strDatum
End If
'dlgSaveAs.InitialFileName = SpecialPath & "\" & msgFileName & " - " & strRecieved & " - " & strDatum
If dlgSaveAs.Show = -1 Then
strCurrentFile = dlgSaveAs.SelectedItems(1)
If Right(strCurrentFile, 4) <> ".pdf" Then
Response = MsgBox("Sorry, only saving in the pdf-format is supported." & _
vbNewLine & vbNewLine & "Save as pdf instead?", vbInformation + vbOKCancel)
If Response = vbCancel Then
wrdDoc.Close 0
If bStarted Then wrdApp.Quit
Exit Sub
ElseIf Response = vbOK Then
intPos = InStrRev(strCurrentFile, ".")
If intPos > 0 Then
strCurrentFile = Left(strCurrentFile, intPos - 1)
End If
strCurrentFile = strCurrentFile & ".pdf"
End If
End If
wrdApp.ActiveDocument.ExportAsFixedFormat OutputFileName:= _
strCurrentFile, _
ExportFormat:=17, _
OpenAfterExport:=False, _
OptimizeFor:=0, _
Range:=0, _
From:=0, _
To:=0, _
Item:=0, _
IncludeDocProps:=True, _
KeepIRM:=True, _
CreateBookmarks:=0, _
DocStructureTags:=True, _
BitmapMissingFonts:=True, _
UseISO19005_1:=False
End If
Set dlgSaveAs = Nothing
wrdDoc.Close
If bStarted Then wrdApp.Quit
Set MyOlNamespace = Nothing
Set MySelectedItem = Nothing
Set wrdDoc = Nothing
Set wrdApp = Nothing
Set oRegEx = Nothing
End Sub
This part gives the notification to the user if they want to save it and I actually just want it removed:
If dlgSaveAs.Show = -1 Then
strCurrentFile = dlgSaveAs.SelectedItems(1)
If Right(strCurrentFile, 4) <> ".pdf" Then
Response = MsgBox("Sorry, only saving in the pdf-format is supported." & _
vbNewLine & vbNewLine & "Save as pdf instead?", vbInformation + vbOKCancel)
If Response = vbCancel Then
wrdDoc.Close 0
If bStarted Then wrdApp.Quit
Exit Sub
ElseIf Response = vbOK Then
intPos = InStrRev(strCurrentFile, ".")
If intPos > 0 Then
strCurrentFile = Left(strCurrentFile, intPos - 1)
End If
strCurrentFile = strCurrentFile & ".pdf"
End If
End If
Here is a screenshot of what I want to be removed:
screenshot
Remove this part:
If Len(strRecieved) = 0 Then
dlgSaveAs.InitialFileName = SpecialPath & "\" & msgFileName & " - " & strSender & " - " & strDatum
ElseIf Len(strRecieved) > 0 Then
dlgSaveAs.InitialFileName = SpecialPath & "\" & msgFileName & " - " & strRecieved & " - " & strDatum
End If
'dlgSaveAs.InitialFileName = SpecialPath & "\" & msgFileName & " - " & strRecieved & " - " & strDatum
If dlgSaveAs.Show = -1 Then
strCurrentFile = dlgSaveAs.SelectedItems(1)
If Right(strCurrentFile, 4) <> ".pdf" Then
Response = MsgBox("Sorry, only saving in the pdf-format is supported." & _
vbNewLine & vbNewLine & "Save as pdf instead?", vbInformation + vbOKCancel)
If Response = vbCancel Then
wrdDoc.Close 0
If bStarted Then wrdApp.Quit
Exit Sub
ElseIf Response = vbOK Then
intPos = InStrRev(strCurrentFile, ".")
If intPos > 0 Then
strCurrentFile = Left(strCurrentFile, intPos - 1)
End If
strCurrentFile = strCurrentFile & ".pdf"
End If
End If
Set the file name you want below:
wrdApp.ActiveDocument.ExportAsFixedFormat OutputFileName:= _
___PUTYOURFILENAMEHERE___, _
ExportFormat:=17, _
OpenAfterExport:=False, _
OptimizeFor:=0, _
Range:=0, _
From:=0, _
To:=0, _
Item:=0, _
IncludeDocProps:=True, _
KeepIRM:=True, _
CreateBookmarks:=0, _
DocStructureTags:=True, _
BitmapMissingFonts:=True, _
UseISO19005_1:=False
Related
I am trying to modify some code to retrieve any links in powerpoint slides and print them to a document. I am struggling to get the objects.
Here is what I have already:
Sub LinkCounter()
Dim FileNum As Integer
Dim oFile As String
Dim textLink() As Shape, i As Long
FileNum = FreeFile()
oFile = ActivePresentation.Path & "\LinksReport.txt"
If Dir(oFile, vbNormal) <> vbNullString Then
Kill oFile
End If
i = 0
p = 1
Open oFile For Append As #FileNum
Print #FileNum, "Links counted on slides"
For Each Slide In ActivePresentation.Slides
Print #FileNum, "Slide"; p
p = p + 1
For Each Hyperlinks.Address In Slide.Hyperlinks
Set textLink(i) = Hyperlinks.Address
Print #FileNum, textLink(i)
i = i + 1
Next Hyperlinks.Address
Next Slide
Close FileNum
End Sub
Any help would be appreciated!
After some deeper searching I found a piece of code that achieves this, it would be good to know where I went wrong however, I'm guessing I need to loop through shapes to find the links?
Sub PPHyperlinkReport()
Dim oSl As Slide
Dim oHl As Hyperlink
Dim sReport As String
Dim iFileNum As Integer
Dim sFileName As String
For Each oSl In ActivePresentation.Slides
For Each oHl In oSl.Hyperlinks
If oHl.Type = msoHyperlinkShape Then
If oHl.Address <> "" Then
sReport = sReport & "HYPERLINK IN SHAPE" _
& vbCrLf _
& "Slide: " & vbTab & oSl.SlideIndex _
& vbCrLf _
& "Shape: " & vbTab & oHl.Parent.Parent.Name _
& vbCrLf _
& "Text: """ & oHl.Parent.Parent.TextFrame.TextRange.Text & """" _
& vbCrLf _
& "External link address:" & vbTab & oHl.Address & vbCrLf & vbNewLine & vbNewLine
Else
sReport = sReport & ""
End If
Else
If oHl.Address <> "" Then
sReport = sReport & "HYPERLINK IN TEXT" _
& vbCrLf _
& "Slide: " & vbTab & oSl.SlideIndex _
& vbCrLf _
& "Shape: " & vbTab & oHl.Parent.Parent.Parent.Parent.Name _
& vbCrLf _
& "Text: """ & oHl.Parent.Parent.Text & """" _
& vbCrLf _
& "External link address:" & vbTab & oHl.Address & vbCrLf & vbNewLine & vbNewLine
Else
sReport = sReport & ""
End If
End If
Next ' hyperlink
Next ' Slide
iFileNum = FreeFile()
sFileName = ActivePresentation.Path & "\AuthorTec_Edits.txt"
Open sFileName For Output As iFileNum
Print #iFileNum, sReport
Close #iFileNum
Call Shell("NOTEPAD.EXE " & sFileName, vbNormalFocus)
End Sub
I am having difficulty adding an appointment to a coworkers calendar that they shared with me. The problem appears to be in the calendar reference. My appointments keep adding to their main default calendar while I am trying to add them to a separate shared calendar named "Study Schedule". I am running office 365.
Dim olApp As Outlook.Application
Dim olappt As Outlook.AppointmentItem
Dim bAppOpened As Boolean
Dim myNamespace As Outlook.NameSpace
Dim objRecip As Outlook.Recipient
Dim strName As String
Dim myFolder As Outlook.Folder
Const olAppointmentItem = 1
On Error Resume Next
Set olApp = GetObject(, "Outlook.Application")
If Err.Number <> 0 Then
Err.Clear
Set olApp = CreateObject("Outlook.Application")
bAppOpened = False ' Outlook was not already running, started it
Else
bAppOpened = True ' Outlook was already running
End If
' On Error GoTo Error_Handler
' Get Study Schedule Folder Location
Set myNamespace = olApp.GetNamespace("MAPI")
Set objRecip = myNamespace.CreateRecipient("John Doe")
objRecip.Resolve
' I believe the problem is in the two lines of code below as I try to reference non default folder (shared from john doe)
Set myFolder = myNamespace.GetSharedDefaultFolder(objRecip, olFolderCalendar)
Set myFolder = myFolder.Folders("Study Schedule")
myFolder.Display
Set olappt = myFolder.Items.Add
'Set olappt = myNewFolder.Items.Add
With olappt
.AllDayEvent = True
.Start = ScheduledDate
.Subject = StudyName
.Body = "Study has been scheduled." & vbCr & _
vbCr & _
"Calendar Assigned: " & myFolder & vbCr & _
"Schedule Entry ID: " & ScheduleEntryID & vbCr & _
"Study Name: " & StudyName & vbCr & _
"Scheduled Date: " & ScheduledDate & vbCr & _
vbCr & _
"Principle Investigator: " & PrincipleInvestigator & vbCr & _
"Order Placed By: " & OrderPlacedBy & vbCr & _
vbCr & _
"Species: " & Spec
ies & vbCr & _
"Strain: " & Strain & vbCr & _
"Sex " & Sex & vbCr & _
"Age: " & Age & vbCr & _
"Weight: " & Weight & " Kg" & vbCr & _
"Quantity : " & Quantity & vbCr & _
vbCr & _
"Study Information: " & StudyDescription & vbCr & _
vbCr & _
"This Event was auto generated from the Scheduling Assistant and In-Vivo Database."
.Location = ""
.Display
' .Save
' .Send
End With
... Rest of Code
Any help is greatly appreciated!
It is likely the shared calendar is at the same level as the default calendar.
' For a folder at the same level as the default calendar
' navigate up then back down
Set myFolder = myNamespace.GetSharedDefaultFolder(objRecip, olFolderCalendar)
Set myFolder = myFolder.Parent.Folders("Study Schedule")
I found a work around. The code I ended up going with is posted Below. Thank you everyone for the rapid responses! I really appreciated the help.
Dim olApp As Outlook.Application
Dim olappt As Outlook.AppointmentItem
Dim bAppOpened As Boolean
Dim myNamespace As Outlook.NameSpace
Dim myFolder As Outlook.Folder
Dim objPane As Outlook.NavigationPane
Dim objModule As Outlook.CalendarModule
Dim CalFolder As Outlook.Folder
Const olAppointmentItem = 1
On Error Resume Next
Set olApp = GetObject(, "Outlook.Application")
If Err.Number <> 0 Then
Err.Clear
Set olApp = CreateObject("Outlook.Application")
bAppOpened = False ' Outlook was not already running, started it
Else
bAppOpened = True ' Outlook was already running
End If
' On Error GoTo Error_Handler
On Error GoTo 0
Set objPane = Outlook.Application.ActiveExplorer.NavigationPane
Set objModule = objPane.Modules.GetNavigationModule(olModuleCalendar)
With objModule.NavigationGroups
For g = 1 To .Count
Set objGroup = .Item(g)
For i = 1 To objGroup.NavigationFolders.Count
Set objNavFolder = objGroup.NavigationFolders.Item(i)
If objNavFolder = "Study Schedule" Or objNavFolder = "John Doe - Study Schedule" Then
Set CalFolder = objNavFolder.Folder
MsgBox CalFolder
End If
Next
Next
End With
Set olappt = CalFolder.Items.Add
With olappt
.Display
.AllDayEvent = True
.Start = ScheduledDate
.Subject = StudyName
.Body = "Study has been scheduled." & vbCr & _
vbCr & _
"Schedule Entry ID: " & ScheduleEntryID & vbCr & _
"Study Name: " & StudyName & vbCr & _
"Scheduled Date: " & ScheduledDate & vbCr & _
vbCr & _
"Principle Investigator: " & PrincipleInvestigator & vbCr & _
"Order Placed By: " & OrderPlacedBy & vbCr & _
vbCr & _
"Species: " & Species & vbCr & _
"Strain: " & Strain & vbCr & _
"Sex " & Sex & vbCr & _
"Age: " & Age & vbCr & _
"Weight: " & Weight & " Kg" & vbCr & _
"Quantity : " & Quantity & vbCr & _
vbCr & _
"Study Information: " & StudyDescription & vbCr & _
vbCr & _
"This Event was auto generated from the Scheduling Assistant and In-Vivo Database."
.Location = ""
.Display
' .Save
' .Send
End With
Is it possible to open an existing application window?
What I want:
What is the code in order to put focus on an already open, but not in focus, application. For example, with:
Set objIE = New InternetExplorer
but I want the macro to put focus on an already existing IE.
Here is another case, I let Lotus notes create an email with the following code:
Sub Email_Bot()
'variables are defined
Dim Maildb As Object
Dim UserName As String
Dim MailDbName As String
Dim MailDoc As Object
Dim Session As Object
Dim AttachME As Object
Dim EmbedObj As Object
Dim Attachment As String
Dim stAttachment As String
Dim Mail_Form As String
Dim Mail_SendTo As String
Dim Mail_Subject As String
Dim Mail_Body As String
Dim Mail_Attachement As Boolean
Dim Mail_Save As Boolean
Dim Mail_Send As Boolean
Dim Mail_Name As String
Dim Mail_Text_1 As String
Dim Mail_Text_2 As String
Dim Mail_Text_3 As String
Dim Mail_Text_4 As String
Dim Mail_Text_5 As String
Dim Mail_Text_6 As String
Dim Mail_Closing As String
Dim Mail_SendBy As String
Dim tb_Mailing_List As Object
Dim tb_Email_Template As Object
Dim LastRow As Integer
Dim Row_Count As Integer
Dim Mail_Body_Lock As Boolean
Dim Workspace As Object
Const EMBED_ATTACHMENT As Long = 1454
'worksheets are defined
Set tb_Mailing_List = ThisWorkbook.Sheets("Mailing List")
Set tb_Email_Template = ThisWorkbook.Sheets("Email Template")
'mail session is defined
Set Session = CreateObject("Notes.NotesSession")
Set Maildb = Session.CURRENTDATABASE
Set MailDoc = Maildb.CREATEDOCUMENT
Set Workspace = CreateObject("Notes.NOTESUIWORKSPACE")
'important variables are set
LastRow = tb_Mailing_List.Cells(Rows.Count, 2).End(xlUp).Row
Row_Count = 3
Mail_Body_Lock = False
'cell assignment
Mail_Text_1 = tb_Email_Template.Cells(4, 4).Value
Mail_Text_2 = tb_Email_Template.Cells(5, 4).Value
Mail_Text_3 = tb_Email_Template.Cells(6, 4).Value
Mail_Text_4 = tb_Email_Template.Cells(7, 4).Value
Mail_Text_5 = tb_Email_Template.Cells(8, 4).Value
Mail_Text_6 = tb_Email_Template.Cells(9, 4).Value
Mail_Closing = tb_Email_Template.Cells(25, 4).Value
Mail_SendBy = tb_Email_Template.Cells(12, 4).Value & vbNewLine & vbNewLine & tb_Email_Template.Cells(13, 4).Value & vbNewLine & tb_Email_Template.Cells(14, 4).Value & vbNewLine & tb_Email_Template.Cells(15, 4).Value & vbNewLine & vbNewLine & tb_Email_Template.Cells(16, 4).Value & vbNewLine & tb_Email_Template.Cells(17, 4).Value & vbNewLine & tb_Email_Template.Cells(18, 4).Value & vbNewLine & tb_Email_Template.Cells(19, 4).Value & vbNewLine & tb_Email_Template.Cells(20, 4).Value & vbNewLine & tb_Email_Template.Cells(21, 4).Value & vbNewLine & tb_Email_Template.Cells(22, 4).Value
'loops until all names have been filled
Do Until Row_Count = LastRow + 1
'Mail Dashboard
Mail_Body_Lock = False
Mail_Send = False
Mail_Form = "Memo"
Mail_Name = tb_Mailing_List.Cells(Row_Count, 2).Value
Mail_SendTo = tb_Mailing_List.Cells(Row_Count, 4).Value
Mail_Subject = tb_Email_Template.Cells(2, 4).Value
Mail_Save = True
'exit round in case the email address is not present
If Mail_SendTo = "" Then GoTo NoEmail
'if only body row 1 has text
If Mail_Text_2 = "" And Mail_Text_3 = "" And Mail_Text_4 = "" And Mail_Text_5 = "" And Mail_Text_6 = "" Then
Mail_Body = "Dear " & Mail_Name & vbNewLine & vbNewLine & Mail_Text_1 & vbNewLine & vbNewLine & Mail_Closing & vbNewLine & vbNewLine & vbNewLine & Mail_SendBy
Mail_Body_Lock = True
End If
'if only body row 1 and row 2 have text
If Mail_Body_Lock = False And Mail_Text_3 = "" And Mail_Text_4 = "" And Mail_Text_5 = "" And Mail_Text_6 = "" Then
Mail_Body = "Dear " & Mail_Name & "," & vbNewLine & vbNewLine & Mail_Text_1 & vbNewLine & Mail_Text_2 & vbNewLine & vbNewLine & Mail_Closing & vbNewLine & vbNewLine & vbNewLine & Mail_SendBy
Mail_Body_Lock = True
End If
'if only body row 1 till row 3 have text
If Mail_Body_Lock = False And Mail_Text_4 = "" And Mail_Text_5 = "" And Mail_Text_6 = "" Then
Mail_Body = "Dear " & Mail_Name & vbNewLine & vbNewLine & Mail_Text_1 & vbNewLine & Mail_Text_2 & vbNewLine & Mail_Text_3 & vbNewLine & vbNewLine & Mail_Closing & vbNewLine & vbNewLine & vbNewLine & Mail_SendBy
Mail_Body_Lock = True
End If
'if only body row 1 till row 4 have text
If Mail_Body_Lock = False And Mail_Text_5 = "" And Mail_Text_6 = "" Then
Mail_Body = "Dear " & Mail_Name & vbNewLine & vbNewLine & Mail_Text_1 & vbNewLine & Mail_Text_2 & vbNewLine & Mail_Text_3 & vbNewLine & Mail_Text_4 & vbNewLine & vbNewLine & Mail_Closing & vbNewLine & vbNewLine & vbNewLine & Mail_SendBy
Mail_Body_Lock = True
End If
'if only body row 1 till row 5 have text
If Mail_Body_Lock = False And Mail_Text_6 = "" Then
Mail_Body = "Dear " & Mail_Name & vbNewLine & vbNewLine & Mail_Text_1 & vbNewLine & Mail_Text_2 & vbNewLine & Mail_Text_3 & vbNewLine & Mail_Text_4 & vbNewLine & Mail_Text_5 & vbNewLine & vbNewLine & Mail_Closing & vbNewLine & vbNewLine & vbNewLine & Mail_SendBy
Mail_Body_Lock = True
End If
'in case there is an error or something
If Mail_Body_Lock = False Then
Mail_Body = "Dear " & Mail_Name & vbNewLine & vbNewLine & Mail_Text_1 & vbNewLine & Mail_Text_2 & vbNewLine & Mail_Text_3 & vbNewLine & Mail_Text_4 & vbNewLine & Mail_Text_5 & vbNewLine & Mail_Text_6 & vbNewLine & vbNewLine & Mail_Closing & vbNewLine & vbNewLine & vbNewLine & Mail_SendBy
End If
'mail build-up
MailDoc.Form = Mail_Form
MailDoc.SendTo = Mail_SendTo
MailDoc.Subject = Mail_Subject
MailDoc.Body = Mail_Body
'attachement build-up
If tb_Email_Template.Cells(28, 4) <> "" And tb_Email_Template.Cells(29, 4) <> "" Then
Attachment = tb_Email_Template.Cells(28, 4)
stAttachment = tb_Email_Template.Cells(29, 4)
Set AttachME = MailDoc.CREATERICHTEXTITEM("stAttachment")
Set EmbedObj = AttachME.EmbedObject(EMBED_ATTACHMENT, "", Attachment, "stAttachment")
End If
Call Workspace.EDITDOCUMENT(True, MailDoc).GOTOFIELD("Body")
MsgBox "Email send?"
NoEmail:
Row_Count = Row_Count + 1
Loop
'variable dump
Set Maildb = Nothing
Set MailDoc = Nothing
Set AttachME = Nothing
Set Session = Nothing
Set EmbedObj = Nothing
End Sub
After:
Call Workspace.EDITDOCUMENT(True, MailDoc).GOTOFIELD("Body")
I want to VBA to open that window and not that I have to go there by myself. I am sure that there has to be a way. I used mouse movements, which worked until a colleague with a different screen res. used the program.
I am quite new to VBA and programming and taught myself so I am sorry if this is maybe a dumb question, but I couldn't find the answer so far anywhere else.
Try This way,
Public vPID As Variant
Public Sub OpenApplication()
'Launch application if not already open
If vPID = 0 Then 'Application not already open
101:
vPID = Shell("C:\Windows\system32\notepad.exe", vbNormalFocus)
Else 'Application already open so reactivate
On Error GoTo 101
AppActivate (vPID)
End If
End Sub
Because the variable vPID is stored as a project level Public Variable, its value will be retained for as long as your instance of Excel (or other Microsoft Office application) is open.
I have this email automation program. I essentially want to create a error catch for RecpName. When RecpName is passed into Lotus Notes and returns an error (due to spelling errors), I want to capture that into a error catch.
I still want the loop to keep going and continue down the list, but tell the user which names it couldn't send emails to.
Here's my code:
Sub Send_HTML_Email()
Const ENC_IDENTITY_8BIT = 1729
'Send Lotus Notes email containing links to files on local computer
Dim NSession As Object 'NotesSession
Dim NDatabase As Object 'NotesDatabase
Dim NStream As Object 'NotesStream
Dim NDoc As Object 'NotesDocument
Dim NMIMEBody As Object 'NotesMIMEEntity
Dim SendTo As String
Dim subject As String
Dim HTML As String, HTMLbody As String
Dim wb As Workbook
Dim ws As Worksheet
Dim lstrow As Long, j As Long
Dim RecpName As String, candiName As String
Dim a As Hyperlink
Set wb = ThisWorkbook
Set ws = wb.Worksheets("Detail")
' Instantiate the Lotus Notes COM's Objects.
lstrow = ws.Range("B" & Rows.Count).End(xlUp).Row
Set NSession = CreateObject("Notes.NotesSession") 'using Lotus Notes Automation Classes (OLE)
Set NDatabase = NSession.GetDatabase("", "")
If Not NDatabase.IsOpen Then NDatabase.OPENMAIL
For j = 3 To lstrow
RecpName = ws.Cells(j, 2).Text
candiName = ws.Cells(j, 1).Text
SendTo = RecpName
subject = wb.Worksheets("Email Settings").Range("B1").Text
Debug.Print subject
Set NStream = NSession.CreateStream
HTMLbody = "<p>" & "Hi " & ws.Cells(j, 2).Text & "," & "</p>" & _
vbCrLf & _
"<p>" & Sheets("Email Settings").Cells(2, 2).Text & vbCrLf & _
Sheets("Detail").Cells(j, 1).Text & "</p>" & vbCrLf & _
"<p>" & Sheets("Email Settings").Cells(3, 2).Text & _
"<br>" & Sheets("Email Settings").Cells(4, 2).Text & _
"<br>" & Sheets("Email Settings").Cells(5, 2).Text & _
"<br>" & Sheets("Email Settings").Cells(6, 2).Text & "</p>" & _
"<p>" & Sheets("Email Settings").Cells(9, 2).Text & _
"<br>" & Sheets("Email Settings").Cells(10, 2).Text & _
"<br>" & Sheets("Email Settings").Cells(11, 2).Text & _
"<br>" & Sheets("Email Settings").Cells(12, 2).Text & _
"<br>" & Sheets("Email Settings").Cells(13, 2).Text & _
"<br>" & Sheets("Email Settings").Cells(14, 2).Text & _
"<br>" & Sheets("Email Settings").Cells(15, 2).Text & "</p>"
HTML = "<html>" & vbLf & _
"<head>" & vbLf & _
"<meta http-equiv=""Content-Type"" content=""text/html; charset=UTF-8""/>" & vbLf & _
"</head>" & vbLf & _
"<body>" & vbLf & _
HTMLbody & _
"</body>" & vbLf & _
"</html>"
NSession.ConvertMime = False 'Don't convert MIME to rich text
Set NDoc = NDatabase.CreateDocument()
With NDoc
.Form = "Memo"
.subject = subject
.SendTo = Split(SendTo, ",")
Set NMIMEBody = .CreateMIMEEntity
NStream.WriteText HTML
NMIMEBody.SetContentFromText NStream, "text/html; charset=UTF-8", ENC_IDENTITY_8BIT
.Send False
.Save True, False, False
End With
NSession.ConvertMime = True 'Restore conversion
Next j
Set NDoc = Nothing
Set NSession = Nothing
MsgBox "The e-mail has successfully been created and distributed", vbInformation
End Sub
Maybe this code can help you:
Sub Send_HTML_Email()
Dim cnt_err As Integer: cnt_err = 1
On Error GoTo ErrorHandler
Const ENC_IDENTITY_8BIT = 1729
' Insert the rest of the code here
MsgBox "The e-mail has successfully been created and distributed", vbInformation
Exit Sub
ErrorHandler:
' Insert code to handle the error, e.g.
wb.Worksheets("SheetToSaveMailsNotSent").Range("A" & cnt) = RecpName
cnt = cnt + 1
' The next instruction will continue the subroutine execution
Resume Next
End Sub
For more help you can go to this link.
HTH ;)
When I run the following sub, the gmail is sent without the attachment. If I set a variable for the attachment, and alter the .AddAttachment statement to .AddAttachment (FName), an attachment is sent with the email but it is empty. Please help. Here is my code:
Sub SendEmail()
Dim iMsg As Object
Dim iConf As Object
Dim Flds As Variant
Dim Msg As String
Dim iBp As CDO.IBodyPart
Set iMsg = CreateObject("CDO.Message")
Set iConf = CreateObject("CDO.Configuration")
iConf.Load -1
Set Flds = iConf.Fields
With Flds
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") _
= "stmpCorpServer"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = _
InputBox("Please enter your email address")
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = _
InputBox("Please enter your password")
.Update
End With
'Set Variables
Msg = "Record Count - " & EmlRcrdCt & vbNewLine & _
"Store Count - " & EmlStrCt & vbNewLine & _
"Record Count for shelf on hand > 6*+1 shelf capacity - " & _
EmlRcrdCtShlf6 & vbNewLine & _
"Record count for shelf on hand > 0 and capacity 0 - " & _
EmlRcrdCtShlf0 & vbNewLine & _
"Record count for quantity of adjustment=0 and adjustment quantity>0 - " & _
EmlRcrdQty0 & vbNewLine & _
"Record count for quantity of adjustment>0 and adjustment quantity=0 - " & _
EmlRcrdCtQtyGrtr0 & vbNewLine & vbNewLine & _
"Attached is a spreadsheet of the 'store' counts and 'shelf on hand' counts." & _
vbNewLine & _
"Please let me know if you have any questions." & vbNewLine & vbNewLine & _
EmlMisStrs & vbNewLine & _
EmlLgVar & vbNewLine
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
'Set email settings
On Error Resume Next
With iMsg
Set .Configuration = iConf
.To = "MyEmail"
.From = """Julia"" <MyEmail>"
.CC = "MyEmail"
.BCC = ""
.Subject = "CAO results for week ending " & LstDayInWk
.TextBody = Msg
.AddAttachment "C:\CAO\SS CAO we 06072014.xlsx"
.Send
End With
On Error GoTo 0
'Activate Control Sheet
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
End Sub