PDA

View Full Version : Solved: Problem naming a formfield within a table



petedw
08-18-2005, 07:05 AM
Hi guys, I have made a small example of a problem that i am having naming a formfield within a table. The code i have works for the 1st formfield it refers to and then when naming the 2nd formfield it decides to name the entire row instead???? :banghead:



Please open the attachment and run the module "NewTable" so you can see the problem that i am having. : pray2:



Thanks again



Pete

TonyJollans
08-18-2005, 08:03 AM
Hi Pete,

Not sure what you mean by naming the row, but your code seems to work alright for me (in Word 2k3).

fumei
08-18-2005, 08:40 AM
One line will fix this. Replace:

ActiveDocument.Tables(iNum).Rows.Last.Select
Selection.Cells(iCnt).Select
with:
ActiveDocument.Tables(iNum).Rows.Last.Select
Selection.Cells(iCnt).Select
Selection.Collapse direction:=wdCollapseStart

You havd selected the CELL, and made the CELL a formfield, and Word thinks that is very very weird. So just collapse the Selection to a point, then the inserted formfield is like any other formfield, and you can name it.

May I add some comments?

1. I do not understand the repeated use of the Selection object here.
ActiveDocument.Tables(iNum).Rows.Last.Select
Selection.Cells(iCnt - 3).Select
Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft

ActiveDocument.Tables(iNum).Rows.Last.Select
Selection.Cells(iCnt - 2).Select
Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft

ActiveDocument.Tables(iNum).Rows.Last.Select
Selection.Cells(iCnt - 1).Select
You are repeatedly selecting the SAME then selecting a cell. Seems to me that this could be do with fewer instructions.

2. Maybe use more With statements? Instaed of:
tbl.Cell(1, 1).Select
Selection.Font.Bold = True
Selection.TypeText Text:="Date"

tbl.Cell(1, 2).Width = 350
tbl.Cell(1, 2).Select
Selection.Font.Bold = True
Selection.TypeText Text:="Details"

tbl.Cell(1, 3).Width = 105
tbl.Cell(1, 3).Select
Selection.Font.Bold = True
Selection.TypeText Text:="Withdrawn" & vbCrLf
Selection.TypeText Text:="(?)"
You could use:
With tbl
With .Cell(1, 1)
.Width = 100
.Range.Font.Bold = True
.Range.Text = "Date"
End With
With .Cell(1, 2)
.Width = 350
.Range.Font.Bold = True
.Range.Text = "Details"
End With
With .Cell(1, 3)
.Width = 105
.Range.Font.Bold = True
.Range.Text = "Withdrawn" & vbCrLf & "(?)"
End With

Also there is no need for two text instructions, one will do, plus it is text on the Range object, not typed text using Selection.

Note that the With statement is not closed, as there are a bunch of them, but I did not want to use them all in this example.

Anyway, just collapse the Selection, and you will be able to name the formfield.

petedw
08-22-2005, 02:16 AM
Thanks again Gerry. :thumb
Greatly appreciated!!

Pete

fumei
08-22-2005, 07:07 AM
You are welcome. :beerchug: