+ Reply to Thread
Page 3 of 3 FirstFirst 123
Results 31 to 43 of 43

Thread: CDO email method

  1. #31
    Valued Forum Contributor mcinnes01's Avatar
    Join Date
    05-25-2010
    Location
    Manchester
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    446

    Re: CDO email method

    I have change it to textbody now:

           CDO_Mail_Object.Textbody = Email_Body
    With the attachment I wasn't sure how to refer to the file, I guess I could refer to it as Destwb? How do I refer to the file that the macro names and saves on the destop?

    Would this work:

           CDO_Mail_Object.AddAttachment "c:\desktop\"& ActiveSheet.Name _
                                                      & Environ("USERNAME") _
                                                      & " - " _
                                                      & Format(Now, " d-m-yy h.m AM/PM")

  2. #32
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    Apparently I can't say
    MS-Off Ver
    Apparently I can't say
    Posts
    8,274

    Re: CDO email method

    You just need to specify the path that you saved it as - i.e.:
    SaveStr & FileExtStr
    However, I don't believe you can send an open workbook via CDO, so you will need to close the saved copy before you try to send it.

  3. #33
    Valued Forum Contributor mcinnes01's Avatar
    Join Date
    05-25-2010
    Location
    Manchester
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    446

    Re: CDO email method

    This is the line that is coming up with 1004 error cannot change part of a merged cell.
    I have tried changing this so it isn't part of a merged cell but it is coming up with the error still. (I.E. I unmerged the cells on the output sheet)

                        .Range("A2:B" & NR & ",E2:E" & NR & ",G2:G" & NR & ",J2:L" & NR).Copy Destination:=Destwb.Worksheets("Sheet1").Range("Area")
    I have also ammend the attachment line as suggested

           CDO_Mail_Object.AddAttachment SaveStr & FileExtStr
    and closed the workbook before the CDO sends the attachment

           .SaveAs SaveStr & FileExtStr, FileFormat:=FileFormatNum
           ActiveWorkbook.Close
           On Error Resume Next
    Last edited by mcinnes01; 10-29-2010 at 09:11 AM.

  4. #34
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    Apparently I can't say
    MS-Off Ver
    Apparently I can't say
    Posts
    8,274

    Re: CDO email method

    That's because you have no data to copy, so the copy is including the header row, and there is a merged cell in the header.

  5. #35
    Valued Forum Contributor mcinnes01's Avatar
    Join Date
    05-25-2010
    Location
    Manchester
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    446

    Re: CDO email method

    Ok I have ammended the layout so there are no merged cells and that has fixed the 1004 issue. It now errors on 424 "Object required at VBAProject" it does not highlight a line in the debugger.

    The file save is still working, here is an example file of what I have done so far...
    Attached Files Attached Files

  6. #36
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    Apparently I can't say
    MS-Off Ver
    Apparently I can't say
    Posts
    8,274

    Re: CDO email method

    You are trying to close the workbook again after you have already closed it - move the
    .Close SaveChanges:=False
    line up to replace the Activeworkbook.Close line.
    You also need to specify a From address and actually send the email.

  7. #37
    Valued Forum Contributor mcinnes01's Avatar
    Join Date
    05-25-2010
    Location
    Manchester
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    446

    Re: CDO email method

    OK that plus a few range changes in the macro has got rid of all the errors and saves the file correctly. But it is not actioning an email to be sent. Is there any way I can check why no email is being created?

    The attachment has the latest workbook in that is working as described above

    (thanks for bearing with me on this one!)
    Attached Files Attached Files

  8. #38
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    Apparently I can't say
    MS-Off Ver
    Apparently I can't say
    Posts
    8,274

    Re: CDO email method

    1. You haven't told it to send!
    2. You still haven't specified a From address
    3. You still haven't added in the username and password for the configuration as I said earlier.

    Once you do that, it should be fine hopefully.

  9. #39
    Valued Forum Contributor mcinnes01's Avatar
    Join Date
    05-25-2010
    Location
    Manchester
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    446

    Re: CDO email method

    with the from address is there a way that this can be pulled from the email client.

    E.g. through MAPI so it looks at the active account, or if the client is closed it opens the client and prompts you to login, then takes the email address from the active account.

    Before when I had the send mail technique, it would do this as far as opening the client if closed or creating an email in the active account if open. So I guess I'm asking is there a way to get the frm address from the client?

  10. #40
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    Apparently I can't say
    MS-Off Ver
    Apparently I can't say
    Posts
    8,274

    Re: CDO email method

    No idea - I don't know your mail program, as I've said before, and I don't use CDO.

  11. #41
    Valued Forum Contributor mcinnes01's Avatar
    Join Date
    05-25-2010
    Location
    Manchester
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    446

    Re: CDO email method

    I have tried changing the way the CDO configures and it seems to be working, the only thing is that this is to be used on many peoples computers and I want it to pull in there email address and password.

    Is this possible?

    Here is my working code:

    Sub EMAILnSAVE()
    
        Dim FileExtStr As String
        Dim FileFormatNum As Long
        Dim Sourcewb As Object
        Dim Destwb As Object
        Dim cell As Long
        Dim NR As Long
        Dim wsData As Worksheet
        Dim wsCSV As Worksheet
        Dim SaveStr As String
        Dim tagerror As String
        Dim Email_Send_To, Email_Send_From, Email_Subject, Email_Body As String
        Dim strUserEmail As String
        Dim strFirstClassPassword As String
        Dim errPar As String
        Dim iMsg As Object
        Dim iConfig As Object
        Dim sConfig As Variant
        
            
        strUserEmail = "me@myemail.ac.uk"
        strFirstClassPassword = "password"
    
        Set iMsg = CreateObject("CDO.Message")
        Set iConfig = CreateObject("CDO.Configuration")
        iConfig.Load -1
        Set sConfig = iConfig.Fields
            
            With sConfig
                .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
                .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "192.168.0.5" 'Name or IP of remote SMTP server
                .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25  'Server Port
                .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = strUserEmail
                .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = strFirstClassPassword
                .Update
            End With
         
    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With
        
        Set Sourcewb = ThisWorkbook
            
            With Sourcewb
                Set wsData = .Sheets("OUTPUT")
                Set wsCSV = .Worksheets.Add(After:=.Sheets(.Sheets.Count))
            End With
        
        Set Destwb = Application.Workbooks.Add
            
            With Destwb.Worksheets("Sheet1")
                .Range("A1") = "EMP_ID"
                .Range("B1") = "KnownAs"
                .Range("C1") = "JobTitle"
                .Range("D1") = "LineManager"
                .Range("E1") = "ReportedSick"
                .Range("F1") = "StartDate"
                .Range("G1") = "EndDate"
                .Range("H1") = "Comments"
                .Range("A2").Name = "Area"
            End With
        
                With wsData
                    NR = .Cells(.Rows.Count, "A").End(xlUp).Row
                        .Range("A2:H" & NR).Copy Destination:=Destwb.Worksheets("Sheet1").Range("Area")
                End With
    
       If Val(Application.Version) < 12 Then
          ' You are using Excel 97-2003.
          FileExtStr = ".xls": FileFormatNum = -4143
       Else
            Select Case Sourcewb.FileFormat
            
               ' Code 51 represents the enumeration for a macro-free
               ' Excel 2007 Workbook (.xlsx).
               Case 51
                    FileExtStr = ".xlsx"
                    FileFormatNum = 51
                    
               ' Code 52 represents the enumeration for a
               ' macro-enabled Excel 2007 Workbook (.xlsm).
               Case 52
                     FileExtStr = ".xlsm"
                     FileFormatNum = 52
               
               ' Code 56 represents the enumeration for a
               ' a legacy Excel 97-2003 Workbook (.xls).
               Case 56
                    FileExtStr = ".xls"
                    FileFormatNum = 56
               ' Code 50 represents the enumeration for a
               ' binary Excel 2007 Workbook (.xlsb).
                Case Else
                    FileExtStr = ".xlsb"
                    FileFormatNum = 50
            End Select
       End If
    
        SaveStr = CreateObject("WScript.Shell").SpecialFolders("Desktop") _
                & Application.PathSeparator _
                & ActiveSheet.Name _
                & Environ("USERNAME") _
                & " - " _
                & Format(Now, " d-m-yy h.m AM/PM")
        
     '-----------------------------------------------------------------------------
    
        Email_Send_To = "andrew.mcinnes@themanchestercollege.ac.uk"
        Email_Send_From = "andrew.mcinnes@themanchestercollege.ac.uk"
        Email_Subject = "OSP " & Format(Now, "mm/yyyy")
        Email_Body = "MANAGERS DETAILS - " & Sourcewb.Worksheets("INPUT").Range("D15") & vbNewLine & vbNewLine _
                     & Sourcewb.Worksheets("INPUT").Range("C15") & " - " & Sourcewb.Worksheets("INPUT").Range("E15") & "   -   " & Sourcewb.Worksheets("INPUT").Range("F15") & vbNewLine & vbNewLine _
                     & " DEPARTMENT - " & Sourcewb.Worksheets("INPUT").Range("G15") & "          DIVISION - " & Sourcewb.Worksheets("INPUT").Range("H15") & vbNewLine _
                     & "HEAD OF DEPARTMENT - " & Sourcewb.Worksheets("INPUT").Range("I15") & "      SITE - " & Sourcewb.Worksheets("INPUT").Range("J15") & vbNewLine _
                     & "CONTACT NUMBER - " & Sourcewb.Worksheets("INPUT").Range("K15")
    
    '-----------------------------------------------------------------------------
               
    
        
        With Destwb
           .SaveAs SaveStr & FileExtStr, FileFormat:=FileFormatNum
           .Close SaveChanges:=False
           On Error Resume Next
        End With
        
        With iMsg
            Set .Configuration = iConfig
        End With
        
                iMsg.To = Email_Send_To
                iMsg.From = Email_Send_From
                iMsg.Subject = Email_Subject
                iMsg.Textbody = Email_Body
                iMsg.AddAttachment SaveStr & FileExtStr
                iMsg.Send
    
              On Error GoTo tagerror
    
        
    If ActiveSheet.Range("a1") = "" Then
        Application.DisplayAlerts = False
        ActiveSheet.Delete
        Application.DisplayAlerts = True
    
    Sourcewb.Activate
        Sheets("INPUT").Select
        
    Else
        Exit Sub
    End If
    
    clean_up:
        With Application
           .EnableEvents = True
           .ScreenUpdating = True
        End With
        Exit Sub
        
    tagerror:
        MsgBox "Error: (" & Err.Number & ") " & Err.Description & " at " & Err.Source, vbCritical
        Resume clean_up
        
    End Sub
    Last edited by mcinnes01; 10-29-2010 at 10:47 AM.

  12. #42
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    Apparently I can't say
    MS-Off Ver
    Apparently I can't say
    Posts
    8,274

    Re: CDO email method

    See this post.

  13. #43
    Valued Forum Contributor mcinnes01's Avatar
    Join Date
    05-25-2010
    Location
    Manchester
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    446

    Re: CDO email method

    HA! OK I get the message.

    Thank you so much for your help! I really wouldnt have got to this point without you!

    What I think I can do, as the code now bypasses the client completely, I can set up a new email address for the spreadsheet, so all the messages are sent through that email address.

    As the senders details are included in the body of the email I will know where it has come from.

    Thanks Again,

    Andy

    PS attached is a fully working example, if anyone is interested change the email details (server, from address, password etc) and it should work!
    Attached Files Attached Files
    Last edited by mcinnes01; 11-01-2010 at 05:43 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.2.0