Page 2 of 2 FirstFirst 12
Results 11 to 18 of 18

Thread: Excel VBA to Attach File And Automatically Send Email

  1. #11
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,402
    Rep Power
    10
    Here's a code to open msg files from a folder, and send it to a mail id

    Code:
    Sub SendMSGFiles()
    
        Dim objItemMSG As Object
        Dim strFolderLocation As String
        Dim objFolder As Object
        Dim strFile As String
        Dim olApp As Object
        
        On Error Resume Next
        Set olApp = GetObject(, "Outlook.Application")
        Err.Clear: On Error GoTo 0: On Error GoTo -1
        If olApp Is Nothing Then
            Set olApp = CreateObject("Outlook.Application")
        End If
        Set objFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Select MSG folder location", 0, "")
        If Not objFolder Is Nothing Then
            strFolderLocation = objFolder.self.Path
        Else
            Exit Sub
        End If
        strFile = Dir(strFolderLocation & "\*.msg")
        While strFile <> ""
            Set objItemMSG = olApp.CreateItemFromTemplate(strFolderLocation & "\" & strFile)
            objItemMSG.To = "abc@xyz.com"
            objItemMSG.Send
            strFile = Dir
        Wend
        
    End Sub
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

  2. #12
    Senior Member
    Join Date
    Jun 2012
    Posts
    337
    Rep Power
    13
    Simply use sendmail....

  3. #13
    Junior Member
    Join Date
    Jul 2013
    Posts
    9
    Rep Power
    0

    Lightbulb Forward outlook mails with excel VBA

    Hi,

    Thank you very much for the support. I could not able to forward the mail but succeed to read the content and paste in another mail and then send to the mail id. I manage to find this code from Ms office help but can't execute it successfully, showing an error message "Run time error 438 Object doesn't support this property or method" at line "Set myinspector = Application.ActiveInspector" in MS office 2007. Please help me to solve this problem and I think my purpose will be solved manipulating the below formula.

    Thanks.
    Code:
    Sub RemoveAttachmentBeforeForwarding()
        Dim myinspector As Outlook.Inspector
        Dim myItem As Outlook.MailItem
        Dim myattachments As Outlook.Attachments
    
        Set myinspector = Application.ActiveInspector
        If Not TypeName(myinspector) = "Nothing" Then
            Set myItem = myinspector.CurrentItem.Forward
            Set myattachments = myItem.Attachments
            While myattachments.Count > 0
                myattachments.Remove 1
            Wend
            myItem.Display
            myItem.Recipients.Add "Dan Wilson"
            myItem.Send
        Else
            MsgBox "There is no active inspector."
        End If
    End Sub
    Last edited by Excel Fox; 07-08-2013 at 02:09 PM. Reason: Code tags added

  4. #14
    Junior Member
    Join Date
    Sep 2012
    Posts
    8
    Rep Power
    0
    I got ExcelFox's to work bang on.

    if your files are in a standard location you could change the

    Code:
        Set objFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Select MSG folder location", 0, "")
        If Not objFolder Is Nothing Then
            strFolderLocation = objFolder.self.Path
    to

    Code:
             strFolderLocation ="c:\YOUR LOCATION"

  5. #15
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,402
    Rep Power
    10
    malay, the code you've posted is to remove attachments from the mail. Your question though was regarding opening msg files from a folder, and forwarding it as it is. Am I mistaken?
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

  6. #16
    Junior Member
    Join Date
    Jul 2013
    Posts
    9
    Rep Power
    0
    Sorry for late response. Yes, the code attached is to remove attachments but in fact i need item attached to the mail. I just gave you the example from the Ms office help on how to forward mail. Can you do me a favor by writing the complete code to open and forward saved mails with attachments as discussed earlier because i am confused how to write as you found a solution that i cannot understand.

  7. #17
    Junior Member
    Join Date
    Jul 2013
    Posts
    9
    Rep Power
    0
    Hi.

    The code you provided can open the ".msg" file in forward mode but "From.." option in outlook is showing the person's name from whom the mail has been originally received. This is why the mail cannot be forwarded clicking the send button. Please tell me how to change the original "From.." to the current sender's mail id...

    Code:
    Sub SendMSGFiles()
    
        Dim objItemMSG As Object
        Dim strFolderLocation As String
        Dim objFolder As Object
        Dim strFile As String
        Dim olApp As Object
        
        On Error Resume Next
        Set olApp = GetObject(, "Outlook.Application")
        Err.Clear: On Error GoTo 0: On Error GoTo -1
        If olApp Is Nothing Then
            Set olApp = CreateObject("Outlook.Application")
        End If
        Set objFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Select MSG folder location", 0, "")
        If Not objFolder Is Nothing Then
            strFolderLocation = objFolder.self.Path
        Else
            Exit Sub
        End If
        strFile = Dir(strFolderLocation & "\*.msg")
        While strFile <> ""
            Set objItemMSG = olApp.CreateItemFromTemplate(strFolderLocation & "\" & strFile)
            objItemMSG.To = "abc@xyz.com"
            objItemMSG.Send
            strFile = Dir
        Wend
        
    End Sub
    Last edited by Excel Fox; 07-15-2013 at 09:58 PM. Reason: Code tag added

  8. #18
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,402
    Rep Power
    10
    malay, there is a clear instruction on top of the quick reply box to use code tags for any code used in the post. Please adhere to forum rules and guidelines.

    So you want to make the mail as if you were sending the mail, instead of it look like a forward mail?
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

Similar Threads

  1. Replies: 2
    Last Post: 05-23-2013, 08:08 AM
  2. Replies: 1
    Last Post: 05-22-2013, 01:50 PM
  3. How To Send Outlook Email Using VBA
    By mfaisalrazzak in forum Excel Help
    Replies: 7
    Last Post: 03-03-2013, 03:09 AM
  4. Send Mail Using VBA In Excel And Attach Files
    By macenmin in forum Excel Help
    Replies: 1
    Last Post: 08-03-2012, 01:03 AM
  5. Send Lotus Notes Email Using VBA
    By ramakrishnan in forum Excel Help
    Replies: 1
    Last Post: 09-08-2011, 09:00 AM

Posting Permissions

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