S
SunshineStateBroker via OfficeKB.com
Afternoon,
I have been developing a template for my employees to use and have run in to
a wall. I am hoping one of you gurus can help me out.
On this template I have a section for "Mortgages", a macro is set to fire
after the user enters the number of mortgages that should appear on the
report. This macro creates a table with x number of identical fields, each
representing an individual mortgage.
That part works all well and good, I have run in to an issue however in
trying to nest a macro within the created table. Each Mortgage field has a
separate area for "Assignments". I am trying to build a macro that allows the
user to enter a number in a form field in each individual mortgage section
that will create x number of assignment fields as necessary.
My questions are as follows:
1) How do I insert a table within a table? Or if this is not possible, insert
rows to the existing table?
2) For the form field that is created in each mortgage section to prompt the
user for the number of assignments that should appear, how do I not only
assign that form field to fire off the macro that builds the assignment table,
but set a bookmark in that section as well? (the assignments need to appear
below the parent mortgage)
Thank you in advance.
------------Here is the code for the mortgage section:----------
Sub Mtg()
Dim sNum9 As Long
Dim oRng9 As Word.Range
Dim bProtected9 As Boolean
Dim objDoc9 As Document
Dim objTable9 As Table
Dim q9 As Long
Dim o9 As Long
'Unprotect the file
If ActiveDocument.ProtectionType <> wdNoProtection Then
bProtected9 = True
ActiveDocument.Unprotect Password:=""
End If
sNum9 = Val(ActiveDocument.FormFields("Mtg").Result)
' checks to see if any changes are needed
If sNum9 = 1 Then
If bProtected9 = True Then
ActiveDocument.Protect _
Type:=wdAllowOnlyFormFields, _
NoReset:=True, _
Password:=""
End If
End
Else
Selection.GoTo What:=wdGoToBookmark, Name:="bMtg"
'Define the range object. This is where the cursor is at (start point)
Set oRng9 = Selection.Range
' sets q to number of rows needed
q9 = (sNum9 * 7)
ActiveDocument.Paragraphs.Add
'makes the table
Set objDoc9 = ActiveDocument
Set objTable9 = objDoc9.Tables.Add(Range:=Selection.Range, NumRows:=q9,
NumColumns:=1)
objTable0.Columns(1).Width = InchesToPoints(7)
For o9 = 1 To sNum9
With Selection
' each block first selects the cell, sets the alignment for that cell, enters
the name, selects the next cell in that row
'continues to end of row, merges cells, re-aligns it, and adds a form field
Selection.Tables.Item(1).Cell(((o9 * 7) - 6), 1).Select
Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
Selection.TypeText o9
Selection.TypeText ". Foreclosure Mortgage"
Selection.Tables.Item(1).Cell(((o9 * 7) - 5), 1).Select
Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
Selection.TypeText "From: "
Selection.FormFields.Add Selection.Range,
wdFieldFormTextInput
ActiveDocument.FormFields.Item(1).Name = "fldName"
ActiveDocument.FormFields.Item("fldName").Result = ""
Selection.Tables.Item(1).Cell(((o9 * 7) - 4), 1).Select
Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
Selection.TypeText "To: "
Selection.FormFields.Add Selection.Range,
wdFieldFormTextInput
ActiveDocument.FormFields.Item(1).Name = "fldName"
ActiveDocument.FormFields.Item("fldName").Result = ""
Selection.Tables.Item(1).Cell(((o9 * 7) - 3), 1).Select
Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
Selection.TypeText "Dated: "
Selection.FormFields.Add Selection.Range,
wdFieldFormTextInput
ActiveDocument.FormFields.Item(1).Name = "fldName"
ActiveDocument.FormFields.Item("fldName").Result = ""
Selection.TypeText " Recorded: "
Selection.FormFields.Add Selection.Range,
wdFieldFormTextInput
ActiveDocument.FormFields.Item(1).Name = "fldName"
ActiveDocument.FormFields.Item("fldName").Result = ""
Selection.TypeText " "
Selection.FormFields.Add Selection.Range,
wdFieldFormTextInput
ActiveDocument.FormFields.Item(1).Name = "fldName"
ActiveDocument.FormFields.Item("fldName").Result = ""
Selection.TypeText " "
Selection.FormFields.Add Selection.Range,
wdFieldFormTextInput
ActiveDocument.FormFields.Item(1).Name = "fldName"
ActiveDocument.FormFields.Item("fldName").Result = ""
Selection.Tables.Item(1).Cell(((o9 * 7) - 2), 1).Select
Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
Selection.TypeText "Amount: $"
Selection.FormFields.Add Selection.Range,
wdFieldFormTextInput
ActiveDocument.FormFields.Item(1).Name = "fldName"
ActiveDocument.FormFields.Item("fldName").Result = ""
Selection.Tables.Item(1).Cell(((o9 * 7) - 1), 1).Select
Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
Selection.TypeText "Assignments: "
Selection.FormFields.Add Selection.Range,
wdFieldFormTextInput
ActiveDocument.FormFields.Item(1).Name = "fldName"
ActiveDocument.FormFields.Item("fldName").Result = ""
Selection.Tables.Item(1).Cell((o9 * 7), 1).Select
Selection.TypeText " "
End With
Next o9
' Returns curser to first block in the new table
ActiveDocument.FormFields("Temp9").Select
'The selection has moved down the page. Redefine the end of the range object.
oRng9.End = Selection.Range.End
'Recreate the bookmark.
ActiveDocument.Bookmarks.Add "bMtg", oRng9
End If
'Reprotect the document.
If bProtected9 = True Then
ActiveDocument.Protect _
Type:=wdAllowOnlyFormFields, _
NoReset:=True, _
Password:=""
End If
End Sub
------------Here is the code for the assignment section.----------
Sub Docs()
Dim sNum As Long
Dim oRng As Word.Range
Dim bProtected As Boolean
Dim objDoc As Document
Dim objTable As Table
Dim q As Long
Dim o As Long
'Unprotect the file
If ActiveDocument.ProtectionType <> wdNoProtection Then
bProtected = True
ActiveDocument.Unprotect Password:=""
End If
sNum = Val(ActiveDocument.FormFields("Documents").Result)
' checks to see if any changes are needed
If sNum = None Then
If bProtected = True Then
ActiveDocument.Protect _
Type:=wdAllowOnlyFormFields, _
NoReset:=True, _
Password:=""
End If
End
Else
Selection.GoTo What:=wdGoToBookmark, Name:="bDocuments"
'Define the range object. This is where the cursor is at (start point)
Set oRng = Selection.Range
' sets q to number of rows needed
q = (sNum * 6)
ActiveDocument.Paragraphs.Add
'makes the table
Set objDoc = ActiveDocument
Set objTable = objDoc.Tables.Add(Range:=Selection.Range, NumRows:=q,
NumColumns:=5)
objTable.Columns(1).Width = InchesToPoints(0.85)
objTable.Columns(2).Width = InchesToPoints(1)
objTable.Columns(3).Width = InchesToPoints(0.75)
objTable.Columns(4).Width = InchesToPoints(1)
objTable.Columns(5).Width = InchesToPoints(2.65)
For o = 1 To sNum
With Selection
' each block first selects the cell, sets the alignment for that cell, enters
the name, selects the next cell in that row
'continues to end of row, merges cells, re-aligns it, and adds a form field
Selection.Tables.Item(1).Cell(((o * 6) - 5), 1).Select
Selection.SelectCell
Selection.MoveRight Unit:=wdCharacter, Count:=4, Extend:
=wdExtend
Selection.Cells.Merge
Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
Selection.TypeText o
Selection.TypeText ". "
Selection.Font.Underline = wdUnderlineSingle
Selection.TypeText " Assignment"
Selection.Tables.Item(1).Cell(((o * 6) - 4), 1).Select
Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
Selection.TypeText "From:"
Selection.Tables.Item(1).Cell(((o * 6) - 4), 2).Select
Selection.SelectCell
Selection.MoveRight Unit:=wdCharacter, Count:=3, Extend:
=wdExtend
Selection.Cells.Merge
Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
Selection.FormFields.Add Selection.Range,
wdFieldFormTextInput
ActiveDocument.FormFields.Item(1).Name = "fldName"
ActiveDocument.FormFields.Item("fldName").Result = ""
Selection.Tables.Item(1).Cell(((o * 6) - 3), 1).Select
Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
Selection.TypeText "To:"
Selection.Tables.Item(1).Cell(((o * 6) - 3), 2).Select
Selection.SelectCell
Selection.MoveRight Unit:=wdCharacter, Count:=3, Extend:
=wdExtend
Selection.Cells.Merge
Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
Selection.FormFields.Add Selection.Range,
wdFieldFormTextInput
ActiveDocument.FormFields.Item(1).Name = "fldName"
ActiveDocument.FormFields.Item("fldName").Result = ""
Selection.Tables.Item(1).Cell(((o * 6) - 2), 1).Select
Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
Selection.TypeText "Dated:"
Selection.Tables.Item(1).Cell(((o * 6) - 2), 2).Select
Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
Selection.FormFields.Add Selection.Range,
wdFieldFormTextInput
ActiveDocument.FormFields.Item(1).Name = "fldName"
ActiveDocument.FormFields.Item("fldName").Result = ""
Selection.Tables.Item(1).Cell(((o * 6) - 2), 3).Select
Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
Selection.TypeText "Recorded:"
Selection.Tables.Item(1).Cell(((o * 6) - 2), 4).Select
Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
Selection.FormFields.Add Selection.Range,
wdFieldFormTextInput
ActiveDocument.FormFields.Item(1).Name = "fldName"
ActiveDocument.FormFields.Item("fldName").Result = ""
Selection.Tables.Item(1).Cell(((o * 6) - 2), 5).Select
Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
Selection.FormFields.Add Selection.Range,
wdFieldFormTextInput
ActiveDocument.FormFields.Item(1).Name = "fldName"
ActiveDocument.FormFields.Item("fldName").Result = ""
Selection.TypeText " "
Selection.FormFields.Add Selection.Range,
wdFieldFormTextInput
ActiveDocument.FormFields.Item(1).Name = "fldName"
ActiveDocument.FormFields.Item("fldName").Result = ""
Selection.Tables.Item(1).Cell(((o * 6) - 1), 1).Select
Selection.SelectCell
Selection.MoveRight Unit:=wdCharacter, Count:=4, Extend:
=wdExtend
Selection.Cells.Merge
Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
Selection.FormFields.Add Selection.Range,
wdFieldFormTextInput
ActiveDocument.FormFields.Item(1).Name = "fldName"
ActiveDocument.FormFields.Item("fldName").Result = ""
Selection.Tables.Item(1).Cell((o * 6), 1).Select
Selection.TypeText " "
End With
Next o
' Returns curser to first block in the new table
ActiveDocument.FormFields("Temp2").Select
'The selection has moved down the page. Redefine the end of the range object.
oRng.End = Selection.Range.End
'Recreate the bookmark.
ActiveDocument.Bookmarks.Add "bDocuments", oRng
End If
'Reprotect the document.
If bProtected = True Then
ActiveDocument.Protect _
Type:=wdAllowOnlyFormFields, _
NoReset:=True, _
Password:=""
End If
End Sub
I have been developing a template for my employees to use and have run in to
a wall. I am hoping one of you gurus can help me out.
On this template I have a section for "Mortgages", a macro is set to fire
after the user enters the number of mortgages that should appear on the
report. This macro creates a table with x number of identical fields, each
representing an individual mortgage.
That part works all well and good, I have run in to an issue however in
trying to nest a macro within the created table. Each Mortgage field has a
separate area for "Assignments". I am trying to build a macro that allows the
user to enter a number in a form field in each individual mortgage section
that will create x number of assignment fields as necessary.
My questions are as follows:
1) How do I insert a table within a table? Or if this is not possible, insert
rows to the existing table?
2) For the form field that is created in each mortgage section to prompt the
user for the number of assignments that should appear, how do I not only
assign that form field to fire off the macro that builds the assignment table,
but set a bookmark in that section as well? (the assignments need to appear
below the parent mortgage)
Thank you in advance.
------------Here is the code for the mortgage section:----------
Sub Mtg()
Dim sNum9 As Long
Dim oRng9 As Word.Range
Dim bProtected9 As Boolean
Dim objDoc9 As Document
Dim objTable9 As Table
Dim q9 As Long
Dim o9 As Long
'Unprotect the file
If ActiveDocument.ProtectionType <> wdNoProtection Then
bProtected9 = True
ActiveDocument.Unprotect Password:=""
End If
sNum9 = Val(ActiveDocument.FormFields("Mtg").Result)
' checks to see if any changes are needed
If sNum9 = 1 Then
If bProtected9 = True Then
ActiveDocument.Protect _
Type:=wdAllowOnlyFormFields, _
NoReset:=True, _
Password:=""
End If
End
Else
Selection.GoTo What:=wdGoToBookmark, Name:="bMtg"
'Define the range object. This is where the cursor is at (start point)
Set oRng9 = Selection.Range
' sets q to number of rows needed
q9 = (sNum9 * 7)
ActiveDocument.Paragraphs.Add
'makes the table
Set objDoc9 = ActiveDocument
Set objTable9 = objDoc9.Tables.Add(Range:=Selection.Range, NumRows:=q9,
NumColumns:=1)
objTable0.Columns(1).Width = InchesToPoints(7)
For o9 = 1 To sNum9
With Selection
' each block first selects the cell, sets the alignment for that cell, enters
the name, selects the next cell in that row
'continues to end of row, merges cells, re-aligns it, and adds a form field
Selection.Tables.Item(1).Cell(((o9 * 7) - 6), 1).Select
Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
Selection.TypeText o9
Selection.TypeText ". Foreclosure Mortgage"
Selection.Tables.Item(1).Cell(((o9 * 7) - 5), 1).Select
Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
Selection.TypeText "From: "
Selection.FormFields.Add Selection.Range,
wdFieldFormTextInput
ActiveDocument.FormFields.Item(1).Name = "fldName"
ActiveDocument.FormFields.Item("fldName").Result = ""
Selection.Tables.Item(1).Cell(((o9 * 7) - 4), 1).Select
Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
Selection.TypeText "To: "
Selection.FormFields.Add Selection.Range,
wdFieldFormTextInput
ActiveDocument.FormFields.Item(1).Name = "fldName"
ActiveDocument.FormFields.Item("fldName").Result = ""
Selection.Tables.Item(1).Cell(((o9 * 7) - 3), 1).Select
Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
Selection.TypeText "Dated: "
Selection.FormFields.Add Selection.Range,
wdFieldFormTextInput
ActiveDocument.FormFields.Item(1).Name = "fldName"
ActiveDocument.FormFields.Item("fldName").Result = ""
Selection.TypeText " Recorded: "
Selection.FormFields.Add Selection.Range,
wdFieldFormTextInput
ActiveDocument.FormFields.Item(1).Name = "fldName"
ActiveDocument.FormFields.Item("fldName").Result = ""
Selection.TypeText " "
Selection.FormFields.Add Selection.Range,
wdFieldFormTextInput
ActiveDocument.FormFields.Item(1).Name = "fldName"
ActiveDocument.FormFields.Item("fldName").Result = ""
Selection.TypeText " "
Selection.FormFields.Add Selection.Range,
wdFieldFormTextInput
ActiveDocument.FormFields.Item(1).Name = "fldName"
ActiveDocument.FormFields.Item("fldName").Result = ""
Selection.Tables.Item(1).Cell(((o9 * 7) - 2), 1).Select
Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
Selection.TypeText "Amount: $"
Selection.FormFields.Add Selection.Range,
wdFieldFormTextInput
ActiveDocument.FormFields.Item(1).Name = "fldName"
ActiveDocument.FormFields.Item("fldName").Result = ""
Selection.Tables.Item(1).Cell(((o9 * 7) - 1), 1).Select
Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
Selection.TypeText "Assignments: "
Selection.FormFields.Add Selection.Range,
wdFieldFormTextInput
ActiveDocument.FormFields.Item(1).Name = "fldName"
ActiveDocument.FormFields.Item("fldName").Result = ""
Selection.Tables.Item(1).Cell((o9 * 7), 1).Select
Selection.TypeText " "
End With
Next o9
' Returns curser to first block in the new table
ActiveDocument.FormFields("Temp9").Select
'The selection has moved down the page. Redefine the end of the range object.
oRng9.End = Selection.Range.End
'Recreate the bookmark.
ActiveDocument.Bookmarks.Add "bMtg", oRng9
End If
'Reprotect the document.
If bProtected9 = True Then
ActiveDocument.Protect _
Type:=wdAllowOnlyFormFields, _
NoReset:=True, _
Password:=""
End If
End Sub
------------Here is the code for the assignment section.----------
Sub Docs()
Dim sNum As Long
Dim oRng As Word.Range
Dim bProtected As Boolean
Dim objDoc As Document
Dim objTable As Table
Dim q As Long
Dim o As Long
'Unprotect the file
If ActiveDocument.ProtectionType <> wdNoProtection Then
bProtected = True
ActiveDocument.Unprotect Password:=""
End If
sNum = Val(ActiveDocument.FormFields("Documents").Result)
' checks to see if any changes are needed
If sNum = None Then
If bProtected = True Then
ActiveDocument.Protect _
Type:=wdAllowOnlyFormFields, _
NoReset:=True, _
Password:=""
End If
End
Else
Selection.GoTo What:=wdGoToBookmark, Name:="bDocuments"
'Define the range object. This is where the cursor is at (start point)
Set oRng = Selection.Range
' sets q to number of rows needed
q = (sNum * 6)
ActiveDocument.Paragraphs.Add
'makes the table
Set objDoc = ActiveDocument
Set objTable = objDoc.Tables.Add(Range:=Selection.Range, NumRows:=q,
NumColumns:=5)
objTable.Columns(1).Width = InchesToPoints(0.85)
objTable.Columns(2).Width = InchesToPoints(1)
objTable.Columns(3).Width = InchesToPoints(0.75)
objTable.Columns(4).Width = InchesToPoints(1)
objTable.Columns(5).Width = InchesToPoints(2.65)
For o = 1 To sNum
With Selection
' each block first selects the cell, sets the alignment for that cell, enters
the name, selects the next cell in that row
'continues to end of row, merges cells, re-aligns it, and adds a form field
Selection.Tables.Item(1).Cell(((o * 6) - 5), 1).Select
Selection.SelectCell
Selection.MoveRight Unit:=wdCharacter, Count:=4, Extend:
=wdExtend
Selection.Cells.Merge
Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
Selection.TypeText o
Selection.TypeText ". "
Selection.Font.Underline = wdUnderlineSingle
Selection.TypeText " Assignment"
Selection.Tables.Item(1).Cell(((o * 6) - 4), 1).Select
Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
Selection.TypeText "From:"
Selection.Tables.Item(1).Cell(((o * 6) - 4), 2).Select
Selection.SelectCell
Selection.MoveRight Unit:=wdCharacter, Count:=3, Extend:
=wdExtend
Selection.Cells.Merge
Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
Selection.FormFields.Add Selection.Range,
wdFieldFormTextInput
ActiveDocument.FormFields.Item(1).Name = "fldName"
ActiveDocument.FormFields.Item("fldName").Result = ""
Selection.Tables.Item(1).Cell(((o * 6) - 3), 1).Select
Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
Selection.TypeText "To:"
Selection.Tables.Item(1).Cell(((o * 6) - 3), 2).Select
Selection.SelectCell
Selection.MoveRight Unit:=wdCharacter, Count:=3, Extend:
=wdExtend
Selection.Cells.Merge
Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
Selection.FormFields.Add Selection.Range,
wdFieldFormTextInput
ActiveDocument.FormFields.Item(1).Name = "fldName"
ActiveDocument.FormFields.Item("fldName").Result = ""
Selection.Tables.Item(1).Cell(((o * 6) - 2), 1).Select
Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
Selection.TypeText "Dated:"
Selection.Tables.Item(1).Cell(((o * 6) - 2), 2).Select
Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
Selection.FormFields.Add Selection.Range,
wdFieldFormTextInput
ActiveDocument.FormFields.Item(1).Name = "fldName"
ActiveDocument.FormFields.Item("fldName").Result = ""
Selection.Tables.Item(1).Cell(((o * 6) - 2), 3).Select
Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
Selection.TypeText "Recorded:"
Selection.Tables.Item(1).Cell(((o * 6) - 2), 4).Select
Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
Selection.FormFields.Add Selection.Range,
wdFieldFormTextInput
ActiveDocument.FormFields.Item(1).Name = "fldName"
ActiveDocument.FormFields.Item("fldName").Result = ""
Selection.Tables.Item(1).Cell(((o * 6) - 2), 5).Select
Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
Selection.FormFields.Add Selection.Range,
wdFieldFormTextInput
ActiveDocument.FormFields.Item(1).Name = "fldName"
ActiveDocument.FormFields.Item("fldName").Result = ""
Selection.TypeText " "
Selection.FormFields.Add Selection.Range,
wdFieldFormTextInput
ActiveDocument.FormFields.Item(1).Name = "fldName"
ActiveDocument.FormFields.Item("fldName").Result = ""
Selection.Tables.Item(1).Cell(((o * 6) - 1), 1).Select
Selection.SelectCell
Selection.MoveRight Unit:=wdCharacter, Count:=4, Extend:
=wdExtend
Selection.Cells.Merge
Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
Selection.FormFields.Add Selection.Range,
wdFieldFormTextInput
ActiveDocument.FormFields.Item(1).Name = "fldName"
ActiveDocument.FormFields.Item("fldName").Result = ""
Selection.Tables.Item(1).Cell((o * 6), 1).Select
Selection.TypeText " "
End With
Next o
' Returns curser to first block in the new table
ActiveDocument.FormFields("Temp2").Select
'The selection has moved down the page. Redefine the end of the range object.
oRng.End = Selection.Range.End
'Recreate the bookmark.
ActiveDocument.Bookmarks.Add "bDocuments", oRng
End If
'Reprotect the document.
If bProtected = True Then
ActiveDocument.Protect _
Type:=wdAllowOnlyFormFields, _
NoReset:=True, _
Password:=""
End If
End Sub