Subscript out of range

Saifuddin Mohammad 0 Reputation points
2023-04-11T14:21:21.9733333+00:00

Sub SendEmail_Demo1()

Dim EmailApp As Outlook.Application
Set EmailApp = New Outlook.Application
Dim NewEmailItem As Outlook.MailItem
Set NewEmailItem = EmailApp.CreateItem(olMailItem)


Dim i As Integer
For i = 4 To 27

 NewEmailItem.To = Workbooks("CATS_Compliance(1).xlsm").Worksheets("TB_Time_HRS").Cells(i, 7).Value
 NewEmailItem.Subject = "Macro_TWR Compliance - Missing TWR Hours"
 With NewEmailItem
  
 .BodyFormat = olFormatHTML
 
 .HTMLBody = "<HTML><BODY>You have a missing time report. Would you please submit the missing time writing at the earliest. </BODY></HTML>"
 
 .Display
 
 .Send
           
 End With

Next

End Sub
Microsoft 365 and Office | Development | Other
Outlook | Windows | Classic Outlook for Windows | For business
Microsoft 365 and Office | Excel | For business | Windows
{count} votes

1 answer

Sort by: Most helpful
  1. MotoX80 37,156 Reputation points
    2023-04-13T19:03:48.31+00:00

    not sure which line is having error....

    One thought is that the NewEmailItem object has been disposed of as part of the .Send call. Move the Set command inside of the loop and see if that works.

    Dim i As Integer 
    For i = 4 To 27
       Set NewEmailItem = EmailApp.CreateItem(olMailItem)
    
    

    Add msgbox or debug.print statements to display what steps your code is doing and what variable contents are. You have to figure out what statement and variable are causing your problem because we don't have access to your spreadsheet.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.