Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 246
Default WHY OH WHY! ... creating a dynamic array of outlookmail items, then display them.

I'm using the following code, with NO reference to outlook:


Dim objOutlook As Object
Dim objMailItem As Object

Dim myCreatedEmails() As Object

Sub CreateAndDisplayEmails()

Set objOutlook = CreateObject("Outlook.Application")

Erase myCreatedEmails

For i = 1 To 3

Set objMailItem = objOutlook.CreateItem(0)

With objMailItem
.To = "Tester" & i
.Subject = "Tester" & i
End With

ReDim myCreatedEmails(k)
'##### falls down on next line #####
myCreatedEmails(k) = objMailItem
k = k + 1

Next i

For k = 1 To UBound(myCreatedEmails)
myCreatedEmails(k).Display
Next k

End Sub


....I've marked where it already falls down and I assume even if this
line is fixed then it'll fall down later on on the line
"myCreatedEmails(k).Display"

The above must be pretty bl##dy close!...can anyone help?

Help appreciated
Jason

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 227
Default WHY OH WHY! ... creating a dynamic array of outlookmail items, then display them.

How does it "fall down"? That is, what error do you get?

Just a guess, but it looks like you would lose previous myCreatedEmails
entries as you run through your For/Next loop because you used ReDim
instead of ReDim Preserve. ReDim by itself clears the array.

WhytheQ wrote:
I'm using the following code, with NO reference to outlook:


Dim objOutlook As Object
Dim objMailItem As Object

Dim myCreatedEmails() As Object

Sub CreateAndDisplayEmails()

Set objOutlook = CreateObject("Outlook.Application")

Erase myCreatedEmails

For i = 1 To 3

Set objMailItem = objOutlook.CreateItem(0)

With objMailItem
.To = "Tester" & i
.Subject = "Tester" & i
End With

ReDim myCreatedEmails(k)
'##### falls down on next line #####
myCreatedEmails(k) = objMailItem
k = k + 1

Next i

For k = 1 To UBound(myCreatedEmails)
myCreatedEmails(k).Display
Next k

End Sub


...I've marked where it already falls down and I assume even if this
line is fixed then it'll fall down later on on the line
"myCreatedEmails(k).Display"

The above must be pretty bl##dy close!...can anyone help?

Help appreciated
Jason


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default WHY OH WHY! ... creating a dynamic array of outlookmail items, the

A couple of things wrong with that off the top of my head.
1. You are missing the "preserve" key word. Without that each time you redim
all of the previous items stored in the array will be blown away. you
probably want.
ReDim Preserve myCreatedEmails(k)

2. An object reference requires the "Set" key word.
set myCreatedEmails(k) = objMailItem

Just curious. Why not use a collection, instead of a dynamic array. Ususally
they are a bit more handy for storing groups of objects...

dim colCreatedEmails as collection

set colCreatedEmails as new collection

colCreatedEmails.add objMailItem, "Some Unique Key"

--
HTH...

Jim Thomlinson


"WhytheQ" wrote:

I'm using the following code, with NO reference to outlook:


Dim objOutlook As Object
Dim objMailItem As Object

Dim myCreatedEmails() As Object

Sub CreateAndDisplayEmails()

Set objOutlook = CreateObject("Outlook.Application")

Erase myCreatedEmails

For i = 1 To 3

Set objMailItem = objOutlook.CreateItem(0)

With objMailItem
.To = "Tester" & i
.Subject = "Tester" & i
End With

ReDim myCreatedEmails(k)
'##### falls down on next line #####
myCreatedEmails(k) = objMailItem
k = k + 1

Next i

For k = 1 To UBound(myCreatedEmails)
myCreatedEmails(k).Display
Next k

End Sub


....I've marked where it already falls down and I assume even if this
line is fixed then it'll fall down later on on the line
"myCreatedEmails(k).Display"

The above must be pretty bl##dy close!...can anyone help?

Help appreciated
Jason


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 246
Default WHY OH WHY! ... creating a dynamic array of outlookmail items, the

nice one Jim
J


Jim Thomlinson wrote:

A couple of things wrong with that off the top of my head.
1. You are missing the "preserve" key word. Without that each time you redim
all of the previous items stored in the array will be blown away. you
probably want.
ReDim Preserve myCreatedEmails(k)

2. An object reference requires the "Set" key word.
set myCreatedEmails(k) = objMailItem

Just curious. Why not use a collection, instead of a dynamic array. Ususally
they are a bit more handy for storing groups of objects...

dim colCreatedEmails as collection

set colCreatedEmails as new collection

colCreatedEmails.add objMailItem, "Some Unique Key"

--
HTH...

Jim Thomlinson


"WhytheQ" wrote:

I'm using the following code, with NO reference to outlook:


Dim objOutlook As Object
Dim objMailItem As Object

Dim myCreatedEmails() As Object

Sub CreateAndDisplayEmails()

Set objOutlook = CreateObject("Outlook.Application")

Erase myCreatedEmails

For i = 1 To 3

Set objMailItem = objOutlook.CreateItem(0)

With objMailItem
.To = "Tester" & i
.Subject = "Tester" & i
End With

ReDim myCreatedEmails(k)
'##### falls down on next line #####
myCreatedEmails(k) = objMailItem
k = k + 1

Next i

For k = 1 To UBound(myCreatedEmails)
myCreatedEmails(k).Display
Next k

End Sub


....I've marked where it already falls down and I assume even if this
line is fixed then it'll fall down later on on the line
"myCreatedEmails(k).Display"

The above must be pretty bl##dy close!...can anyone help?

Help appreciated
Jason



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 246
Default WHY OH WHY! ... creating a dynamic array of outlookmail items, the

Hi Jim
How about the below? (on a machine without outlook at the moment so
can't unfortunately test it)

Option Explicit

Dim objOutlook As Object
Dim objMailItem As Object

Dim i As Integer

Dim myCreatedEmails As Collection


Sub CreateAndDisplayEmails()


Set objOutlook = CreateObject("Outlook.Application")

Set myCreatedEmails = New Collection

For i = 1 To 3


Set objMailItem = objOutlook.CreateItem(0)


With objMailItem
.To = "Tester" & i
.Subject = "Tester" & i
End With

myCreatedEmails.Add objMailItem, i

Next i


For i = 1 To myCreatedEmails.Count
myCreatedEmails(i).Display
Next i


End Sub

I'm not too sure if the Display method is going to work?!
Help greatly appreciated.
Jason.


WhytheQ wrote:

nice one Jim
J


Jim Thomlinson wrote:

A couple of things wrong with that off the top of my head.
1. You are missing the "preserve" key word. Without that each time you redim
all of the previous items stored in the array will be blown away. you
probably want.
ReDim Preserve myCreatedEmails(k)

2. An object reference requires the "Set" key word.
set myCreatedEmails(k) = objMailItem

Just curious. Why not use a collection, instead of a dynamic array. Ususally
they are a bit more handy for storing groups of objects...

dim colCreatedEmails as collection

set colCreatedEmails as new collection

colCreatedEmails.add objMailItem, "Some Unique Key"

--
HTH...

Jim Thomlinson


"WhytheQ" wrote:

I'm using the following code, with NO reference to outlook:


Dim objOutlook As Object
Dim objMailItem As Object

Dim myCreatedEmails() As Object

Sub CreateAndDisplayEmails()

Set objOutlook = CreateObject("Outlook.Application")

Erase myCreatedEmails

For i = 1 To 3

Set objMailItem = objOutlook.CreateItem(0)

With objMailItem
.To = "Tester" & i
.Subject = "Tester" & i
End With

ReDim myCreatedEmails(k)
'##### falls down on next line #####
myCreatedEmails(k) = objMailItem
k = k + 1

Next i

For k = 1 To UBound(myCreatedEmails)
myCreatedEmails(k).Display
Next k

End Sub


....I've marked where it already falls down and I assume even if this
line is fixed then it'll fall down later on on the line
"myCreatedEmails(k).Display"

The above must be pretty bl##dy close!...can anyone help?

Help appreciated
Jason





Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Always display items AND Include hidden items in totals Ted M H Excel Discussion (Misc queries) 0 November 6th 07 09:47 PM
xla creating new menu items dspencer[_2_] Excel Programming 2 January 30th 06 03:01 PM
Dynamic PageField Items DynamiteSkippy Excel Programming 3 November 11th 05 03:50 PM
Pivot Table calculated items with dynamic file source [email protected] Excel Programming 0 June 9th 05 05:54 PM
Dynamic Selection of Pivottable Drop Down Items Pat[_8_] Excel Programming 0 November 7th 03 10:01 PM


All times are GMT +1. The time now is 07:49 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"