osmanelsoz
02-10-2016, 07:24 AM
First of all, I have word document which contains number of document properties. I have to populate a table which has fields. Now I've got this VBA code;
Sub NewTable()
Dim tblNew As Table
Dim intX As Integer
Dim intY As Integer
Dim sValue As Integer
Set tblNew = ActiveDocument.Tables.Add(Selection.Range, sValue + 1, 5)
With ActiveDocument.Tables.Item(1)
.Cell(1, 1).Range.Text = "Header1"
.Cell(1, 2).Range.Text = "Header2"
.Cell(1, 3).Range.Text = "Header3"
.Cell(1, 4).Range.Text = "Header4"
.Cell(1, 5).Range.Text = "Header5"
End With
With tblNew
For intX = 2 To 3
.Cell(intX, 1).Range.InsertAfter intX - 1
Next intX
End With
With tblNew
For intX = 2 To sValue
For intY = 2 To 6
.Cell(intX, intY).Range.InsertAfter "Cell: R" & intX & ", C" & intY
Next intY
Next intX
.Columns.AutoFit
End With
End Sub
Sub NewTable()
Dim tblNew As Table
Dim intX As Integer
Dim intY As Integer
Set tblNew = ActiveDocument.Tables.Add(Selection.Range, 3, 5)
With ActiveDocument.Tables.Item(1)
.Cell(1, 1).Range.Text = "Header1"
.Cell(1, 2).Range.Text = "Header2"
.Cell(1, 3).Range.Text = "Header3"
.Cell(1, 4).Range.Text = "Header4"
.Cell(1, 5).Range.Text = "Header5"
'Below generates the field but I have to assign it to a cell.
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"DOCPROPERTY Document_1_form_Description ", PreserveFormatting:=True
End With
With tblNew
For intX = 2 To 3
.Cell(intX, 1).Range.InsertAfter intX - 1
Next intX
End With
With tblNew
For intX = 2 To 3
For intY = 2 To 5
.Cell(intX, intY).Range.InsertAfter "Cell: R" & intX & ", C" & intY
Next intY
Next intX
.Columns.AutoFit
End With
End Sub
I can generate the field but it is not in the cell I want it to be. Let's say I want to assign it to Cell(2,2)
Please help me.
Sub NewTable()
Dim tblNew As Table
Dim intX As Integer
Dim intY As Integer
Dim sValue As Integer
Set tblNew = ActiveDocument.Tables.Add(Selection.Range, sValue + 1, 5)
With ActiveDocument.Tables.Item(1)
.Cell(1, 1).Range.Text = "Header1"
.Cell(1, 2).Range.Text = "Header2"
.Cell(1, 3).Range.Text = "Header3"
.Cell(1, 4).Range.Text = "Header4"
.Cell(1, 5).Range.Text = "Header5"
End With
With tblNew
For intX = 2 To 3
.Cell(intX, 1).Range.InsertAfter intX - 1
Next intX
End With
With tblNew
For intX = 2 To sValue
For intY = 2 To 6
.Cell(intX, intY).Range.InsertAfter "Cell: R" & intX & ", C" & intY
Next intY
Next intX
.Columns.AutoFit
End With
End Sub
Sub NewTable()
Dim tblNew As Table
Dim intX As Integer
Dim intY As Integer
Set tblNew = ActiveDocument.Tables.Add(Selection.Range, 3, 5)
With ActiveDocument.Tables.Item(1)
.Cell(1, 1).Range.Text = "Header1"
.Cell(1, 2).Range.Text = "Header2"
.Cell(1, 3).Range.Text = "Header3"
.Cell(1, 4).Range.Text = "Header4"
.Cell(1, 5).Range.Text = "Header5"
'Below generates the field but I have to assign it to a cell.
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"DOCPROPERTY Document_1_form_Description ", PreserveFormatting:=True
End With
With tblNew
For intX = 2 To 3
.Cell(intX, 1).Range.InsertAfter intX - 1
Next intX
End With
With tblNew
For intX = 2 To 3
For intY = 2 To 5
.Cell(intX, intY).Range.InsertAfter "Cell: R" & intX & ", C" & intY
Next intY
Next intX
.Columns.AutoFit
End With
End Sub
I can generate the field but it is not in the cell I want it to be. Let's say I want to assign it to Cell(2,2)
Please help me.