A
Al & Cay Grant
Hi Folks,
I added the following code to my project to calculate a persons age and
automatically update a text box on the user form.
Private Sub TextBox4_Exit(ByVal Cancel As MSForms.ReturnBoolean)
' CALCULATES AGE
If Not IsDate(TextBox4.Value) Then
Cancel = True
Exit Sub
End If
TextBox5.Value = Int(DateDiff("d", TextBox4, Now) / 365.25)
End Sub
Problem now is that I get requested member of collection does not exist in
the
following code at the point I have marked with a "--->>>>"
I think I am getting it because the code is switching to a new document
which
does not contain the requested bookmark.
The code among other things switches between two documents and for each loop
pastes a string into each document, so the code switches between documents.
If anyone knows a easier way to do this lemme know.
Formatting the string for each loop is also a little tricky.
The documents could be saved first if it makes things easier, but it would
be better
if they were then saved into a new directory named after textbox2.
The headers from the table in the data document (offences.doc) looks like
| OFFENCE | ACT | PENALTY | CHARGE TEXT | JURISDICTION |
I am pretty new to all this programming so, please, be gentle!!
-Al
Private Sub UserForm_Initialize()
Dim Sourcedoc As Document, i As Integer, j As Integer, myitem As Range, m As
Long, n As Long
Application.ScreenUpdating = False
Set Sourcedoc = Documents.Open(FileName:="C:\Documents and
Settings\Alistair\Application Data\Microsoft\Templates\Offences.doc")
' GET NUMBER OF OFFENCES = NUMBER OF ROWS - 1
i = Sourcedoc.Tables(1).Rows.Count - 1
' NUMBER OF COLUMNS
j = 3
' SET NUMBER OF COLUMNS IN LISTBOX TO MATCH
' NUMBER OF COLUMNS IN TABLE OF OFFENCES
ListBox1.ColumnCount = j
' DEFINE ARRAY TO BE LOADED WITH OFFENCES DATA
Dim MyArray() As Variant
'LOAD OFFENCE DATA INTO MY ARRAY
ReDim MyArray(i, j)
For n = 0 To j - 1
For m = 0 To i - 1
Set myitem = Sourcedoc.Tables(1).Cell(m + 2, n + 1).Range
myitem.End = myitem.End - 1
MyArray(m, n) = myitem.Text
Next m
Next n
' LOAD DATA INTO LISTBOX1
ListBox1.List() = MyArray
' CLOSE FILE CONTAINING LIST OF OFFENCES
Sourcedoc.Close SaveChanges:=wdDoNotSaveChanges
End Sub
Private Sub CommandButton1_Click()
Dim i As Long, j As Long, CrimeandPunishment As String, SpecimenCharge As
String, Sourcedoc As Document _
, k As Long
' WINDOWS COUNT
Documents.Add Template:="H:\Office\Templates\normal.dot", NewTemplate:=False
k = Windows.Count
Windows(k - 1).Activate
' ITEMS ARE INDEXED FROM ZERO
' LOOPS THROUGH ALL ITEMS
For i = 0 To ListBox1.ListCount - 1
'...AND CHECK WETHER EACH SELECTED
If ListBox1.Selected(i) Then
' IF SELECTED DISPLAY ITEM IN MSGBOX
CrimeandPunishment = ""
SpecimenCharge = ""
For j = 0 To ListBox1.ColumnCount - 2
ListBox1.BoundColumn = j
If j = 1 Then
SpecimenCharge = "JUSTICE CODE=" & CrimeandPunishment
CrimeandPunishment = CrimeandPunishment & vbCr & vbCr
SpecimenCharge = SpecimenCharge & vbCr & "ACT & SECTION=" &
ListBox1.List(i, j)
SpecimenCharge = SpecimenCharge & vbCr & "PENALTY=" &
ListBox1.List(i, j + 1)
End If
CrimeandPunishment = CrimeandPunishment & ListBox1.List(i, j)
Set Sourcedoc = Documents.Open(FileName:="C:\Documents and
Settings\Alistair\Application Data\Microsoft\Templates\Offences.doc")
SpecimenCharge = SpecimenCharge & vbCr & "CHARGE TEXT=" &
Sourcedoc.Tables(1).Cell(i + 2, j + 3).Range
Sourcedoc.Close SaveChanges:=wdDoNotSaveChanges
Next j
CrimeandPunishment = CrimeandPunishment & vbCr & "Penalty:" &
ListBox1.List(i, ListBox1.ColumnCount - 1) & vbCr & vbCr
End If
If ListBox1.Selected(i) Then
Windows(k - 1).Activate
------->>> ActiveDocument.Bookmarks("Offences").Range.InsertBefore
(CrimeandPunishment)
MsgBox (SpecimenCharge)
Windows(k).Activate
ActiveDocument.Content.InsertAfter (SpecimenCharge)
End If
Next i
' APPLY FORMATTING TO CHARGES
Windows(k - 1).Activate
For i = 1 To ActiveDocument.Bookmarks("Offences").Range.Paragraphs.Count
ActiveDocument.Bookmarks("Offences").Range.Paragraphs(i).Range.Font.Bold
= True
ActiveDocument.Bookmarks("Offences").Range.Paragraphs(i).Range.Font.AllCaps
= True
i = i + 4
Next i
UserForm1.Hide
Windows(k - 1).Activate
With ActiveDocument
.Bookmarks("name").Range _
.InsertBefore TextBox1
.Bookmarks("street").Range _
.InsertBefore TextBox2
.Bookmarks("town").Range _
.InsertBefore TextBox3
.Bookmarks("DOB").Range _
.InsertBefore TextBox4
.Bookmarks("age").Range _
.InsertBefore TextBox5
.Bookmarks("occ").Range _
.InsertBefore TextBox6
.Bookmarks("exhibits").Range _
.InsertBefore TextBox9
.Bookmarks("witnesses").Range _
.InsertBefore TextBox10
End With
End Sub
I added the following code to my project to calculate a persons age and
automatically update a text box on the user form.
Private Sub TextBox4_Exit(ByVal Cancel As MSForms.ReturnBoolean)
' CALCULATES AGE
If Not IsDate(TextBox4.Value) Then
Cancel = True
Exit Sub
End If
TextBox5.Value = Int(DateDiff("d", TextBox4, Now) / 365.25)
End Sub
Problem now is that I get requested member of collection does not exist in
the
following code at the point I have marked with a "--->>>>"
I think I am getting it because the code is switching to a new document
which
does not contain the requested bookmark.
The code among other things switches between two documents and for each loop
pastes a string into each document, so the code switches between documents.
If anyone knows a easier way to do this lemme know.
Formatting the string for each loop is also a little tricky.
The documents could be saved first if it makes things easier, but it would
be better
if they were then saved into a new directory named after textbox2.
The headers from the table in the data document (offences.doc) looks like
| OFFENCE | ACT | PENALTY | CHARGE TEXT | JURISDICTION |
I am pretty new to all this programming so, please, be gentle!!
-Al
Private Sub UserForm_Initialize()
Dim Sourcedoc As Document, i As Integer, j As Integer, myitem As Range, m As
Long, n As Long
Application.ScreenUpdating = False
Set Sourcedoc = Documents.Open(FileName:="C:\Documents and
Settings\Alistair\Application Data\Microsoft\Templates\Offences.doc")
' GET NUMBER OF OFFENCES = NUMBER OF ROWS - 1
i = Sourcedoc.Tables(1).Rows.Count - 1
' NUMBER OF COLUMNS
j = 3
' SET NUMBER OF COLUMNS IN LISTBOX TO MATCH
' NUMBER OF COLUMNS IN TABLE OF OFFENCES
ListBox1.ColumnCount = j
' DEFINE ARRAY TO BE LOADED WITH OFFENCES DATA
Dim MyArray() As Variant
'LOAD OFFENCE DATA INTO MY ARRAY
ReDim MyArray(i, j)
For n = 0 To j - 1
For m = 0 To i - 1
Set myitem = Sourcedoc.Tables(1).Cell(m + 2, n + 1).Range
myitem.End = myitem.End - 1
MyArray(m, n) = myitem.Text
Next m
Next n
' LOAD DATA INTO LISTBOX1
ListBox1.List() = MyArray
' CLOSE FILE CONTAINING LIST OF OFFENCES
Sourcedoc.Close SaveChanges:=wdDoNotSaveChanges
End Sub
Private Sub CommandButton1_Click()
Dim i As Long, j As Long, CrimeandPunishment As String, SpecimenCharge As
String, Sourcedoc As Document _
, k As Long
' WINDOWS COUNT
Documents.Add Template:="H:\Office\Templates\normal.dot", NewTemplate:=False
k = Windows.Count
Windows(k - 1).Activate
' ITEMS ARE INDEXED FROM ZERO
' LOOPS THROUGH ALL ITEMS
For i = 0 To ListBox1.ListCount - 1
'...AND CHECK WETHER EACH SELECTED
If ListBox1.Selected(i) Then
' IF SELECTED DISPLAY ITEM IN MSGBOX
CrimeandPunishment = ""
SpecimenCharge = ""
For j = 0 To ListBox1.ColumnCount - 2
ListBox1.BoundColumn = j
If j = 1 Then
SpecimenCharge = "JUSTICE CODE=" & CrimeandPunishment
CrimeandPunishment = CrimeandPunishment & vbCr & vbCr
SpecimenCharge = SpecimenCharge & vbCr & "ACT & SECTION=" &
ListBox1.List(i, j)
SpecimenCharge = SpecimenCharge & vbCr & "PENALTY=" &
ListBox1.List(i, j + 1)
End If
CrimeandPunishment = CrimeandPunishment & ListBox1.List(i, j)
Set Sourcedoc = Documents.Open(FileName:="C:\Documents and
Settings\Alistair\Application Data\Microsoft\Templates\Offences.doc")
SpecimenCharge = SpecimenCharge & vbCr & "CHARGE TEXT=" &
Sourcedoc.Tables(1).Cell(i + 2, j + 3).Range
Sourcedoc.Close SaveChanges:=wdDoNotSaveChanges
Next j
CrimeandPunishment = CrimeandPunishment & vbCr & "Penalty:" &
ListBox1.List(i, ListBox1.ColumnCount - 1) & vbCr & vbCr
End If
If ListBox1.Selected(i) Then
Windows(k - 1).Activate
------->>> ActiveDocument.Bookmarks("Offences").Range.InsertBefore
(CrimeandPunishment)
MsgBox (SpecimenCharge)
Windows(k).Activate
ActiveDocument.Content.InsertAfter (SpecimenCharge)
End If
Next i
' APPLY FORMATTING TO CHARGES
Windows(k - 1).Activate
For i = 1 To ActiveDocument.Bookmarks("Offences").Range.Paragraphs.Count
ActiveDocument.Bookmarks("Offences").Range.Paragraphs(i).Range.Font.Bold
= True
ActiveDocument.Bookmarks("Offences").Range.Paragraphs(i).Range.Font.AllCaps
= True
i = i + 4
Next i
UserForm1.Hide
Windows(k - 1).Activate
With ActiveDocument
.Bookmarks("name").Range _
.InsertBefore TextBox1
.Bookmarks("street").Range _
.InsertBefore TextBox2
.Bookmarks("town").Range _
.InsertBefore TextBox3
.Bookmarks("DOB").Range _
.InsertBefore TextBox4
.Bookmarks("age").Range _
.InsertBefore TextBox5
.Bookmarks("occ").Range _
.InsertBefore TextBox6
.Bookmarks("exhibits").Range _
.InsertBefore TextBox9
.Bookmarks("witnesses").Range _
.InsertBefore TextBox10
End With
End Sub