Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, below code generates an email. While runing under Win7/2010Office it is showing Run-time error '9'
VB
Private Sub CreateEmails_Click()
Call PrepareEmail
End Sub

Public Sub PrepareEmail()

Dim sheetcount As Integer
Dim managerEmail As String
sheetcount = Worksheets.Count

For i = 1 To 30
    If Worksheets.Count < 4 Then
        Exit For
    Else
        If Worksheets(sheetcount).Name = "T" Then
            sheetcount = sheetcount - 1
        ElseIf Worksheets(sheetcount).Name = "Template" Then
            sheetcount = sheetcount - 1
        ElseIf Worksheets(sheetcount).Name = "Main" Then
            sheetcount = sheetcount - 1
        ElseIf Worksheets(sheetcount).Name = "Exceptions" Then
            sheetcount = sheetcount - 1
        Else
        Call SendManagerEmail(sheetcount)
        Application.DisplayAlerts = False
        Sheets(sheetcount).Delete
        Application.DisplayAlerts = True
        sheetcount = sheetcount - 1
    End If
End If
Next i
Posted
Updated 26-May-15 20:04pm
v2

1 solution

There are possible two reasons:
1) Overflow[^]
and
2) Subscript out of range (Error 9)[^]

Follow the links to find out how to fix this error. Use debugger and check the code step by step (using F8 key).
 
Share this answer
 
Comments
Member 11721449 27-May-15 6:59am    
Hi Maciej los,
thanks for replying soon, the above code is working fine in Xp /2003.While its going to run in Win7/2010 its show run time error '9' subscript out of range.
Maciej Los 27-May-15 7:19am    
Based on this piece of code i can't help you. As i mentioned, you should debug the code to find out where an error occurs.
Member 11721449 28-May-15 5:34am    
Application.ScreenUpdating = False

Worksheets(ManagerSheetNum).Activate
'Call RemoveRef

myDate = CDate(Now)
D = GetPlainDate
myMonth = FindMonth(Month(myDate))

'strDate = Worksheets("Main").Range("D20")
managerEmail = Worksheets(ManagerSheetNum).Name
templocation = Worksheets("Main").Range("D18") & "\"
TempFile = templocation & managerEmail & " " & Year(myDate) & "-" & myMonth & ".xlsm"
'Basestation = Worksheets("template").Range("G5")

strto = managerEmail
strsub = Worksheets("Main").Range("D22") & " - " & myMonth & " " & Year(myDate)

Worksheets(Array(Worksheets(ManagerSheetNum).Name)).Select
Worksheets(Array(Worksheets(ManagerSheetNum).Name)).Copy

Set wb = ActiveWorkbook
'Worksheets(ManagerSheetNum).Activate

Set OutMail = OutApp.CreateItemFromTemplate(EmailTemplate)
'Set OutMail = OutApp.CreateItemFromTemplate("\\svrau182frm04.oceania.corp.anz.com\TEPPy INTPO\SHARE\Network Security\REVIEWS\Reviews - 2011\Feb 2011\GPP\GPP.msg")
OutMail.Display

With wb
wb.Colors = Workbooks("IC Box user access review v1.1.xlsm").Colors

.SaveAs TempFile /* RUN - TIME ERROR 9 */


With OutMail '= OutApp.CreateItemFromTemplate("g:\zUAM\GPP" & "\" & "test.oft")
.FlagDueBy = duedate
.SentOnBehalfOfName = "Access Review - Payments and Cash Operations"
'.SentOnBehalfOfName = "Payments & Cash - User Access Management"
.To = strto
'.CC = "Access Review - Payments and Cash Operations"
'.CC = "Payments & Cash - User Access Management"
.Subject = strsub

'.HTMLBody = "" & .HTMLBody
'OutMail.Body = OutMail.Body

.Attachments.Add wb.FullName

'OutMail.Display
End With
'Set OutMail = OutApp.CreateItemFromTemplate("g:\zUAM\GPP" & "\" & "test.oft")

End With


ActiveWorkbook.Saved = True
ActiveWindow.Close

Application.ScreenUpdating = True

'Call DeleteTempFiles(templocation)

Exit Sub
End Sub Improve question
Member 11721449 28-May-15 5:39am    
Hi MaciejLos,
Thanks again for reply me soon, I hope the above code will help you to find solution for an Error that i have mantioned on paticular line i-e run Time error '9' on .Save TempFile

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900