?
Solved

Outlook Macro that sends a mail when right clicked need a change.

Posted on 2009-04-03
2
Medium Priority
?
160 Views
Last Modified: 2012-05-06
Hi,

Outlook Macro that sends a mail when right clicked need a change.
Code from Chris need the name to be as
Hi First name,
but now i get this
Hi First name . Last name,

Regards
SHarath

Sub AutoReply1()
Const FilePathandName As String = "D:\Machinename.txt"
'Const FilePathandName As String = "c:\deleteme\mcusers.txt"
Dim inputFile As Object
Dim fso As Object
Dim dic As Object
Dim arrCount As Long
Dim strName As String
Dim strMC As String
Dim strAddy As String
    Dim olkItem As Object, _
        olkMsg As Outlook.mailitem, _
        olkRecip As Object, _
        arrLines As Variant, _
        varLine As Variant, _
        strTo As String, _
        arrTo() As String
    
    Set fso = CreateObject("scripting.filesystemobject")
    If fso.FileExists(FilePathandName) Then
        Set inputFile = fso.OpenTextFile(FilePathandName, 1, TristateTrue)
        strTo = inputFile.ReadAll
        arrTo = Split(strTo, vbCrLf)
        Set inputFile = Nothing
    Else
        Set inputFile = Nothing
        strTo = ""
    End If
    Set fso = Nothing
    If strTo <> "" Then
        Set dic = CreateObject("scripting.dictionary")
        For arrCount = 0 To UBound(arrTo)
            strName = arrTo(arrCount)
            If InStr(strName, ";") > 0 Then
                strMC = LCase(Left(strName, InStr(strName, ";") - 1))
                strAddy = LCase(Right(strName, Len(strName) - InStr(strName, ";")))
                If Not dic.Exists(strMC) Then
                    dic.Add strMC, strAddy
                End If
            End If
        Next
    End If
    For Each olkItem In Application.ActiveExplorer.Selection
        If olkItem.Class = olMail Then
            arrLines = Split(olkItem.body, vbCrLf)
            For Each varLine In arrLines
                If InStr(1, varLine, "Machine:") Then
                    strName = LCase(Mid(varLine, InStr(1, varLine, ":") + 1))
                    strName = Trim(strName)
                    strName = Replace(strName, vbCr, "")
                    strName = Replace(strName, vbLf, "")
                    If dic.Exists(strName) Then
                        strTo = dic(strName)
                    Else
                        strTo = ""
                    End If
                    If strTo = "" Then Exit For
                    Set olkMsg = Application.CreateItem(olMailItem)
                    With olkMsg
                        Set olkRecip = .Recipients.Add(strTo)
                        olkRecip.Resolve
                        If InStr(strTo, "@") > 0 Then
                            strName = Mid(strTo, 1, InStr(1, strTo, "@") - 1)
                        Else
                            strName = Mid(olkRecip.Name, 1, InStr(1, olkRecip.Name, " ") - 1)
                        End If
                        strname = ucase(left(strname, 1)) & lcase(right(strname, len(strname) - 1))
                        'Change the subject on the next line'
                        .subject = "Subject data"
                        .BodyFormat = olFormatHTML
                        'Change the message body on the next line'
                        .HTMLBody = "Hi " & strName & ",<br><br>This is the matter in the body.<br><br>Regards<br>Sharath<br>(1)<br><hr><br>" & olkItem.HTMLBody
                        .Display
                    End With
                    Exit For
                End If
            Next
        End If
    Next
    Set olkItem = Nothing
    Set olkMsg = Nothing
    Set olkRecip = Nothing
End Sub

Open in new window

0
Comment
Question by:bsharath
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 59

Accepted Solution

by:
Chris Bottomley earned 2000 total points
ID: 24057311
I haven't got access to exchange to test the change .. but try this if you would?

Chris
Sub AutoReply1()
'Const FilePathandName As String = "D:\Machinename.txt"
Const FilePathandName As String = "c:\deleteme\mcusers.txt"
Dim inputFile As Object
Dim fso As Object
Dim dic As Object
Dim arrCount As Long
Dim strName As String
Dim strMC As String
Dim strAddy As String
    Dim olkItem As Object, _
        olkMsg As Outlook.mailitem, _
        olkRecip As Object, _
        arrLines As Variant, _
        varLine As Variant, _
        strTo As String, _
        arrTo() As String
    
    Set fso = CreateObject("scripting.filesystemobject")
    If fso.FileExists(FilePathandName) Then
        Set inputFile = fso.OpenTextFile(FilePathandName, 1, TristateTrue)
        strTo = inputFile.ReadAll
        arrTo = Split(strTo, vbCrLf)
        Set inputFile = Nothing
    Else
        Set inputFile = Nothing
        strTo = ""
    End If
    Set fso = Nothing
    If strTo <> "" Then
        Set dic = CreateObject("scripting.dictionary")
        For arrCount = 0 To UBound(arrTo)
            strName = arrTo(arrCount)
            If InStr(strName, ";") > 0 Then
                strMC = LCase(Left(strName, InStr(strName, ";") - 1))
                strAddy = LCase(Right(strName, Len(strName) - InStr(strName, ";")))
                If Not dic.Exists(strMC) Then
                    dic.Add strMC, strAddy
                End If
            End If
        Next
    End If
    For Each olkItem In Application.ActiveExplorer.Selection
        If olkItem.Class = olMail Then
            arrLines = Split(olkItem.body, vbCrLf)
            For Each varLine In arrLines
                If InStr(1, varLine, "Machine:") Then
                    strName = LCase(Mid(varLine, InStr(1, varLine, ":") + 1))
                    strName = Trim(strName)
                    strName = Replace(strName, vbCr, "")
                    strName = Replace(strName, vbLf, "")
                    If dic.Exists(strName) Then
                        strTo = dic(strName)
                    Else
                        strTo = ""
                    End If
                    If strTo = "" Then Exit For
                    Set olkMsg = Application.CreateItem(olMailItem)
                    With olkMsg
                        Set olkRecip = .Recipients.Add(strTo)
                        olkRecip.Resolve
                        If Not olkRecip.Resolved Then
                            strName = Mid(strTo, 1, InStr(1, strTo, "@") - 1)
                            If InStr(strName, ".") > 0 Then strName = Left(strName, InStr(strName, ".") - 1)
                            If InStr(strName, " ") > 0 Then strName = Left(strName, InStr(strName, " ") - 1)
                        Else
                            strName = Mid(olkRecip.Name, 1, InStr(1, olkRecip.Name, " ") - 1)
                        End If
                        strName = UCase(Left(strName, 1)) & LCase(Right(strName, Len(strName) - 1))
                        'Change the subject on the next line'
                        .subject = "Subject data"
                        .BodyFormat = olFormatHTML
                        'Change the message body on the next line'
                        .HTMLBody = "Hi " & strName & ",<br><br>This is the matter in the body.<br><br>Regards<br>Sharath<br>(1)<br><hr><br>" & olkItem.HTMLBody
                        .Display
                    End With
                    Exit For
                End If
            Next
        End If
    Next
    Set olkItem = Nothing
    Set olkMsg = Nothing
    Set olkRecip = Nothing
End Sub

Open in new window

0
 
LVL 11

Author Comment

by:bsharath
ID: 24057343
Thank U Chris...
Posted a related post for the same change in another 2 similar codes...
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to import an Outlook PST file to Office 365 using a third party product to avoid Microsoft's Azure command line tool, saving you time.
If you troubleshoot Outlook for clients, you may want to know a bit more about the OST file before doing your next job. IMAP can cause a lot of drama if removed in the accounts without backing up.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question