HI, I have an EXCEL marco designed, which searches a particular folder (using a browsefolder function) for excel attachments, then it opens each file in turn, uses the filename (minus the .xls) and searches a worksheet using vlookup which contains email addresss to then populate the ".to" variable and automatically saves the the email to my oulook, as well as saving the open workbook as an attachment.
However, I cannot get the ".to" to be populated byt the email address, which is saved as a variable "Smail". Can anyone help? I am at my wits end....
I'm using Windows XP & Excel 2003/Outlook 2003
Option Explicit
Public getfolder As String
' Create Email '
Sub Create_Email()
Dim olApp As Object
Dim Outmail As Object
Dim SigString As String
Dim Strbody As String
Dim Signature As String
Dim path As String
Dim Prompt As String
Dim OpenAt As String
Dim lCount As Long
Dim wbResults As Workbook
Dim wbCodeBook As Workbook
Dim Filter As String, Title As String
Dim FilterIndex As Integer
Dim filename As Variant
Dim posn As Integer
Dim ReceiveName As String
Dim Lookuprange As Range
Dim Smail As String
' This Userform asks who is running the report '
Userform1.Show
'Search For Files To Use As Attachments using Browsefolder Function '
If BrowseFolder = False Then
Debug.Print "No folder selected."
On Error GoTo 0
End If
'Create the Message Subject, Body & Recepient'
Strbody = "Good Morning," & "<br><br><br>" & _
"Test Body message"
'Set The signature accrding to the current user select in userform'
SigString = "C:\Documents and Settings\" & Environ("UserName") & _
"\Application Data\Microsoft\Signatures\test.htm"
Signature = GetBoiler(SigString)
'Create The Message '
' Stop Screen Updating '
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.EnableEvents = False
On Error Resume Next
Set wbCodeBook = ThisWorkbook
'Do A Filesearch in specified path '
With Application.FileSearch
.NewSearch
'Change path to suit
.LookIn = getfolder & "\"
.FileType = msoFileTypeExcelWorkbooks
If .Execute > 0 Then 'Workbooks in folder '
For lCount = 1 To .FoundFiles.Count 'Loop through all
'Open Workbook x and Set a Workbook variable to it
Filter = "Excel Files (*.xls),*.xls"
FilterIndex = 3
Set wbResults = Workbooks.Open(filename:=.FoundFiles(lCount), UpdateLinks:=0)
posn = 0
posn = InStr(wbResults.Name, ".")
If posn <> 0 Then
ReceiveName = Left(wbResults.Name, posn - 1)
End If
Smail = ""
Smail = WorksheetFunction.VLookup(ReceiveName, Worksheets("Contacts").Range("A1:B245"), 2, 0)
'Specific Code For each Email'
' Set olApp as Microsoft Outlook & Msg as New Email Message '
Set olApp = CreateObject("Outlook.Application")
Set Outmail = olApp.CreateItem(0)
On Error Resume Next
Function GetBoiler(ByVal sFile As String) As String
'Signature Function '
Dim fso As Object
Dim ts As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.GetFile(sFile).OpenAsTextStream(1, -2)
GetBoiler = ts.readall
ts.Close
End Function
Function BrowseFolder(Optional Title, Optional OpenAt As Variant) As Variant
'Function purpose: To Browser for a user selected folder.
'If the "OpenAt" path is provided, open the browser at that directory
'NOTE: If invalid, it will open at the Desktop level
Dim ShellApp As Object
'Create a file browser window at the default folder
Set ShellApp = CreateObject("Shell.Application").BrowseForFolder(0, "Please choose a folder", 0, "X:\1. Specialist Team\Alliance & Managed\")
'Set the folder to that selected. (On error in case cancelled)
On Error Resume Next
BrowseFolder = ShellApp.self.path
getfolder = ShellApp.self.path
On Error GoTo 0
'Destroy the Shell Application
Set ShellApp = Nothing
'Check for invalid or non-entries and send to the Invalid error
'handler if found
'Valid selections can begin L: (where L is a letter) or
'\\ (as in \\servername\sharename. All others are invalid
Select Case Mid(BrowseFolder, 2, 1)
Case Is = ":"
If Left(BrowseFolder, 1) = ":" Then GoTo Invalid
Case Is = "\"
If Not Left(BrowseFolder, 1) = "\" Then GoTo Invalid
Case Else
GoTo Invalid
End Select
Exit Function
Invalid:
'If it was determined that the selection was invalid, set to False
BrowseFolder = False
End Function
Bookmarks