Overflow Issue

eekka

New Member
Joined
Apr 1, 2022
Messages
18
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hi guys,
I have been struggling with my macro for the whole day and I really don't know what to do. Basically macro is supposed to add attachments to the mail based on criteria set (to follow same order as in the file), however I am experiencing overflow issue all the time.

VBA Code:
Sub AttachFinalTermsPDFToEmail()
    Dim OutlookApp As Object
    Dim OutlookMail As Object
    Dim ws As Worksheet
    Dim LastRow As Long
    Dim i As Long
    Dim FilePath As String
    Dim FinalTermsNumber As String
    Dim FileName As String
    Dim FolderPath As String
    Dim TotalSize As Double
    Dim FileSize As Double
    Dim EmailLimit As Double
    Dim FileList As Collection
    Dim EmailCount As Integer
    Dim fso As Object

    On Error GoTo ErrorHandler

    ' Initialize FileSystemObject
    Set fso = CreateObject("Scripting.FileSystemObject")

    ' Initialize Outlook Application
    Set OutlookApp = CreateObject("Outlook.Application")

    ' Set the worksheet object
    Set ws = ThisWorkbook.Sheets("Tabelle 1")

    ' Set the folder path where the PDF files are stored
    FolderPath = ""

    ' Check if the folder path exists
    If Not fso.FolderExists(FolderPath) Then
        MsgBox "The specified folder path does not exist: " & FolderPath, vbCritical
        Exit Sub
    End If

    ' Set email attachment size limit (20MB in bytes)
    EmailLimit = 20 * 1024 * 1024

    ' Find the last row with data in the Final Terms No column
    LastRow = ws.Cells(ws.Rows.Count, "T").End(xlUp).Row

    ' Initialize a collection to hold file paths
    Set FileList = New Collection
    TotalSize = 0
    EmailCount = 1

    ' Loop through each Final Terms number starting from row 9
    For i = 9 To LastRow
        FinalTermsNumber = ws.Cells(i, 20).Value

        If Trim(FinalTermsNumber) <> "" Then
            FileName = Dir(FolderPath & "*-" & FinalTermsNumber & "*.pdf")

            Do While FileName <> ""
                FilePath = FolderPath & FileName
                FileSize = fso.GetFile(FilePath).Size

                If FileSize > EmailLimit Then
                    MsgBox "File " & FileName & " exceeds the email size limit and will not be attached.", vbExclamation
                ElseIf TotalSize + FileSize > EmailLimit Then
                    SendEmailWithAttachments OutlookApp, FileList, EmailCount
                    Set FileList = New Collection
                    TotalSize = 0
                    EmailCount = EmailCount + 1
                Else
                    FileList.Add FilePath
                    TotalSize = TotalSize + FileSize
                End If

                FileName = Dir()
            Loop
        End If
    Next i

    If FileList.Count > 0 Then
        SendEmailWithAttachments OutlookApp, FileList, EmailCount
    End If

    MsgBox "Emails created successfully. Please review and send them manually."

ExitSub:
    ' Release objects
    Set OutlookMail = Nothing
    Set OutlookApp = Nothing
    Exit Sub

ErrorHandler:
    MsgBox "An error occurred: " & Err.Description, vbCritical
    Resume ExitSub

End Sub

Sub SendEmailWithAttachments(OutlookApp As Object, FileList As Collection, EmailCount As Integer)
    Dim OutlookMail As Object
    Dim i As Integer

    ' Create a new mail item
    Set OutlookMail = OutlookApp.CreateItem(0)

    ' Add attachments to the mail item
    For i = 1 To FileList.Count
        OutlookMail.Attachments.Add FileList(i)
    Next i

    ' Customize email properties
    With OutlookMail
        .Subject = "Terms PDFs"
        .Body = "Please find attached the  Terms PDFs."
        .To = ""
        ' Add more recipients, CCs, etc. as needed
    End With

    ' Display the mail item (change .Send to .Display if you want to review before sending)
    OutlookMail.Display

    ' Release object
    Set OutlookMail = Nothing
End Sub
 
Last edited by a moderator:

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
VBA Code:
'Try changing
EmailLimit = 20 * 1024 * 1024
'to
EmailLimit = 20 * 1024 * 1024#
'or
EmailLimit = 20971520

Notwithstanding the fact that you've declared EmailLimit as Double (or Long, which would have been more appropriate), VBA sees 20*20*1024 as an integer, and tries to calculate the result as an integer, which has a maximum value of 32,767.
 
Upvote 0
Issue is it doesn’t open outlook at all. Gets the same output as my version
 
Upvote 0
You only mentioned an overflow problem.

Gets the same output as my version
I have no idea what this means? What is being "output"? What is "my version"? We can only comment on the code that you post.

Is this the actual code that you are running? At the moment, you have FolderPath = ""

Are you getting any error? If so, can you turn off the error handler and let us know which line errors, and what the error message is?

Also please let us know:

- What is the value of FolderPath?
- Whether this is a valid filename: FolderPath & "*-" & FinalTermsNumber & "*.pdf"
 
Upvote 0
You only mentioned an overflow problem.


I have no idea what this means? What is being "output"? What is "my version"? We can only comment on the code that you post.

Is this the actual code that you are running? At the moment, you have FolderPath = ""

Are you getting any error? If so, can you turn off the error handler and let us know which line errors, and what the error message is?

Also please let us know:

- What is the value of FolderPath?
- Whether this is a valid filename: FolderPath & "*-" & FinalTermsNumber & "*.pdf"
Hi,
what I mean is you can execute the code, you get the message that emails were created, however there are no mails, it doesn't open outlook at all. That is the issue

when it comes to folderpath it is just a folder on a drive that has a lot of files with different numbers, so according to the excel file they need to be inserted in outlook mail in that sequence.

Hope I clarified it more
 
Upvote 0
what I mean is you can execute the code, you get the message that emails were created, however there are no mails, it doesn't open outlook at all.
You have a logic error in your code. If the code doesn't error, you will always get the message that emails have been created successfully:

VBA Code:
If FileList.Count > 0 Then
    SendEmailWithAttachments OutlookApp, FileList, EmailCount
End If

MsgBox "Emails created successfully. Please review and send them manually."

If your code is getting this far, it suggests that you have a valid FolderPath, but that the code is not finding any PDFs with the specified format.

So I suggest you take a look at the value of:

FileName = Dir(FolderPath & "*-" & FinalTermsNumber & "*.pdf")

Do you have any PDFs with this format?
 
Last edited:
Upvote 0
Does FolderPath end with the relevant path separator for the OS?
 
Upvote 0
You have a logic error in your code. If the code doesn't error, you will always get the message that emails have been created successfully:

VBA Code:
If FileList.Count > 0 Then
    SendEmailWithAttachments OutlookApp, FileList, EmailCount
End If

MsgBox "Emails created successfully. Please review and send them manually."

If your code is getting this far, it suggests that you have a valid FolderPath, but that the code is not finding any PDFs with the specified format.

So I suggest you take a look at the value of:

FileName = Dir(FolderPath & "*-" & FinalTermsNumber & "*.pdf")

Do you have any PDFs with this format?
Very good point. In the folder there are multiple files, for example "Final Terms-1RX ...", and only the number changes. That is why I am working on this code, because numbers are not subsequent, rather in the file they are ordered randomly so according to the specific column which says Final Terms Number I want them to be added to the mail in that order
 
Upvote 0
It needs to end in a path separator, or you need to add one in the code. Otherwise you are adding the file name straight onto the end of the folder name in your Dir function.
 
Upvote 0

Forum statistics

Threads
1,224,759
Messages
6,180,814
Members
452,996
Latest member
nelsonsix66

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top