0

I am receiving continues emails from customer (different customers) to update their asset details in database.. once process done .. I have to reply (including cc) from their mail telling like "asset details successfully stored in Database" (am using template) using VBA.

Option Explicit


Public Sub ReplyToAll()

Dim oExp As Outlook.Explorer

'for selected mails in outlook

Dim oSM As mailItem

Dim oNM As mailItem

    On Error GoTo Err
    
    Set oExp = Outlook.Application.ActiveExplorer
    
    'Check if something is selected
    If oExp.Selection.Count > 0 Then
        
        'Get the first item selected

        Set oSM = ActiveExplorer.Selection.Item(1)
        
            'Create a Reply template

            Set oNM = oSM.ReplyAll
            
            With oNM
                
                'Change the subject
                .Subject = "RE: " & oSM.Subject
                
                'Change the body
                .Body = .Body & Chr(13) & Chr(13)
                
            'Display the new mail before sending it
                .Display
            
            End With
            
    End If
    
    Exit Sub
    
Err:
    
    MsgBox Err.Description, vbCritical
    
End Sub

section 3

Sub ReplyAll()
    Dim objOutlookObject As mailItem
    
For Each objOutlookObject In GetCurrentOutlookItems

    With objOutlookObject
    .ReplyAll.Display
'prob area code does not include the template saved in the location c ..throws some error

    .createitemtemplate("c:\car.jtm")
   End With
Next
End Sub
 
Function GetCurrentOutlookItems() As Collection
    Dim objApp As Outlook.Application
    Dim objItem As Object
    Dim colItems As New Collection
        
    Set objApp = CreateObject("Outlook.Application")
    On Error Resume Next
    Select Case TypeName(objApp.ActiveWindow)
        Case "Explorer"
            For Each objItem In objApp.ActiveExplorer.Selection
                colItems.Add objItem
            Next
        Case "Inspector"
            colItems.Add objApp.ActiveInspector.CurrentItem
        Case Else
            ' anything else will result in an error, which is
            ' why we have the error handler above
    End Select
    
    Set objApp = Nothing
    Set GetCurrentOutlookItems = colItems
End Function
eglease
  • 2,445
  • 11
  • 18
  • 28
Sathish Kothandam
  • 1,530
  • 3
  • 16
  • 34
  • 1
    Look through the `outlook-vba` questions for the last month. Several explain different aspects of your problem: how to access mail items, how to extract details and how to reply automatically. You tell us nothing about your database so no one can help you with that. Build a macro from the recent answers then come back with a specific question if the macro does not work. – Tony Dallimore Jan 11 '12 at 14:29
  • I searched i could it find related things.. can u suggest any post ..how send a reply from original mail so that i can develop my macro.. – Sathish Kothandam Jan 12 '12 at 07:49
  • Agreed with Tony. You need to ask _specific_ questions and show what you have tried. – JimmyPena Jan 13 '12 at 01:32
  • Above i have given the code and what output it displays.. and what i need .. Thanks for your suggestion :) – Sathish Kothandam Jan 13 '12 at 06:51

1 Answers1

1

I am sorry my comment was so curt; 500 characters does not leave much room for a full answer.

Your question is very unclear so it is likely to be closed. Certainly, I do not see how anyone could answer it. That is why it is important that you try to solve your own problem and return as necessary with specific questions.

Below I provide links to recent posts that I believe will help you get started. As I said in my comment, look through recent posts. Use the search facility. There are some very good answers here if you look for them.

The first two posts are tutorials written by me. The early steps are the same but, because the questions were not quite the same, later steps are different. Look at both and pick out the bits relevant to you. The others posts all contain information you may find helpful.

How to import the outlook mail data to excel

update excel sheet based on outlook mail

could anyone guide me in creating an outlook macro that does the following

send an email from excel 2007 vba using an outlook template set variables

using visual basic to access subfolder in inbox

vba outlook event moving email

New section in response to new information from questioner

Except for minor modifications, the code in your question was taken from the Microsoft Help file for NewMailEx Event. This code will only work if you have the correct type of installation and if you place it in the correct place:

  • "The NewMailEx event will only fire for mailboxes in Microsoft Outlook that provide notification for received message such as Microsoft Exchange Server. Also, the event will fire only if Outlook is running. In other words, it will not fire for the new items that are received in the Inbox when Outlook was not open. Developers who want to access these items for customers running Outlook on an Exchange server e-mail account need to implement their code on the server. However, the NewMailEx event will fire against Cached Exchange Mode in all settings: Download Full Items, Download Headers, and Download Headers and then Full Items."

Do you have the correct type of installation? Can you place your code on the server? Even if this is the correct approach for the final version of your macro, I do not believe it is the correct approach while you are learning VBA and Outlook.

You need two things:

  1. a detailed specification of the macro you wish to write and
  2. more understanding of VBA and Outlook.

I doubt you can create the detailed specification yet because you do not know enough about VBA and Outlook. But we can list things you will need to know:

  • How do you write to your database from Outlook?
  • How do you identify the mail items you wish to record? In your example, you are checking for a subject of "Hello" and replying "Hi". This is fine for a first experiment but you need to identify the real method. Is it a new sender? Is there specific information in the body of the message? Does a human have to identify such mail items?
  • In your example, you have a folder "Personal" under "Inbox". Many people seem to have this type of folder structure and Microsoft examples tend to use folders like this. I do not. I have a folder called "!Home". Under this I have folders for "Insurance", "Shopping", "Money". Under these I have folders for my different suppliers. Once I have dealt with a message, I move it to appropriate folder. Replies go to the same folder. This is my system and it works for me. What is your system going to be? Will, for example, there be a single folder for all customers or one per customer?

The above is a starter list of questions for your specification but it is also a starter list of things you need to know.

Perhaps you have a boss who wants you to stop wasting time and start writing the macro but you do not know enough yet to plan the final macro.

Start with my tutorials. The first three steps are about the folder structure. These steps are essential if you have the kind of complex folder structures I have. Next I go through a folder displaying selected information from each mail item. I have steps in which I write message bodies to disc. I suggest you go through both tutorials and try my code. Not all of it will be immediately useful but it is all good background information.

What is your database? Is it Access or Excel? There is some help in my tutorials and in the other links above with writing to Excel which you could adapt for Access.

I think the above is enough for now. Take it slowly and it will start to make sense. I still remember the first time I tried to write an Outlook macro so I understand your confusion. I promise that it will become clear. Best of luck.

New section in response to the following comment:

  • "hello i have tried ..Got what i want....Removed my previous code..and tried replaced the new code .. Now little help needed from you ....is there any way to use same format like when we click the replyall button in outlook .. my code working fine ..prob is format of the mail is differ .."

Problem 1

.Body = .Body & Chr(13) & Chr(13)

You are using the text body. I think you want the HTML body. Try:

.HTMLBody = .HTMLBody & Chr(13) & Chr(13)

Problem 2

You cannot add to the HTML body in this way. The HTML body will be:

<!doctype ...><html><head> ... </head><body> ... </body></html>

You must add your text to the beginning of the body; that is, just after <body>. If you just add your text, you will be accepting whatever style, margins and colours the sender has used. The following code adds some text that looks the same in every email I have tried it with. My text is within a table with a single cell. The table covers the full width of the page. The text is blue on a white background.

Dim InsertStg As String
Dim Inx As Long
Dim Pos As Long

    'Change the body step 1: Create the string to be inserted
    InsertStg = "<table border=0 width=""100%"" style=""Color: #0000FF""" & _
                " bgColor=#FFFFFF><tr><td><p>"
    For Inx = 1 To 10
      InsertStg = InsertStg & "Sentence " & Inx & " of first paragraph. "
    Next
    InsertStg = InsertStg & "</p><p>"
    For Inx = 1 To 10
      InsertStg = InsertStg & "Sentence " & Inx & " of second paragraph. "
    Next

    ' The following adds a signature at the bottom of the message.
    ' "font-family" gives a list of fonts to be tried.  If these are
    ' missing from your computer, use the names of fonts you do have.
    ' "serif" means that if none of the fonts exist any serif font
    ' that exists is to be used.
    InsertStg = InsertStg & "</p><p style = ""font-family: Mistral, " & _
                "Vivaldi, serif; font-size: 14px; color: " & _
                "rgb(127,0,127)"">John Smith<br>5 Acacia Avenue<br>"

    InsertStg = InsertStg & "</p></td></tr></table>"
    'Change the body step 2: Find insertion position just after <Body> element
    Pos = InStr(1, LCase(.HTMLBody), "<body")
    If Pos = 0 Then
      Call MsgBox("<Body> element not found in HTML body", vbCritical)
      Exit Sub
    End If
    Pos = InStr(Pos, .HTMLBody, ">")
    If Pos = 0 Then
      Call MsgBox("Terminating > for <Body> element not found in HTML body", vbCritical)
      Exit Sub
    End If
    'Change the body step 3: Insert my text into body
    .HTMLBody = Mid(.HTMLBody, 1, Pos) & InsertStg & Mid(.HTMLBody, Pos + 1)
Community
  • 1
  • 1
Tony Dallimore
  • 12,335
  • 7
  • 32
  • 61
  • @Sathish Kothandam. I hope the next section I have added to my original answer is helpful. – Tony Dallimore Jan 13 '12 at 11:48
  • Fantastic.. your are awesome … I started learning to Vba-outlook as you suggested me ..Thanks i have added section 2.. To tell you what am doing what i need exactly ..Thanks fro your help ... will come back with good knowledge in VBA..any idea pls give in this post.. – Sathish Kothandam Jan 17 '12 at 05:36
  • Can u post best site to learn outlook-vba to learn it quickly – Sathish Kothandam Jan 17 '12 at 07:17
  • I do not know what to suggest. I learnt VBA from Wrox's _Excel 2002 VBA_ which I found excellent. But I learnt my first programming language in 1965 and have lost count of how many I have used since then. I learnt Outlook VBA using the VBA Editor's help and experimentation. My first links above are to tutorials which I wrote for staff at my last employer and which many found helpful. – Tony Dallimore Jan 17 '12 at 20:51
  • hello i have tried ..Got what i want....Removed my previous code..and tried replaced the new code .. Now little help needed from you ....is there any way to use same format like when we click the replyall button in outlook .. my code working fine ..prob is format of the mail is differ .. – Sathish Kothandam Jan 18 '12 at 06:29
  • I have added another section to my answer which should solve your problem. – Tony Dallimore Jan 18 '12 at 14:54
  • Your code working perfect ...really awesome.. its helping me to increase knowledge in Vba and html ... to the continuation of my vba reading i have found the function called "Function GetCurrentOutlookItems() ".... now my prob is the code replyall the selected mails when i fire the macro.. but it doesnt include template("c:\car.jtm"") in that ..Can you help out from this .. – Sathish Kothandam Jan 19 '12 at 10:10
  • `.createitemtemplate("c:\car.jtm")`. I have not tried this command myself. However. I think you will find the command is `CreateItemFromTemplate`. Why use the extension `jtm` which is associated with Java. The standard extension for an Outlook File Template is `oft`. This almost certainly does not matter technically but using the standard extension saves confusing humans. Are you trying to reply by adding to the sender's message or are you using your own template? I do not think you can do both. – Tony Dallimore Jan 19 '12 at 10:38
  • Yes u'r right... Am trying to reply by adding to the sender message with my template saved in specific folder .. its working fine upto replyall line.. but i couldnt add template in message ..i altered the code to findout the exact solution.. as you said i can't do two action at a time .. is there any possibilities to add a simple message... in template section like '' "Hello Team, Thanks for your mail ,your asset details successfully loaded into Asset Manager .. THanks.. Sathish.K – Sathish Kothandam Jan 20 '12 at 01:48
  • Is there any option in vba to include signature(Ctrl+N,message tab,include section,signature ) ... if anything found like that means it will be helpful to me – Sathish Kothandam Jan 20 '12 at 01:56
  • If you are interested in digital signatures, I cannot help since I have never investigated them. Try asking a new question about them. If you just want your name and addresses at the bottom in an unusual font and colour then HTML will do that for you. I have added an extra line to my answer to add a purple signature in a handwriting font. – Tony Dallimore Jan 20 '12 at 10:46
  • I missed the first of your two new questions. I do not understand why it is important to use an Outlook template. The message I added to the reply was only junk but it shows how to add any text. You could add `

    Hello Team, Thanks for your mail ,your asset details successfully loaded into Asset Manager.

    ` to InsertStg. You could have some ordinary text files and read the text from one of them. Surely either of these methods is easier than trying to twist an Outlook template to your requirement.
    – Tony Dallimore Jan 20 '12 at 10:57
  • Hello Tony ..All of your comments make me to find out exact solution ..Thanks much for your help ....finaly step by step spoon feeding make me to find out the exact solution – Sathish Kothandam Jan 21 '12 at 04:44
  • Hello Tony Dallimore.. Do you any idea for my ques ...Which i raised in stackoverflow.... Consolitate data from multible sheets ,re-arrange the data as per the column name – Sathish Kothandam Jan 22 '12 at 05:19