Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2015
    Location
    Wales. Land of the sheep.
    Posts
    1,228

    Counting a loop.

    The majority of this code is fine, I'm trying to add a count to it so, in the email I can show how many attachments there are.

    Here are the lines I have added myself:


    Code:
    [Dim loopcnt As Variant
    Set loopcnt = "0"
    loopcnt = loopcnt + 1

    This line causes error: compile error: object required.
    Code:
    Set loopcnt = "0"
    Full code:
    Code:
    Private Sub Command32_Click()
    
        Dim rst As DAO.Recordset
        Dim appOutLook As Outlook.Application
        Dim MailOutLook As Outlook.MailItem
        Dim strPath As String
        Dim strFile As String
        Dim loopcnt As Variant
        
        Set appOutLook = CreateObject("Outlook.Application")
        Set MailOutLook = appOutLook.CreateItem(olMailItem)
        Set rst = Me.RecordsetClone
        Set loopcnt = "0"
        
        rst.MoveLast
        rst.MoveFirst
        
        
        Do Until rst.EOF
            strPath = "\\ramptecserver\finance\invoices\" & Company_Name & "" & rst!Order_Number & ""
            
            strFile = rst!Order_Number & "-" & rst!Order_ID & "-" & rst!On_Issue & ".pdf"
                    Debug.Print strPath
                   Debug.Print strFile
            If strFile <> "" Then
              loopcnt = loopcnt + 1
                With MailOutLook
                    .BodyFormat = olFormatRichText
                    .To = "bob@builder.com"    '<<-- all emails go to same email address???
                    ''.cc = ""
                    ''.bcc = ""
                    .Subject = "text here" & loopcnt
                    .HTMLBody = "text here"
                    .Attachments.Add (strPath & strFile)
                    '.Send
                    .Display    'Used during testing without sending (Comment out .Send if using this line)
                End With
            End If
            rst.MoveNext
        Loop
        'close recordset and clean up
        rst.Close
        Set rst = Nothing
        Set appOutLook = Nothing
        Set MailOutLook = Nothing
        MsgBox "Done"
    End Sub
    When this is counting as I want I can hopefully update the subject line in the email with how many attachments there are. Also I would like to limit the maximum number of attachments. So, lets say 10 is the maximum before it makes a new email and starts again until the record set is complete. Ill have a lot of reading up to do on this but all help appreciated.



    Thanks.

  2. #2
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,428
    declare loocnt as an integer and instead of

    Set loopcnt = "0"

    use

    loopcnt = 0

  3. #3
    Join Date
    Jun 2015
    Location
    Wales. Land of the sheep.
    Posts
    1,228
    Thank you very much. Ill attempt to stop it using the counter now... and create a new one. lets see how it goes haha.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 9
    Last Post: 03-07-2017, 02:49 PM
  2. Replies: 12
    Last Post: 06-05-2015, 04:27 PM
  3. Replies: 13
    Last Post: 08-20-2014, 09:17 AM
  4. Replies: 17
    Last Post: 04-07-2014, 07:48 PM
  5. Replies: 3
    Last Post: 03-10-2013, 07:04 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
  •  
Other Forums: Microsoft Office Forums