Outlook VBA Form not finding FOR LOOP -- Error message

Status
Not open for further replies.
M

Mitch

I'm using Outlook 2003 SP3, using a VBA Form created throug Outlook, it

basically checks some checkboxes for a value of True, then adds an email

address. It works perfectly fine when I do it as a macro.

When I plug it into the form, my FOR Loop "NEXT" isn't finding the loop. I

get a Next neesda for loop even though I have one in it.

Here's my code on Mouse click submit. [Note: NumberEmails.Value is a number

taken from a text box whos value is modifed by SpinButton]

Mind you I have replaced some string variables I hardcoding into the macro

as variables, so those have not been tested just yet, but I'd expect them to

work.

Private Sub SubmitButton_Click()

Set OutlookApp = CreateObject("Outlook.Application")

Dim objMail As Outlook.MailItem

Set objMail = OutlookApp.CreateItem(0)

Dim rcpt As Recipient

For i = 1 To NumberEmails.Value

Set objMail = Application.CreateItem(0)

If CB1.Value = True Then

Set rcpt = objMail.Recipients.Add(CB1.Tag)

If CB2.Value = True Then

Set rcpt = objMail.Recipients.Add(CB2.Tag)

If CB3.Value = True Then

Set rcpt = objMail.Recipients.Add(CB3.Tag)

If CB4.Value = True Then

Set rcpt = objMail.Recipients.Add(CB4.Tag)

If CB5.Value = True Then

Set rcpt = objMail.Recipients.Add(CB5.Tag)

If CB6.Value = True Then

Set rcpt = objMail.Recipients.Add(CB6.Tag)

If CB7.Value = True Then

Set rcpt = objMail.Recipients.Add(CB7.Tag)

If CB8.Value = True Then

Set rcpt = objMail.Recipients.Add(CB8.Tag)

If CB9.Value = True Then

Set rcpt = objMail.Recipients.Add(CB9.Tag)

If CB10.Value = True Then

Set rcpt = objMail.Recipients.Add(CB10.Tag)

If CB11.Value = True Then

Set rcpt = objMail.Recipients.Add(CB11.Tag)

If CB12.Value = True Then

Set rcpt = objMail.Recipients.Add(CB12.Tag)

If CB13.Value = True Then

Set rcpt = objMail.Recipients.Add(CB13.Tag)

If CB14.Value = True Then

Set rcpt = objMail.Recipients.Add(CB14.Tag)

If CB15.Value = True Then

Set rcpt = objMail.Recipients.Add(CB15.Tag)

If CB16.Value = True Then

Set rcpt = objMail.Recipients.Add(CB16.Tag)

If CB17.Value = True Then

Set rcpt = objMail.Recipients.Add(CB17.Tag)

If CB18.Value = True Then

Set rcpt = objMail.Recipients.Add(CB18.Tag)

If CB19.Value = True Then

Set rcpt = objMail.Recipients.Add(CB19.Tag)

If CB20.Value = True Then

Set rcpt = objMail.Recipients.Add(CB20.Tag)

Else: MsgBox ("No Emails Selected"), vbOKOnly

objMail.Subject = SubjectLine + CStr(i)

objMail.Body = "Test Message " + CStr(i)

Set myAtt = objMail.Attachments

myAtt.Add = AttachmentPath

objMail.Save

objMail.Send

Next

Set objMail = Nothing

Set myAtt = Nothing

Set rcpt = Nothing

End Sub

----
Private Sub SpinButton1_SpinDown()

If NumberEmails.Value = 1 Then NumberEmails.Value = 1 Else

NumberEmails.Value = NumberEmails.Value - 1

End Sub

----
Private Sub SpinButton1_SpinUp()

If NumberEmails.Value = 20 Then NumberEmails.Value = 20 Else

NumberEmails.Value = NumberEmails.Value + 1

End Sub

----------
 
M

Mitch

Found that my If statements were messing it up, wonder if there's a better

way to do this. A for loop to run through my 20 if statements ?

"Mitch" wrote:


> I'm using Outlook 2003 SP3, using a VBA Form created throug Outlook, it
> basically checks some checkboxes for a value of True, then adds an email
> address. It works perfectly fine when I do it as a macro.

> When I plug it into the form, my FOR Loop "NEXT" isn't finding the loop. I
> get a Next neesda for loop even though I have one in it.

> Here's my code on Mouse click submit. [Note: NumberEmails.Value is a number
> taken from a text box whos value is modifed by SpinButton]

> Mind you I have replaced some string variables I hardcoding into the macro
> as variables, so those have not been tested just yet, but I'd expect them to
> work.

> Private Sub SubmitButton_Click()

> Set OutlookApp = CreateObject("Outlook.Application")
> Dim objMail As Outlook.MailItem
> Set objMail = OutlookApp.CreateItem(0)

> Dim rcpt As Recipient

> For i = 1 To NumberEmails.Value
> Set objMail = Application.CreateItem(0)
> If CB1.Value = True Then
> Set rcpt = objMail.Recipients.Add(CB1.Tag)
> If CB2.Value = True Then
> Set rcpt = objMail.Recipients.Add(CB2.Tag)
> If CB3.Value = True Then
> Set rcpt = objMail.Recipients.Add(CB3.Tag)
> If CB4.Value = True Then
> Set rcpt = objMail.Recipients.Add(CB4.Tag)
> If CB5.Value = True Then
> Set rcpt = objMail.Recipients.Add(CB5.Tag)
> If CB6.Value = True Then
> Set rcpt = objMail.Recipients.Add(CB6.Tag)
> If CB7.Value = True Then
> Set rcpt = objMail.Recipients.Add(CB7.Tag)
> If CB8.Value = True Then
> Set rcpt = objMail.Recipients.Add(CB8.Tag)
> If CB9.Value = True Then
> Set rcpt = objMail.Recipients.Add(CB9.Tag)
> If CB10.Value = True Then
> Set rcpt = objMail.Recipients.Add(CB10.Tag)
> If CB11.Value = True Then
> Set rcpt = objMail.Recipients.Add(CB11.Tag)
> If CB12.Value = True Then
> Set rcpt = objMail.Recipients.Add(CB12.Tag)
> If CB13.Value = True Then
> Set rcpt = objMail.Recipients.Add(CB13.Tag)
> If CB14.Value = True Then
> Set rcpt = objMail.Recipients.Add(CB14.Tag)
> If CB15.Value = True Then
> Set rcpt = objMail.Recipients.Add(CB15.Tag)
> If CB16.Value = True Then
> Set rcpt = objMail.Recipients.Add(CB16.Tag)
> If CB17.Value = True Then
> Set rcpt = objMail.Recipients.Add(CB17.Tag)
> If CB18.Value = True Then
> Set rcpt = objMail.Recipients.Add(CB18.Tag)
> If CB19.Value = True Then
> Set rcpt = objMail.Recipients.Add(CB19.Tag)
> If CB20.Value = True Then
> Set rcpt = objMail.Recipients.Add(CB20.Tag)
> Else: MsgBox ("No Emails Selected"), vbOKOnly

> objMail.Subject = SubjectLine + CStr(i)
> objMail.Body = "Test Message " + CStr(i)

> Set myAtt = objMail.Attachments
> myAtt.Add = AttachmentPath

> objMail.Save
> objMail.Send
> Next

> Set objMail = Nothing
> Set myAtt = Nothing
> Set rcpt = Nothing

> End Sub
> ----> Private Sub SpinButton1_SpinDown()
> If NumberEmails.Value = 1 Then NumberEmails.Value = 1 Else
> NumberEmails.Value = NumberEmails.Value - 1
> End Sub
> ----> Private Sub SpinButton1_SpinUp()
> If NumberEmails.Value = 20 Then NumberEmails.Value = 20 Else
> NumberEmails.Value = NumberEmails.Value + 1
> End Sub
> ----------
 
M

Mitch

Got it working, had to also remove = from the myAtt.Add line.

"Mitch" wrote:


> Found that my If statements were messing it up, wonder if there's a better
> way to do this. A for loop to run through my 20 if statements ?

> "Mitch" wrote:
>
> > I'm using Outlook 2003 SP3, using a VBA Form created throug Outlook, it
> > basically checks some checkboxes for a value of True, then adds an email
> > address. It works perfectly fine when I do it as a macro.
> > When I plug it into the form, my FOR Loop "NEXT" isn't finding the loop. I
> > get a Next neesda for loop even though I have one in it.
> > Here's my code on Mouse click submit. [Note: NumberEmails.Value is a number
> > taken from a text box whos value is modifed by SpinButton]
> > Mind you I have replaced some string variables I hardcoding into the macro
> > as variables, so those have not been tested just yet, but I'd expect them to
> > work.
> > Private Sub SubmitButton_Click()
> > Set OutlookApp = CreateObject("Outlook.Application")
> > Dim objMail As Outlook.MailItem
> > Set objMail = OutlookApp.CreateItem(0)
> > Dim rcpt As Recipient
> > For i = 1 To NumberEmails.Value
> > Set objMail = Application.CreateItem(0)
> > If CB1.Value = True Then
> > Set rcpt = objMail.Recipients.Add(CB1.Tag)
> > If CB2.Value = True Then
> > Set rcpt = objMail.Recipients.Add(CB2.Tag)
> > If CB3.Value = True Then
> > Set rcpt = objMail.Recipients.Add(CB3.Tag)
> > If CB4.Value = True Then
> > Set rcpt = objMail.Recipients.Add(CB4.Tag)
> > If CB5.Value = True Then
> > Set rcpt = objMail.Recipients.Add(CB5.Tag)
> > If CB6.Value = True Then
> > Set rcpt = objMail.Recipients.Add(CB6.Tag)
> > If CB7.Value = True Then
> > Set rcpt = objMail.Recipients.Add(CB7.Tag)
> > If CB8.Value = True Then
> > Set rcpt = objMail.Recipients.Add(CB8.Tag)
> > If CB9.Value = True Then
> > Set rcpt = objMail.Recipients.Add(CB9.Tag)
> > If CB10.Value = True Then
> > Set rcpt = objMail.Recipients.Add(CB10.Tag)
> > If CB11.Value = True Then
> > Set rcpt = objMail.Recipients.Add(CB11.Tag)
> > If CB12.Value = True Then
> > Set rcpt = objMail.Recipients.Add(CB12.Tag)
> > If CB13.Value = True Then
> > Set rcpt = objMail.Recipients.Add(CB13.Tag)
> > If CB14.Value = True Then
> > Set rcpt = objMail.Recipients.Add(CB14.Tag)
> > If CB15.Value = True Then
> > Set rcpt = objMail.Recipients.Add(CB15.Tag)
> > If CB16.Value = True Then
> > Set rcpt = objMail.Recipients.Add(CB16.Tag)
> > If CB17.Value = True Then
> > Set rcpt = objMail.Recipients.Add(CB17.Tag)
> > If CB18.Value = True Then
> > Set rcpt = objMail.Recipients.Add(CB18.Tag)
> > If CB19.Value = True Then
> > Set rcpt = objMail.Recipients.Add(CB19.Tag)
> > If CB20.Value = True Then
> > Set rcpt = objMail.Recipients.Add(CB20.Tag)
> > Else: MsgBox ("No Emails Selected"), vbOKOnly
> > objMail.Subject = SubjectLine + CStr(i)
> > objMail.Body = "Test Message " + CStr(i)
> > Set myAtt = objMail.Attachments
> > myAtt.Add = AttachmentPath
> > objMail.Save
> > objMail.Send
> > Next
> > Set objMail = Nothing
> > Set myAtt = Nothing
> > Set rcpt = Nothing
> > End Sub
> > ----> > Private Sub SpinButton1_SpinDown()
> > If NumberEmails.Value = 1 Then NumberEmails.Value = 1 Else
> > NumberEmails.Value = NumberEmails.Value - 1
> > End Sub
> > ----> > Private Sub SpinButton1_SpinUp()
> > If NumberEmails.Value = 20 Then NumberEmails.Value = 20 Else
> > NumberEmails.Value = NumberEmails.Value + 1
> > End Sub
> > ----------
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
H Custom Outlook Contact Form VBA Outlook VBA and Custom Forms 1
D create an html table in outlook custom form 2010 using vba in MsAccess Outlook VBA and Custom Forms 7
G Adding a contact to Outlook with a custom form using Access VBA Outlook VBA and Custom Forms 1
M VBA Code to extract data from an Outlook Form Using Outlook 0
J VBA Form in Outlook to capture data and complile a mail Outlook VBA and Custom Forms 2
J Outlook custom form - VBS call VBA macro Outlook VBA and Custom Forms 3
M VBA Form how to load in Outlook 2003 Outlook VBA and Custom Forms 4
S Excel vba code to manage outlook web app Using Outlook 8
S Problem Checking the available stores in my Inbox (Outlook VBA) Outlook VBA and Custom Forms 0
S Outlook VBA How to adapt this code for using in a different Mail Inbox Outlook VBA and Custom Forms 0
O VBA Outlook Message Attachment - Array Index Out of Bounds Outlook VBA and Custom Forms 0
J Want to learn VBA Macros for Outlook. What book can you recommend? Outlook VBA and Custom Forms 2
M Outlook 2013 reminder email by using Outlook vba Outlook VBA and Custom Forms 2
D Outlook VBA error extracting property data from GetRules collection Outlook VBA and Custom Forms 10
O Email not leaving Outbox when using Excel VBA to sync Outlook account Outlook VBA and Custom Forms 4
L Moving emails with similar subject and find the timings between the emails using outlook VBA macro Outlook VBA and Custom Forms 1
B Outlook Business Contact Manager with SQL to Excel, User Defined Fields in BCM don't sync in SQL. Can I use VBA code to copy 1 field to another? BCM (Business Contact Manager) 0
N How can I increase/faster outlook VBA Macro Speed ? Using Outlook 2
N Outlook Email Rule execution through shortcut keys (VBA codes) Using Outlook 1
A VBA Code in Outlook disappears after first use Outlook VBA and Custom Forms 1
dweller Outlook 2010 Rule Ignores VBA Script Outlook VBA and Custom Forms 2
G Outlook VBA and Google Calendar ("Events") Outlook VBA and Custom Forms 1
J VBA Outlook : Subject line : Cut and Paste name to heading , number to very end of the body of Email Outlook VBA and Custom Forms 1
B Advanced Search in MS Outlook by VBA and SQL Outlook VBA and Custom Forms 2
K Outlook Archive to PST Files by Date Range VBA Script? Outlook VBA and Custom Forms 1
J Help Please!!! Outlook 2016 - VBA Macro for replying with attachment in meeting invite Outlook VBA and Custom Forms 9
S Find a cell value in excel using outlook vba Using Outlook 1
J Execute Add-In Button from VBA Outlook 2016 Outlook VBA and Custom Forms 1
J Open an outlook email by Subject on MS Access linked table with VBA Outlook VBA and Custom Forms 10
M Slow VBA macro in Outlook Outlook VBA and Custom Forms 5
T Outlook AntiSpam with VBA Outlook VBA and Custom Forms 1
F "Move to" O365 feature to Outlook client via VBA Outlook VBA and Custom Forms 4
B query outlook using vba Outlook VBA and Custom Forms 13
J VBA to switch Outlook online/offline Outlook VBA and Custom Forms 4
M VBA to change flag status in outlook contact item Outlook VBA and Custom Forms 3
T VBA outlook, detect priority emails Outlook VBA and Custom Forms 5
C Need VBA code to automatically save message outside outlook and add date Outlook VBA and Custom Forms 1
stephen li VBA Outlook send mail automatically by specified outlook mail box Outlook VBA and Custom Forms 1
S Outlook VBA Contacts Notes Outlook VBA and Custom Forms 0
O VBA to Run Font Change on Outlook Startup Outlook VBA and Custom Forms 4
P Outlook 2007 Email Categorization using VBA Outlook VBA and Custom Forms 1
O VBA or other solution for Outlook tasks to OneNote Outlook VBA and Custom Forms 0
S Automatically selecting folders and deleting messages in Outlook VBA Outlook VBA and Custom Forms 7
D Creating an outlook session from Access vba but run silently. With A specific profile Outlook VBA and Custom Forms 1
Diane Poremsky Outlook VBA: Use a Text File to Populate a ListBox New Slipstick.com Articles 0
C Saving Outlook attachments and links to attachments with VBA Outlook VBA and Custom Forms 2
Diane Poremsky Outlook VBA: Work with Open Item or Selected Item New Slipstick.com Articles 0
Diane Poremsky Working with VBA and non-default Outlook Folders New Slipstick.com Articles 0
C Outlook VBA to set current account Outlook VBA and Custom Forms 1
Diane Poremsky Use VBA to create an Outlook Search Folder for Sender New Slipstick.com Articles 0

Similar threads

Top