PDA

View Full Version : Macro to take table data and output outside of table



IanLazer
12-21-2012, 10:17 AM
I have a series of word docs that have tables. I need to take the data in the tables and put it in the document without the tables. I have the following VBA working to build the array of the table entries. I am having trouble with the loop to write the information out. Any help would be appreciated.

Sub ReturnCellContentsToArray()
Dim intCells As Integer
Dim celTable As Cell
Dim strCells() As String
Dim intCount As Integer
Dim rngText As Range
Dim A As String


If ActiveDocument.Tables.Count >= 1 Then
With ActiveDocument.Tables(1).Range
intCells = .Cells.Count
ReDim strCells(intCells)
intCount = 1
For Each celTable In .Cells
Set rngText = celTable.Range
rngText.MoveEnd Unit:=wdCharacter, Count:=-1
strCells(intCount) = rngText
intCount = intCount + 1
Next celTable
End With
End If

fumei
12-21-2012, 02:36 PM
"I am having trouble with the loop to write the information out."

Not sure what you mean by "out". Please describe what you want to happen.

fumei
12-21-2012, 03:10 PM
BTW, here is something that you may be able to use.
Function CellText(r As Range)
CellText = Left(r.Text, Len(r.Text) - 2)
End Function
This is a function to grab ONLY the text from a cell - stripping off the end-of-cell marker.
Sub StringTogetherTableText2()
Dim celTable As Cell

For Each celTable In ActiveDocument.Tables(1).Range.Cells
Selection.TypeText celltext(celTable.Range) & vbCrLf
Next
End SubThis puts the text from each cell from table(1) as a paragraph, at the Selection point.

I guess I am asking why you need the cell contents in an array.

macropod
12-21-2012, 03:44 PM
Ian: So what do you want to achieve that the 'convert to text' option doesn't give you?

fumei
12-21-2012, 05:52 PM
Doh. There is that isn't there.