PDA

View Full Version : Solved: create table with 2 columns tabbed cont in 2nd col?



samohtwerdna
10-06-2009, 05:32 PM
OH man, I have some questions tonight.

Anyway, I want to create a table of an entire document where each paragraph is a cell. I want the table to have 2 columns. All paragraphs that start without a Tab I want to go into the first column and all paragraphs that start with a tab I want to go into the 2nd column.

I have this for a start:

Selection.WholeStory
Selection.ConvertToTable Separator:=wdSeparateByParagraphs, NumColumns:=2, _
NumRows:=myNumRows, AutoFitBehavior:=wdAutoFitFixed
With Selection.Tables(1)
If .Style <> "Table Grid" Then
.Style = "Table Grid"
End If
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = True
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = True
End With



Thanks for any help,

macropod
10-07-2009, 06:39 PM
Hi samohtwerdna,

You don't need a macro for this! All you need to do is to select the range concerned, then use Table|Convert|Text to Table > Tabs > OK.

You'll lose the tabs in the process, but that seems a small price to pay - and could could restore them afterwards.

The only catch to this is that any other tabs in the range will cause cell breaks too, so you might want to temporarily replace those tabs with a special string, which you could convert back to tabs later.

If you really need it, a recorded macro could be used to automate the process.

samohtwerdna
10-07-2009, 07:35 PM
hello macropod,

I want the macro because I have documents that are about 40 pages long. and there will be about 2 tables per page. I know I said I wanted the whole doc - but that was because I don't know how to count the number of words in a paragraph to determine where the table should stop and where it should start. You see paragraphs with more than 10 words should not be included in the table.

Do you have any idea how to get the number of words in each paragraph??

Thanks,

macropod
10-07-2009, 08:11 PM
Hi samohtwerdna,

That part's easy:

Sub Demo()
Dim oPara As Paragraph
With ActiveDocument
For Each oPara In .Paragraphs
MsgBox "There are " & oPara.Range.Words.Count & " words in:" _
& vbCr & oPara.Range.Text
Next
End With
End Sub
Do note, though, that Word has a strange idea of how to treat dates and numbers. For example, a paragraph containing just:
12/09/1927 counts as 6 words; and
$1,024,512.96 counts as 9 words.
An empty paragraph is counted as 1 word.

fumei
10-08-2009, 10:39 AM
I would just like to reinforce macropod's last comment. Be VERY aware that if you have any "empty" paragraphs used to make space between paragraphs - an unfortunately common thing - each one of those counts as well.

You are not being consistent.

"I want to create a table of an entire document where each paragraph is a cell."

That means to me, ONE table.

"I want the macro because I have documents that are about 40 pages long. and there will be about 2 tables per page. I know I said I wanted the whole doc - but that was because I don't know how to count the number of words in a paragraph to determine where the table should stop and where it should start."

That definitely sounds like more than one table. It sounds like many many tables. There is no easy way to get "2 tables per page". How are you coming up with that? Is that an actual requirement? I strongly suggest you write out - precisely - what your requirements are.

"All paragraphs that start without a Tab I want to go into the first column and all paragraphs that start with a tab I want to go into the 2nd column. "

That sounds like:

For Each Paragraph
If Word.Count < 10 Then
If startOFparagraph <> vbTab Then
put Paragraph into rowX/col1
Else
put Paragraph into rowX/col2
End If
Else ' it IS > 10 words
' what?
End If
Next
I can see flaws there.

Say Paragraph_A is < 10, and has no tab, OK, put it is rowX/Col1...of what table? Do you need to make a table first?

What happens if the next paragraph IS > 10. You state it is NOT to go into a table.

So, you now have a table with Paragraph_A in rowX/Col1 - and nothing in Col2 - then no table (for the next Paragraph), then say the next paragraph is < 10, and it IS tabbed, so......you need to make a new table, and put that paragraph into column 2, leaving column 1 blank.

I am definitely not trying to discourage you from trying to do this, but you DO need to be precise and explicit as to what is going to be the logic.

I am not saying the above scenario example is wrong; I am trying to point out that you need to write out EXACTLY what it is you need.

Clearly, if you have separations - the paragraphs > 10 words that are NOT going into tables - you need to create multiple tables. Which can be done, but you MUST must must must be absolutely precise about the conditions that will create those tables.

If you are going to do this via code, Convert> TextToTable is not going to work without very precise logic.

Of course, you could simply do it manually.

macropod
10-08-2009, 02:11 PM
Hi fumei,

I understood the requirement to be one (or, apparently, more than one) table with two columns. The more expansive specification is not necessarily inconsistent with the 'two tables per page' scenario.

macropod
10-09-2009, 12:57 AM
Hi samohtwerdna,

Try the following. You'll note that, instead of using 'oPara.Range.Words.Count', I used 'oPara.Range.ComputeStatistics(wdStatisticWords)' for a more reliable word count.
Sub Tabulate()
Application.ScreenUpdating = False
Dim oPara As Paragraph, oRng As Range, i As Integer
With ActiveDocument
With .Content.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "^t"
.Replacement.Text = "ÿ"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False
.Execute Replace:=wdReplaceAll
.Text = "^pÿ"
.Replacement.Text = "^p^tÿ"
.Execute Replace:=wdReplaceAll
End With
If .Characters(1) = "ÿ" Then .Characters(1).InsertBefore vbTab
For Each oPara In .Paragraphs
i = oPara.Range.ComputeStatistics(wdStatisticWords)
If i > 0 And i < 11 And Not oPara.Next Is Nothing Then
If oRng Is Nothing Then
Set oRng = oPara.Range
Else
oRng.MoveEnd wdParagraph, 1
End If
Else
If Not oRng Is Nothing Then
oRng.ConvertToTable Separator:=wdSeparateByTabs, NumColumns:=2, _
Format:=wdTableFormatNone, ApplyBorders:=True, ApplyShading:= _
True, ApplyFont:=True, ApplyColor:=True, ApplyHeadingRows:=True, _
ApplyLastRow:=False, ApplyFirstColumn:=True, ApplyLastColumn:=False, _
AutoFit:=True, AutoFitBehavior:=wdAutoFitFixed
Set oRng = Nothing
End If
End If
Next
With .Content.Find
.Text = "ÿ"
.Replacement.Text = "^t"
.Execute Replace:=wdReplaceAll
End With
End With
Application.ScreenUpdating = True
End SubThe above code also implements what I suggested earlier for dealing with intra-paragraph tabs and restore the tabs for the paragraphs that started with them in the first place.

samohtwerdna
10-09-2009, 05:16 AM
Thank macropod,

One problem though, that will put everything in the first column of the table instead of separating the tabbed paragraphs to the 2nd column.

macropod
10-09-2009, 12:37 PM
Hi samohtwerdna,

that will put everything in the first column of the table instead of separating the tabbed paragraphs to the 2nd columnNot when I run it -paragraphs beginning with tabs go to the 2nd column.