Nested Tables Question

  • Thread starter SunshineStateBroker via OfficeKB.com
  • Start date
S

SunshineStateBroker via OfficeKB.com

Good morning,
I seem to be running in to an issue that i can't figure out.
The point of the code is to generate a table with a number of formfields
based on the user's input. The issue i seem to be having and cannot figure
out is there are two fields being created one inside the other and throwing
the whole form off kilter.

----

Sub Mortgages()
Dim mNum As Long
'Declare a range object
Dim pRng As Word.Range
Dim bProtected As Boolean
Dim mCount As Long
Dim k As Long
Dim e As Long
Dim objDocm As Document
Dim objTablem As Table
Dim x As Long
Dim f As Long
Dim v As Long
Dim w As Long
Dim g As Long
Dim h As Long

'Unprotect the file
If ActiveDocument.ProtectionType <> wdNoProtection Then
bProtected = True
ActiveDocument.Unprotect Password:=""
End If
mNum = ActiveDocument.FormFields("Mortgages").Result
Selection.GoTo What:=wdGoToBookmark, Name:="bMortgages"
'Define the range object. This is where the cursor is at (start point)
Set pRng = Selection.Range

' sets q to number of rows needed
x = (mNum * 13)

' Decides if any formatting needs to be donw
If mNum = 1 Then
'Select the first of the fields in the range
ActiveDocument.FormFields("MortText1").Select

Else

ActiveDocument.Paragraphs.Add
Selection.Tables.Add Selection.Range, x, 2

Set objDocm = ActiveDocument
Set objTablem = objDocm.Tables.Add(Range:=Selection.Range, NumRows:=x,
NumColumns:=2)
objTablem.Columns(1).Width = InchesToPoints(1.7)
objTablem.Columns(2).Width = InchesToPoints(4.5)


' resizes rows
For f = 1 To x
With Selection
objTablem.Rows(f).Height = InchesToPoints(0.2)
End With
Next f

'adds formfields to column 2
For v = 1 To x

With Selection

If v = (v * 13) Then

Selection.Tables.Item(1).Cell(v, 2).Select
'Selection.FormFields.Add Selection.Range,
wdFieldFormTextInput
ActiveDocument.FormFields.Item(1).Name = ("fldName")
ActiveDocument.FormFields.Item("fldName").Result = ""

Else

Selection.Tables.Item(1).Cell(v, 2).Select
Selection.FormFields.Add Selection.Range,
wdFieldFormTextInput
ActiveDocument.FormFields.Item(1).Name = ("fldName")
ActiveDocument.FormFields.Item("fldName").Result = ""

End If

End With

Next v

'adds text to column 1
For w = 1 To mNum

With Selection

Selection.Tables.Item(1).Cell(((w * 13) - 12), 1).Select
Selection.TypeText "Document Type:"
Selection.Tables.Item(1).Cell(((w * 13) - 11), 1).Select
Selection.TypeText "Mortgagee:"
Selection.Tables.Item(1).Cell(((w * 13) - 10), 1).Select
Selection.TypeText "Mortgagor:"
Selection.Tables.Item(1).Cell(((w * 13) - 9), 1).Select
Selection.TypeText "Amount: $"
Selection.Tables.Item(1).Cell(((w * 13) - 8), 1).Select
Selection.TypeText "Dated:"
Selection.Tables.Item(1).Cell(((w * 13) - 7), 1).Select
Selection.TypeText "Recorded:"
Selection.Tables.Item(1).Cell(((w * 13) - 6), 1).Select
Selection.TypeText "Open Ended:"
Selection.Tables.Item(1).Cell(((w * 13) - 5), 1).Select
Selection.TypeText "Book:"
Selection.Tables.Item(1).Cell(((w * 13) - 4), 1).Select
Selection.TypeText "Page:"
Selection.Tables.Item(1).Cell(((w * 13) - 3), 1).Select
Selection.TypeText "Instrument No:"
Selection.Tables.Item(1).Cell(((w * 13) - 2), 1).Select
Selection.TypeText "Maturity Date:"
Selection.Tables.Item(1).Cell(((w * 13) - 1), 1).Select
Selection.TypeText "Comments:"
Selection.Tables.Item(1).Cell((w * 13), 1).Select
Selection.TypeText " "

End With

Next w

If ActiveDocument.FormFields("Deeds").Result = 1 Then
ActiveDocument.FormFields("Text48").Select
Else
g = ActiveDocument.FormFields("Deeds").Result
h = (g * 10) + 48
ActiveDocument.FormFields("Text" & h).Select
End If

End If

'The selection has moved down the page. Redefine the end of the range object.

pRng.End = Selection.Range.End

'Recreate the bookmark.
ActiveDocument.Bookmarks.Add "bMortgages", pRng

'Count the form fields added to the range
'fCount = oRng.FormFields.Count

'Give each of the added fields a unique bookmark name
'For e = 1 To mCount
' With oRng.FormFields(e)
' .Name = "MortText" & e 'Add a unique bookmark name
' .Enabled = True 'Enable the field for user entry
' .CalculateOnExit = False 'Uncheck the calculate on exit check box
' End With
'Next e

'Reprotect the document.
If bProtected = True Then
ActiveDocument.Protect _
Type:=wdAllowOnlyFormFields, _
NoReset:=True, _
Password:=""
End If



End Sub
 
D

Doug Robbins - Word MVP

You should avoid using the Selection object and use the Range object in its
place.

Something like:

Dim ffrange As Range
Dim ff As FormField
With ActiveDocument
Set ffrange = .Tables.Item(1).Cell(v, 2).Range
ffrange.Collapse wdCollapseStart
Set ff = .FormFields.Add(ffrange, wdFieldFormTextInput)
With ff
.Name = "fldName"
.result = ""
End With
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top