I have been given some Word templates to populate from Excel.
I can use bookmarks for the simple stuff but in the
templates there are Tables and Charts as pictures inside
Text Boxes I need to fill in and replace.
My problem is two fold.
First how can I reference the cells of the table inside the
text boxes (other than assigjning a bookmark to each and
every cell? One way would be to position at the first
orw/column and "tab" through each cell replacing each
successive cell but I can't find out how to set a range
inside the text box or how to tab through the cells in this
way.
Second if a Text box contains say 6 charts/pictures how I
refer to each one to replace it with another chart/picture?
Thanks in Advance, James
> I have been given some Word templates to populate from Excel.
> I can use bookmarks for the simple stuff but in the
[quoted text clipped - 15 lines]
>
> Thanks in Advance, James
Hi James,
Have a look at these two demo macros. Post back if you still have questions.
Note especially that they use Range objects to get access to cells and
pictures, and they do *not* move the Selection (the cursor). That can be an
important consideration for the speed of the macro when there are a lot of
things to work on.
Sub DemoTableInTextbox()
Dim oTextBox As Shape
Dim oTable As Table
Dim oCell As Cell
Dim oRg As Range
Dim nRow As Long, nCol As Long
' assume there is exactly one textbox
' and that it contains a table --
' in real code you would have to
' check these assumptions!
Set oTextBox = ActiveDocument.Shapes(1)
Set oTable = oTextBox.TextFrame.TextRange.Tables(1)
' method 1:
' iterate through the Cells collection
For Each oCell In oTable.Range.Cells
Set oRg = oCell.Range
' don't include cell marker
oRg.MoveEnd unit:=wdCharacter, Count:=-1
MsgBox "cell (" & oCell.RowIndex & "," & _
oCell.ColumnIndex & ") = " & oRg.Text
Next oCell
' method 2:
' access individual cells
' [Note: this will get a runtime error if there are
' any merged cells in the table!]
For nRow = 1 To oTable.Rows.Count
For nCol = 1 To oTable.Columns.Count
Set oRg = oTable.Cell(nRow, nCol).Range
oRg.MoveEnd unit:=wdCharacter, Count:=-1
MsgBox "Individual cell (" & nRow & "," & _
nCol & ") = " & oRg.Text
Next nCol
Next nRow
Set oRg = Nothing
Set oCell = Nothing
Set oTable = Nothing
Set oTextBox = Nothing
End Sub
Sub DemoPictureInTextbox()
Dim oTextBox As Shape
Dim oInlinePicture As InlineShape
Dim oRg As Range
Dim nPicture
' Note: Word doesn't support floating pictures
' (Shape objects) inside text boxes
Set oTextBox = ActiveDocument.Shapes(2)
Set oRg = oTextBox.TextFrame.TextRange
' method 1:
' iterate through the InlineShapes collection
For Each oInlinePicture In oRg.InlineShapes
MsgBox "Inline: " & vbCr & _
oInlinePicture.Width & " by " & _
oInlinePicture.Height
Next oInlinePicture
' method 2:
' access individual pictures
For nPicture = 1 To oRg.InlineShapes.Count
Set oInlinePicture = oRg.InlineShapes(nPicture)
MsgBox "Inline " & nPicture & ": " & vbCr & _
oInlinePicture.Width & " by " & _
oInlinePicture.Height
Next nPicture
Set oInlinePicture = Nothing
Set oRg = Nothing
Set oTextBox = Nothing
End Sub

Signature
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
anonymous@discussions.microsoft.com - 15 Mar 2005 04:45 GMT
Thanks for your help. I can't seem to find this kind of
help when I look in books or elsewhere online :-)
First I built some code to make sure I was referencing the
right shape, then after I knew I was looking at Shape #3:
Set oTextBox = wd.ActiveDocument.Shapes(3)
Set oTable = oTextBox.TextFrame.TextRange.Tables(1)
MsgBox " Table Rows " & oTable.Rows.Count
MsgBox " Table Cols Count " & oTable.Columns.Count
Set oRg = oTable.Cell(1, 1).Range
MsgBox "Cell Contents = " & oRg.Value
The code above tells me I have 53 Rows and 14 columns.
If I don't define oTxxtBox as an 'Object' I get an error of
Type Mismatch (i.e. if As Shape).
When I get to the Set oRg line also I get a Type Mismatch
error.
I get the same error with:
Set oRg = oTable.Range.Cells(1).Range
And just for fun I tried:
For Each oCell In oTable.Range.Cells
Set oRg = oCell.Range
and received the same error.
(I'm using Office XP)
Any further hints would be appreciated.
Cheers, James
>-----Original Message-----
>> I have been given some Word templates to populate from Excel.
[quoted text clipped - 103 lines]
> Set oTextBox = Nothing
>End Sub