+ Reply to Thread
Results 1 to 20 of 20

VBA stopped with Error-Message

Hybrid View

  1. #1
    Registered User
    Join Date
    08-27-2015
    Location
    Friedberg
    MS-Off Ver
    2010
    Posts
    68

    VBA stopped with Error-Message

    Hi all,

    I have the following VBA Code and an Excel file from which the VBA get his data (25 lines).
    My Problem is, that I do not have an attachment for every line. In this case the VBA will not finish.

    Sub Excel_Serial_Mail()
    
    
     Dim objOLOutlook As Object  
     Dim objOLMail As Object 
     Dim lngMailNr As Long  
     Dim lngZaehler As Long  
     
     Set objOLOutlook = CreateObject("Outlook.Application")
     lngMailNr = ActiveSheet.Cells(Rows.Count, 2).End(xlUp).Row 
    
     For lngZaehler = 2 To lngMailNr
     If Cells(lngZaehler, 2) <> "" Then
     Set objOLMail = objOLOutlook.CreateItem(olMailItem)
     With objOLMail
     .To = Cells(lngZaehler, 3)
     .CC = Cells(lngZaehler, 4)
     .BCC = "test@test.com"
     .SentOnBehalfOfName = "test@test.com"
     .Subject = Cells(lngZaehler, 6)
     .BodyFormat = olFormatPlain
     .Body = Cells(lngZaehler, 7)
     Dim strAttachmentPfad1 As String
     strAttachmentPfad1 = ActiveSheet.Cells(lngZaehler, 8)
     .Attachments.Add strAttachmentPfad1
     .Display
      End With
     Set objOLMail = Nothing
     End If
     Next lngZaehler  
     Set objOLOutlook = Nothing
    
     Exit Sub
    
     End Sub
    Last edited by torti111; 04-28-2016 at 03:05 AM.

  2. #2
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,968

    Re: problem with code

    Try this:
    Sub Excel_Serial_Mail()
    
        Dim objOLOutlook                As Object
        Dim objOLMail                   As Object
        Dim lngMailNr                   As Long
        Dim lngZaehler                  As Long
    
        Set objOLOutlook = CreateObject("Outlook.Application")
        lngMailNr = ActiveSheet.Cells(Rows.Count, 2).End(xlUp).Row
    
        For lngZaehler = 2 To lngMailNr
            If Cells(lngZaehler, 2) <> "" Then
                Set objOLMail = objOLOutlook.CreateItem(olMailItem)
                With objOLMail
                    .To = Cells(lngZaehler, 3)
                    .CC = Cells(lngZaehler, 4)
                    .BCC = "test@test.com"
                    .SentOnBehalfOfName = "test@test.com"
                    .Subject = Cells(lngZaehler, 6)
                    .BodyFormat = olFormatPlain
                    .Body = Cells(lngZaehler, 7)
                    Dim strAttachmentPfad1 As String
                    If Len(ActiveSheet.Cells(lngZaehler, 8).Value2) <> 0 Then .Attachments.Add ActiveSheet.Cells(lngZaehler, 8).Value2
                    .Display
                End With
                Set objOLMail = Nothing
            End If
        Next lngZaehler
        Set objOLOutlook = Nothing
    
    End Sub

  3. #3
    Registered User
    Join Date
    08-27-2015
    Location
    Friedberg
    MS-Off Ver
    2010
    Posts
    68

    Re: problem with code

    Hi Rorya,
    thank you for your help.
    But there is a Problem with the following part: .Attachments.Add ActiveSheet.Cells(lngZaehler, 8).Value2

  4. #4
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,968

    Re: problem with code

    What is the problem specifically?

  5. #5
    Registered User
    Join Date
    08-27-2015
    Location
    Friedberg
    MS-Off Ver
    2010
    Posts
    68

    Re: problem with code

    I have found, that the VBA works if I delete the path for the file that not exists. Perfect!
    Do you have an idea if I have for two lines sometimes two files to attach?

  6. #6
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,968

    Re: problem with code

    Are the two attachments listed in the same cell? If so, how are they separated? If not, where are they?

  7. #7
    Registered User
    Join Date
    08-27-2015
    Location
    Friedberg
    MS-Off Ver
    2010
    Posts
    68

    Re: problem with code

    how is the format to list them in the same cell? This is the first attachment in cell H3 = G:\Shareplan\test_31686\Plan\08\Sale Proceeds\sales_breakdown 210416 ret.pdf and the second file Name is sales breakdown 100516 ret.pdf

  8. #8
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,968

    Re: problem with code

    If you separate them with a comma, like:

    G:\Shareplan\test_31686\Plan\08\Sale Proceeds\sales_breakdown 210416 ret.pdf, G:\Shareplan\test_31686\Plan\08\Sale Proceeds\sales breakdown 100516 ret.pdf

    then you can use:
    Sub Excel_Serial_Mail()
    
        Dim objOLOutlook                As Object
        Dim objOLMail                   As Object
        Dim lngMailNr                   As Long
        Dim lngZaehler                  As Long
        Dim vAttachments
        Dim n                           As Long
    
        Set objOLOutlook = CreateObject("Outlook.Application")
        lngMailNr = ActiveSheet.Cells(Rows.Count, 2).End(xlUp).Row
    
        For lngZaehler = 2 To lngMailNr
            If Cells(lngZaehler, 2) <> "" Then
                Set objOLMail = objOLOutlook.CreateItem(olMailItem)
                With objOLMail
                    .To = Cells(lngZaehler, 3)
                    .CC = Cells(lngZaehler, 4)
                    .BCC = "test@test.com"
                    .SentOnBehalfOfName = "test@test.com"
                    .Subject = Cells(lngZaehler, 6)
                    .BodyFormat = olFormatPlain
                    .Body = Cells(lngZaehler, 7)
    
                    If Len(ActiveSheet.Cells(lngZaehler, 8).Value2) <> 0 Then
                        vAttachments = VBA.Split(ActiveSheet.Cells(lngZaehler, 8).Value2, ",")
                        For n = LBound(vAttachments) To UBound(vAttachments)
                            If VBA.Dir$(vAttachments(n)) <> vbNullString Then .Attachments.Add vAttachments(n)
                        Next n
                    End If
                    .Display
                End With
                Set objOLMail = Nothing
            End If
        Next lngZaehler
        Set objOLOutlook = Nothing
    
    End Sub
    which will check each file to see if it exists and add it to the email if it does.

  9. #9
    Registered User
    Join Date
    08-27-2015
    Location
    Friedberg
    MS-Off Ver
    2010
    Posts
    68

    Re: problem with code

    Hi Rorya,

    thanks a lot.
    But I have 24 different lines (for each City I have one line because in this line are different E-Mail addresses and subjects). Most of the cities have one attachment. 2 or 3 no attachment but not always the same and two of them have two attachments. The path is from h2 to h25.
    Do you have a solution for this?

  10. #10
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,920

    Re: problem with code

    torti111,
    there are two problems with this thread:

    1. Your post does not comply with Rule 1 of our Forum RULES. Your post title should accurately and concisely describe your problem, not your anticipated solution.

    Use terms appropriate to a Google search. Poor thread titles, like Please Help, Urgent, Need Help, Formula Problem, Code Problem, and Need Advice will be addressed according to the OP's experience in the forum: If you have less than 10 posts, expect (and respond to) a request to change your thread title. If you have 10 or more posts, expect your post to be locked, so you can start a new thread with an appropriate title.

    To change a Title go to your first post, click EDIT then Go Advanced and change your title, if 2 days have passed ask a moderator to do it for you.

    2. Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE] [/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here



    (This thread should receive no further responses until this moderation request is fulfilled, as per Forum Rule 7)
    Ben Van Johnson

  11. #11
    Registered User
    Join Date
    08-27-2015
    Location
    Friedberg
    MS-Off Ver
    2010
    Posts
    68

    Re: problem with code

    Hi Ben,

    I have changed the two Points.

    Kind regards,

    Thorsten

  12. #12
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    53,047

    Re: the following code works not fine

    Sorry, but I fail to see how "the following code works not fine" is any more descriptive of your problem than "problem with code" is?

    Your thread title needs to be something descriptive of your problem - not what you think the answer might be. (think google search terms?). Once you have done this please send me a PM and I will remove this request. (Also, include a link to your thread - copy from the address bar)

    Many members search our previous posts, and thread titles play a big part of the search. I doubt anybody would do a search based on your title?

    To change a Title on your post, click EDIT POST then Go Advanced and change your title, if 2 days have passed ask a moderator to do it for you.

    (note: this change is not optional )
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  13. #13
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,968

    Re: the following code works not fine

    When you've done that, please clarify what the problem is with the last code.

  14. #14
    Registered User
    Join Date
    08-27-2015
    Location
    Friedberg
    MS-Off Ver
    2010
    Posts
    68

    Re: VBA stopped with Error-Message

    I have 24 different lines (for each City I have one line because in this line are different E-Mail addresses and subjects). Most of the cities have one attachment. 2 or 3 no attachment but not always the same and two of them have two attachments. The path is from h2 to h25.
    If I use a comma between the file path for each of them I get the following error message: Run-time error 52 - Bad file Name or numer.

    The code works for every line with 1 attachment fine. If I have a second attachment I got this error.

  15. #15
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,968

    Re: VBA stopped with Error-Message

    Please give an exact example of the contents when there are two files.

  16. #16
    Registered User
    Join Date
    08-27-2015
    Location
    Friedberg
    MS-Off Ver
    2010
    Posts
    68

    Re: VBA stopped with Error-Message

    Hi,
    for two cities I have in a Special path sometimes two files which have to be attached:
    for example: G:\Sale Proceeds\sales_breakdown 210416 bah br.pdf and G:\Sale Proceeds\sales_breakdown 210416 bah tr.pdf

  17. #17
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,968

    Re: VBA stopped with Error-Message

    Those are not separated by a comma.

  18. #18
    Registered User
    Join Date
    08-27-2015
    Location
    Friedberg
    MS-Off Ver
    2010
    Posts
    68

    Re: VBA stopped with Error-Message

    If I make it in this way: "G:\Sale Proceeds\sales_breakdown 210416 bah br.pdf,G:\Sale Proceeds\sales_breakdown 210416 bah tr.pdf"
    I get an error message: Run-time error 52 - Bad file Name or number.

  19. #19
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,968

    Re: VBA stopped with Error-Message

    I take it you don't actually have the " in the cell?

    Which line gives the error?

  20. #20
    Registered User
    Join Date
    08-27-2015
    Location
    Friedberg
    MS-Off Ver
    2010
    Posts
    68

    Re: VBA stopped with Error-Message

    Hi Rorya,
    I don't have the " in the cell.
    I have copied the code into a new file and now it works.
    I have added: ".Save and .Close olPromtForSave "and now I have all E-Mails in my drafts - perfect.
    Now the code drafts an E-Mail for every line. Is it easy to Change the code, that he don't create an E-Mail if there is no file to attach?

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Vba save as pdf code problem in code any help please ?
    By tytyseka in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-11-2015, 11:33 PM
  2. vba code problem
    By sianjialin in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-26-2013, 09:44 PM
  3. [SOLVED] Code Problem: How to Write this into a VBA code
    By rlkerr in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-28-2013, 02:46 AM
  4. Replies: 2
    Last Post: 01-22-2013, 07:09 AM
  5. [SOLVED] problem with code
    By Ana in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-27-2006, 07:25 AM
  6. [SOLVED] if & and code problem
    By jocker in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 01-06-2006, 09:30 AM
  7. VBA Code Problem
    By Shawn in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 07-19-2005, 12:05 AM

Tags for this Thread

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.6.0 RC 1