Consulting

Results 1 to 5 of 5

Thread: Standard Reply with generic button

  1. #1
    VBAX Newbie
    Joined
    Jun 2006
    Posts
    3
    Location

    Standard Reply with generic button

    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

  2. #2
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    Hi and welcome to VBAX

    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" routine[VBA]Sub 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 Function[/VBA]I think that should do it
    K :-)

  3. #3
    VBAX Newbie
    Joined
    Jun 2006
    Posts
    3
    Location
    Thanks Killian

    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:

    [VBA]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[/VBA]

  4. #4
    VBAX Newbie
    Joined
    Jun 2006
    Posts
    3
    Location
    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

    [VBA]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[/VBA]

  5. #5
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    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! )[VBA]i = FreeFile
    Open strPath For Input As #i
    Do While Not EOF(1)
    Input #i, str
    GetTextFromFile = GetTextFromFile & str
    Loop
    Close #1 '<-- ahh, yes...[/VBA]
    K :-)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •