LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 19
Default Passing an Array of User-Defined Type to an Argument of a Function

Two problems that I note -- others may exist.

Inside GetMailThreads you fill the array arMailContents. But I don't
see any declaration for it.

In testgetmailthreads, you ReDim myarmailcontents with a number
returned by GetMailThreads. But that just creates an empty array
within the procedure. At that point myarmailcontents has nothing to do
with the arMailContents that was actually filled by GetMailThreads.

I'd restructure the code as follows:

Option Explicit
Public Type MailThread
SubjectTitle As String
Subject As String
Body As String
End Type
Sub testGetMailThreads()
'...
Dim MailThreads(0 To 0) As MailThread
'...
getMailThreads "c:...", MailThreads
'...

End Sub
Sub getMailThreads(ByVal aFileName As String, _
ByRef MailThreads() As MailThread)
Dim aMailThread As MailThread
'...
While getNextMessage(aMailThread)
ReDim Preserve MailThreads(0 To UBound(MailThreads) + 1)
Wend

End Sub
Function getNextMessage(ByRef aMailThread As MailThread)
'...
'return true while a real message is available. _
That message is in aMailThread
End Function



--
Regards,

Tushar Mehta, MS MVP -- Excel
www.tushar-mehta.com
Excel, PowerPoint, and VBA add-ins, tutorials
Custom MS Office productivity solutions

In article <y73SnGJZDHA.64@newsgroup, says...
Dear Colleagues;

I'm trying to save and retrieve email templates in a text file. In order to
programmatically manipulate the final email message, I need to be able to
use arrays.

But I don't know how to pass an array of user-defined types as an argument
of a function. I know that for general types such as String, Integer, etc.
can be passed to a function by reference(as opposed to value), because I
tried it before. But I don't know how that will work with user-defined
types.

The following is the code meant to do the job(It took me the whole morning
this morning to code the following. (-.-) (^!^)
Please help!

I could probably declare arMailThread as a Public variable and see if it
works(I believe it will, even though I didn't try it). But I do not want to
do that because the code will look a lot messier.

Thank you all.

Suk Ho

+++++


Public Type MailThread
SubjectTitle As String
Subject As String
Body As String
End Type

Sub testgetmailthreads()

Dim myarmailcontents() As Variant '---- Here I declared a Variant variable
array in order to contain MailThread arrays.

ReDim myarmailcontents(0 To GetMailThreads("testfile.txt",
myarmailcontents)) '--- Here I'm trying to fill the array and get the
number of elements of that array using the GetMailThreads function.

For i = 0 To UBound(myarmailcontents)
Debug.Print myarmailcontents(i).Subject -I get Error here!! And if I set
breakpoint and observe myarmailcontents, it contains nothing.
Debug.Print myarmailcontents(i).Body
Next
End Sub


Function GetMailThreads(TextFileToRead As String, arMailContents As Variant)
As Single

Dim strBlockRead As String
Dim strKey As String
Dim bKeyOnOff As Boolean
Dim strMailThreadKey As String
Dim bMailThreadKeyOnOff As Boolean
'Dim arMailContents() As MailThread ---I commented out this because I
believed that it might not be needed.
Dim nMailThreadCount As Single

strKey = "UpdateProformaStatus"
strMailThreadKey = "Mail Thread"

bKeyOnOff = False




Dim Msg
' If an error occurs, construct an error message
On Error GoTo exitsub: ' Defer error handling.




Open "TESTFILE.txt" For Input As #1 ' Open file for input.

nMailThreadCount = 0 '¸ÞÀÏ °¹¼ö¸¦ ÃʱÈ*ÇÑ´Ù.

While Not EOF(1)

RestartPoint:

Input #1, BlockRead

'Debug.Print BlockRead

If BlockRead = strKey Then
If bKeyOnOff = False Then
bKeyOnOff = True
Else
bKeyOnOff = False
End If

GoTo RestartPoint
End If

'bKeyOnOff°¡ ÄÑÁ®ÀÖÀ¸¸é ¿©±¼* ùßÇÔ.

If bKeyOnOff = True Then

If BlockRead = strMailThreadKey Then
bMailThreadKeyOnOff = True
GoTo RestartPoint
End If

If bMailThreadKeyOnOff = True Then
nMailThreadCount = nMailThreadCount + 1 '°¹¼ö¸¦ 1°³ ¿Ã·ÁÁØ´Ù
bMailThreadKeyOnOff = False
GoTo RestartPoint

End If

End If

Wend

Close #1

'¿©±¼*´Â Çà·ÄÀ» Á¤ÀÇÇÑ ÈÄ¿¡ ½ÇÁ¦ ÀÛ¾÷À» ÇÑ´Ù.

ReDim arMailContents(0 To nMailThreadCount - 1)

Open "TESTFILE.txt" For Input As #1 ' Open file for input.

Dim StepInsideArray As Single
Dim StepMailThreadCount As Single

StepInsideArray = 0
StepMailThreadCount = 0


While Not EOF(1)

RealRestartPoint:

Input #1, BlockRead

'Debug.Print BlockRead

If BlockRead = strKey Then
bKeyOnOff = True
GoTo RealRestartPoint
End If

'bKeyOnOff°¡ ÄÑÁ®ÀÖÀ¸¸é ¿©±¼* ùßÇÔ.

If bKeyOnOff = True Then

If BlockRead = strMailThreadKey Then
bMailThreadKeyOnOff = True
GoTo RealRestartPoint
End If

If bMailThreadKeyOnOff = True Then
Select Case StepInsideArray
Case 1: arMailContents(StepMailThreadCount).SubjectTitle =
BlockRead
Case 2: arMailContents(StepMailThreadCount).Subject = BlockRead
Case 3: arMailContents(StepMailThreadCount).Body = BlockRead
StepInsideArray = 0
StepMailThreadCount = StepMailThreadCount + 1
bMailThreadKeyOnOff = False
End Select
StepInsideArray = StepInsideArray + 1
End If


End If

Wend

Close #1
GetMailThreads = nMailThreadCount
exitsub:

If Err.Number = 53 Then

Msg = "Error # " & Str(Err.Number) & " was generated by " _
& Err.Source & Chr(13) & Err.Description
MsgBox Msg, , "Error", Err.HelpFile, Err.HelpContext
MsgBox "È*ÀÏÀ» »ý¼ºÇÏÁö ¾Ê¾Ò±¸¸¸À¯"
End If
End Function




 
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
Passing a range name as an argument to the Index Function Michael Sharpe Excel Discussion (Misc queries) 3 September 5th 12 01:33 PM
Passing a range to a user defined function Gary Nelson Excel Discussion (Misc queries) 1 July 19th 07 04:22 PM
passing arrays to user defined functions ramki Excel Worksheet Functions 2 February 15th 06 08:34 AM
"User-defined type not defined" message in Excel RW1946 Excel Discussion (Misc queries) 0 August 31st 05 12:14 PM
passing a variable as an argument to a function Drew[_6_] Excel Programming 3 July 25th 03 08:51 PM


All times are GMT +1. The time now is 09:39 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"