?
Solved

Macro which automatically sends attachment to Outlook

Posted on 2016-09-16
14
Medium Priority
?
101 Views
Last Modified: 2016-09-20
Hi Guys, I have a macro which sends an Excel attachment to Outlook, sends it and then kills the attachment, then deletes the attachment.

I want the macro to save the attachment to the drive o:\ProductControl\DailyBS\Sep 16. How do I do this?
Balance-Sheet_15Sep2016_FCD-Extract.xlsm
0
Comment
Question by:JCutcliffe
[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
  • 7
  • 7
14 Comments
 
LVL 52

Expert Comment

by:Rgonzo1971
ID: 41801344
Hi,

pls try
Sub Mail_ActiveSheet()
'Working in Excel 2000-2016

    Dim FileExtStr As String
    Dim FileFormatNum As Long
    Dim Sourcewb As Workbook
    Dim Destwb As Workbook
    Dim TempFilePath As String
    Dim TempFileName As String
    Dim OutApp As Object
    Dim OutMail As Object
Dim rng As Range
    Dim strFolder As String
    strFolder = "o:\ProductControl\DailyBS\"
    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    Set Sourcewb = ActiveWorkbook

    'Copy the ActiveSheet to a new workbook
    ActiveSheet.Copy
    Set Destwb = ActiveWorkbook

    'Determine the Excel version and file extension/format
    With Destwb
        If Val(Application.Version) < 12 Then
            'You use Excel 97-2003
            FileExtStr = ".xls": FileFormatNum = -4143
        Else
            'You use Excel 2007-2016
            Select Case Sourcewb.FileFormat
            Case 51: FileExtStr = ".xlsx": FileFormatNum = 51
            Case 52:
                If .HasVBProject Then
                    FileExtStr = ".xlsm": FileFormatNum = 52
                Else
                    FileExtStr = ".xlsx": FileFormatNum = 51
                End If
            Case 56: FileExtStr = ".xls": FileFormatNum = 56
            Case Else: FileExtStr = ".xlsb": FileFormatNum = 50
            End Select
        End If
    End With
arLinks = ActiveWorkbook.LinkSources(Type:=xlLinkTypeExcelLinks)
    If Not IsEmpty(arLinks) Then
        For lnk = LBound(arLinks) To UBound(arLinks)
            ActiveWorkbook.BreakLink Name:=arLinks(lnk), Type:=xlLinkTypeExcelLinks
        Next lnk
    End If
    '    'Change all cells in the worksheet to values if you want
       With Destwb.Sheets("ECB Calc").Range("rngEmailBody")
           .Copy
           .PasteSpecial xlPasteValues
           .Select
        End With
        Application.CutCopyMode = False

    'Save the new workbook/Mail it/Delete it
    TempFilePath = Environ$("temp") & "\"
    TempFileName = Sheets("ECB Calc").Range("mailSubj")
    

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    With Destwb
        .SaveAs strFolder & Format(Date, "MMM YY") & "\" & TempFileName & FileExtStr, 
        .SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
FileFormat:=FileFormatNum
        On Error Resume Next
        With OutMail
            .To = Range("mailto")
            
            .CC = ""
            .BCC = ""
            .Subject = Range("mailSubj")
            '.Body = "Hi there"
            .Attachments.Add Destwb.FullName
            'You can add other files also like this
            '.Attachments.Add ("C:\test.txt")
             .Display
        End With
        On Error GoTo 0
        .Close savechanges:=False
    End With

    'Delete the file you have send
    'Kill Save
  Kill TempFilePath & TempFileName & FileExtStr

    Set OutMail = Nothing
    Set OutApp = Nothing

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
End Sub

Open in new window

Regards
0
 

Author Comment

by:JCutcliffe
ID: 41801432
I got the error message "Method 'SaveAs' of object '_Workbook' failed on this part of the code.

With Destwb
        .SaveAs strFolder & Format(Date, "MMM YY") & "\" & TempFileName & FileExtStr
        .SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
0
 
LVL 52

Expert Comment

by:Rgonzo1971
ID: 41801445
then try
MsgBox strFolder & Format(Date, "MMM YY") & "\" & TempFileName & FileExtStr
 .SaveAs strFolder & Format(Date, "MMM YY") & "\" & TempFileName & FileExtStr, FileFormat:=FileFormatNum
 .SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum

What is the result of the msgbox?
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

Author Comment

by:JCutcliffe
ID: 41801509
Result is "o:\ProductControl\Daily BS\Sep 16\ECB 15 Sep 2016.xlsx" in MsgBox

Then Method 'SaveAs' of object_Workbook' failed

.SaveAs strFolder & Format(Date, "MMM YY") & "\" & TempFileName & FileExtStr, FileFormat:=FileFormatNum
0
 
LVL 52

Expert Comment

by:Rgonzo1971
ID: 41801513
Does the folder already exist
0
 

Author Comment

by:JCutcliffe
ID: 41801553
I am not sure why, but I recorded a Macro and saved it to that Folder and I received the message "Errors were detected whilst saving '\\10.32.64.28\Accounts\PRODUCT CONTROL\Daily BS\ECB 15 Sep 2016.xlsm" Microsoft Excel may be able to save the file by removing or repairing some features."
0
 
LVL 52

Expert Comment

by:Rgonzo1971
ID: 41801567
I meant this folder
o:\ProductControl\Daily BS\Sep 16
0
 

Author Comment

by:JCutcliffe
ID: 41801574
Sorry, I was wrong . The path is o:\Product Control\Daily BS"
0
 
LVL 52

Expert Comment

by:Rgonzo1971
ID: 41801627
then try
 MsgBox strFolder & TempFileName & FileExtStr
  .SaveAs strFolder & TempFileName & FileExtStr, FileFormat:=FileFormatNum 
  .SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum 

Open in new window

0
 

Author Comment

by:JCutcliffe
ID: 41801656
"Run time error '1004': Method 'Save as of object_ Workbook failed"
0
 
LVL 52

Expert Comment

by:Rgonzo1971
ID: 41801658
Since the error mentioned before
I am not sure why, but I recorded a Macro and saved it to that Folder and I received the message "Errors were detected whilst saving '\\10.32.64.28\Accounts\PRODUCT CONTROL\Daily BS\ECB 15 Sep 2016.xlsm" Microsoft Excel may be able to save the file by removing or repairing some features."

I cannot help further
0
 

Author Comment

by:JCutcliffe
ID: 41801688
Does it work when you try to save it to your Folder?
0
 

Author Comment

by:JCutcliffe
ID: 41801726
I have switched it to another Folder and still getting: Run time error '1004': Method 'Save as of object_ Workbook failed"
0
 
LVL 52

Accepted Solution

by:
Rgonzo1971 earned 2000 total points
ID: 41803524
Sorry can't help further
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Changing a few Outlook Options can help keep you organized!
CodeTwo Sync for iCloud (http://www.codetwo.com/sync-for-icloud?sts=6554) automatically synchronizes your Outlook 2016, 2013, 2010 or 2007 folders with iCloud folders available via iCloud Control Panel. This lets you automatically sync them with…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Suggested Courses

764 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