Sub AddOLE()
Dim fPath, i As Integer, oleName$
i = ActiveSheet.OLEObjects.Count + 1
oleName = "oleFile" & i 'Make sure we make a unique name.
'Select the cell in which you want to place the attachment
Range("C39").Select
'Get file path
fPath = Application.GetOpenFilename("All Files,*.*", Title:="Select file")
If fPath = False Then Exit Sub
'Insert file
With ActiveSheet
.OLEObjects.Add _
Filename:=fPath, _
Link:=False, _
DisplayAsIcon:=True, _
IconFileName:="excel.exe", _
IconIndex:=0, _
IconLabel:=CreateObject("Scripting.FileSystemObject").GetBaseName(fPath)
.OLEObjects(i).Name = oleName
With .Shapes(oleName)
.AlternativeText = fPath
.Top = [C39].Top
.Left = [C39].Left
End With
End With
End Sub
Sub Main()
Dim S$, T$, sig$
'Tools > References > Microsoft Outlook xx.0 Object Library > OK
Dim olApp As Outlook.Application, olMail As Outlook.MailItem
'Tools > References > Microsoft Word xx.0 Object Library > OK
Dim Word As Document, wr As Word.Range, rTo As Recipient, wos As Word.Selection
'INPUTS to change if needed...........................................................
S = "Hello World Example" 'Subject
T = "ken@gmail.com" 'To
sig = ThisWorkbook.Path & "\sig.rtf" 'contents to copy for signature.
'End INPUTS...........................................................................
'Get Outlook application
Set olApp = New Outlook.Application
'Make email, send/display.
'Set olMail = olApp.CreateItem(olMailItem)
With olApp.CreateItem(olMailItem)
.Subject = S
.Importance = olImportanceNormal
'Set the recipient(s) for To field and resolve.
Set rTo = .Recipients.Add(T)
rTo.Resolve
rTo.Type = olTo 'olTo, olcc, olbcc
If rTo.Resolved = False Then
Debug.Print T & "email address: Resolved=False"
'.To = T 'Using rTo above for .To instead.
GoTo TheEnd
End If
'Setup WordEditor parts:
.GetInspector.Display
Set Word = .GetInspector.WordEditor
Set wr = Word.Range
'Body, introductory text:
Word.Content = "Dear VBA Enthusiast, " & vbCrLf & vbCrLf & _
"I hope that you find this example of copied Excel Range " _
& "and embedded OLEObject using WordEditor in Outlook " _
& "useful." & String(4, vbCrLf)
'Body, range A1, copy/paste:
'Set wos = Word.Windows(1).Selection
Sheet1.Range("A1").CopyPicture xlScreen, xlBitmap
wr.Collapse Direction:=wdCollapseEnd
'Word.Range(Start:=Word.Content.End - 2).PasteAndFormat wdPasteDefault
wr.Paste
wr.Collapse Direction:=wdCollapseEnd
Word.Range.InsertAfter String(4, vbCrLf)
'Body, copy/paste OLEObject
Sheet1.OLEObjects("pdfFirstName").Copy
wr.Collapse Direction:=wdCollapseEnd
Word.Range(Start:=Word.Content.End - 2).PasteAndFormat wdPasteDefault
'wr.Paste
'Body, copy/paste contents of sig.rtf, signature...
GetObject(sig).Range.Copy
wr.Collapse Direction:=wdCollapseEnd
'Word.Range(Start:=Word.Content.End - 2).PasteAndFormat wdPasteDefault
wr.Paste
'.Attachments.Add e 'e is the full path to a file.
.Display
'.Send
End With
TheEnd:
Set olMail = Nothing
Set olApp = Nothing
End Sub