Log in

View Full Version : Standard Reply with generic button



lloydio
06-12-2006, 03:40 AM
Sorry but im a bit of a newbie to Outlook Macros programming but all i want to do is have a button in the toolbar which will send a generic email message to the current selected message in the inbox. also i want the body text to be read from a text file on "C:\message.txt".

Hope you are able to lead me in the right direction.

Lloyd

Killian
06-12-2006, 05:41 AM
Hi and welcome to VBAX :hi:

You'll need to add the following code in a standard module, then add a new tool bar button ("customize" toolbars) that links to the "main" routineSub main()

Dim myReply As MailItem
Dim objItem As MailItem

If Application.ActiveExplorer.Selection.Count > 0 Then
For Each objItem In Application.ActiveExplorer.Selection
If objItem.Class = olMail Then
Set myReply = objItem.Reply
myReply.Body = GetTextFromFile("C:\message.txt")
myReply.Display
End If
Next
End If

End Sub

Function GetTextFromFile(strPath As String) As String
Dim i As Long
Dim str As String

i = FreeFile
Open strPath For Input As #i
Do While Not EOF(1) ' Loop until end of file.
Input #i, str
GetTextFromFile = GetTextFromFile & str
Loop

End FunctionI think that should do it

lloydio
06-12-2006, 05:52 AM
Thanks Killian:clap:

That worked a treat, I think this way will work better as i was trying to grab the email address from the current selected message and fire off a new message but was having some difficulties.

Previouse method of sending:

Set oLapp = CreateObject("Outlook.application")
Set oItem = oLapp.CreateItem(0)
'
With oItem
.Subject = "Testies" 'Subject
.To = "test@test.co.uk" 'Recipient
.Body = Message
.Send
End With
'
Set oLapp = Nothing
Set oItem = Nothing

lloydio
06-12-2006, 06:20 AM
there was a small bug in the code where by once you pressed the button once it would only read the .txt file once so i have managed to fix it by taking out the function and doing it this method.

Sorry if its a bit clunky but it works :P

Sub main()

Dim myReply As MailItem
Dim objItem As MailItem

Dim objFile, strGuyFile, strFilePath
strFilePath = "C:\message.txt"
Set objFile = CreateObject("Scripting.FileSystemObject")
Set strGuyFile = objFile.OpenTextFile(strFilePath, 1)
Message = strGuyFile.ReadAll
strGuyFile.Close


If Application.ActiveExplorer.Selection.Count > 0 Then
For Each objItem In Application.ActiveExplorer.Selection
If objItem.Class = olMail Then
Set myReply = objItem.Reply
myReply.Body = Message
myReply.Display





End If
Next
End If

End Sub

Killian
06-12-2006, 08:16 AM
there was a small bug in the code...Well spotted. I did consider using the FileSystemObject for this but it's not nessecary to have that overhead (such that it is). The bug in my original can be fixed by closing the file after accessing it (DOH! :doh: )i = FreeFile
Open strPath For Input As #i
Do While Not EOF(1)
Input #i, str
GetTextFromFile = GetTextFromFile & str
Loop
Close #1 '<-- ahh, yes...