PDA

View Full Version : Assign a field to a table cell VBA



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.

gmaxey
02-10-2016, 11:17 AM
Dim oRng as Range
Set oRng = ??.Cell(?,?).Range
oRng.End =oRng.End - 1
oRng.Fields.Add Range:=oRng, Type:=wdFieldEmpty, Text:= _
"DOCPROPERTY Document_1_form_Description ", PreserveFormatting:=True

osmanelsoz
02-10-2016, 12:03 PM
Dim oRng as Range
Set oRng = ??.Cell(?,?).Range
oRng.End =oRng.End - 1
oRng.Fields.Add Range:=oRng, Type:=wdFieldEmpty, Text:= _
"DOCPROPERTY Document_1_form_Description ", PreserveFormatting:=True

I replaced first set of question marks with table name, is it correct?

It does not work, "Run-time error '424'. Object required".

Thank you for your kind help

gmaxey
02-10-2016, 12:17 PM
This works. Not sure what you mean by table name.


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oRng As Range
Set oRng = ActiveDocument.Tables(1).Cell(1, 1).Range
oRng.End = oRng.End - 1
oRng.Fields.Add Range:=oRng, Type:=wdFieldEmpty, Text:= _
"DOCPROPERTY Document_1_form_Description ", PreserveFormatting:=True
lbl_Exit:
Exit Sub
End Sub

gmaxey
02-10-2016, 01:05 PM
Sub ScratchMacrot()
'A basic Word macro coded by Greg Maxey
Dim oTbl As Table, lngVarRows As Long
Dim lngRow As Long, lngCol
Dim oRng As Word.Range
MsgBox "I will help you, but I am not going to do all of your work for you." & vbCr + vbCr _
& "One thing that I hope you are learning in college is that life is not a free ride."
lngVarRows = 6 'or some variable count
Set oTbl = ActiveDocument.Tables.Add(Selection.Range, lngVarRows, 6)
With oTbl
.Cell(1, 1).Range.Text = "A"
.Cell(1, 2).Range.Text = "B"
.Cell(1, 3).Range.Text = "C"
.Cell(1, 4).Range.Text = "D"
.Cell(1, 5).Range.Text = "E"
.Cell(1, 6).Range.Text = "F"
For lngRow = 2 To lngVarRows
For lngCol = 1 To 6
Set oRng = .Cell(lngRow, lngCol).Range
oRng.End = oRng.End - 1
'ActiveDocument.Fields.Add ...
Next
Next
End With
MsgBox "There, I have helped you. Now it is your turn." & vbCr + vbCr _
& "When you leave your campus or dorm or wherever you are staying tonight, I want you " _
& "to do something king/charitable for someone you see less fortunate than you who could use some help." & vbCr + vbCr _
& "You might not learn this in college, but will in life. What goes around comes around."
lbl_Exit:
Exit Sub
End Sub

osmanelsoz
02-10-2016, 01:26 PM
Thank you very much, sir. I will not forget what you said and your kind help