View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Jim Thomlinson Jim Thomlinson is offline
external usenet poster
 
Posts: 5,939
Default Is VBA Class have Constructor like java ?

In VBA there is an event generated when a class is initialized. This would be
similar to the constructor in Java. In the Class just above the code window
There is a drop down with (General) in it. Switch that to Class. It will by
default drop in a sub for the initialize event. other events are listed to
the right of that drop down...

One thing to not is that your declaration has the work new in it. That is a
very inefficient way of instantiating objects. check out this link (about 2/3
the way down)...

http://www.cpearson.com/excel/variables.htm


--
HTH...

Jim Thomlinson


"moonhk" wrote:

Is VBA Class have Constructor like java ? I want New a clsStatement run
Init_ME() or other Init_xxx ? Is it possible ?


Option Explicit

Public StatementContent As New clsStatement

Sub Start()
With StatementContent
StatementContent.FileNameSource = "g:\ass\ass_statement.txt"
.Read_file
End With

End Sub


'~~ Class Modules

Option Explicit
Public FileNameSource As String ' Statement file name
Const TEMPLATE_DIRECTORY = "g:\ass\"
Const TEMPLATE_NAME = "eStatement_template.xls"
Const STATEMENT_DIR = "g:\ass\Statements"
Private Debtor_FN As String
Private Details(1024) As String
Private Debtor_nbr_pos As Long
Private Debtor_mail_pos As Long

Public Sub Init_ME()

End Sub

Private Sub Init_pos()
Debtor_nbr_pos = 0
Debtor_mail_pos = 0
Debtor_FN = ""
End Sub


Public Sub Build_file()
Dim cnt As Long
Dim Act As Long
Dim loSheet As Variant

'MsgBox VBA.Str(Debtor_nbr_pos)

'~~ Open template file
Workbooks.Open FileName:=TEMPLATE_DIRECTORY + "\" + TEMPLATE_NAME
Workbooks(TEMPLATE_NAME).Activate
Application.Workbooks(TEMPLATE_NAME).SaveAs STATEMENT_DIR + "\" +
Debtor_FN

' Workbooks("new.xls").Activate
Set loSheet = Application.Workbooks(Debtor_FN).Sheets("Statement ")
Act = 6
For cnt = Debtor_nbr_pos To Debtor_mail_pos
loSheet.Cells(Act, 1).Value = Details(cnt)
Act = Act + 1
Next
Application.Workbooks(Debtor_FN).Save
Application.Workbooks(Debtor_FN).Close
Call Init_pos
End Sub

Public Sub Read_file()
Dim fileNumber As Long
Dim cnt As Long
Dim inputVal As String

Call Init_pos

fileNumber = FreeFile ' get unused file number
Application.StatusBar = "Reading ...." & FileNameSource


Open FileNameSource For Input As #fileNumber
Do While Not EOF(fileNumber)
Line Input #fileNumber, inputVal
cnt = cnt + 1
Details(cnt) = inputVal
If VBA.Left(inputVal, 10) = "Debtor nr:" Then
' MsgBox inputVal + " " + VBA.Str(cnt)
Debtor_nbr_pos = cnt
Debtor_FN = VBA.Trim(VBA.Mid(inputVal, 11,
VBA.Len(VBA.Trim(inputVal)) - 10) + ".xls")
'MsgBox Debtor_FN
End If
If VBA.Left(inputVal, 8) = "E-mail :" Then
'MsgBox inputVal + " " + VBA.Str(cnt)
Debtor_mail_pos = cnt
End If
If Debtor_mail_pos < 0 Then
Call Build_file
End If
Loop
Close #fileNumber
MsgBox cnt

End Sub