ckb
03-06-2012, 08:40 PM
I have been scratching my head at this code for a few days now with great frustration.
The background: client wants document formatted with a certain look. Essentially, anything I have as a "Heading 1" or "Heading 2" has to end up on the left side of the page, and anything else has to end up on the right side of the page (Heading 3, Heading 4, etc, as well as a few hidden text styles. The first line of text on the right has to be vertically aligned with the preceding heading 2.
The first thing I tried was to use text columns, and through a combination of section breaks and column breaks. While I can get this to work, as soon as there is a page break, the system falls apart.
The 2nd thing I tried was to use a table with two columns. If the text is Heading 1 or Heading 2, add a row and set the left cell's text as the Heading 1 or Heading 2, respectively, and if anything else, add the text to the (now existing) empty right cell, and apply paragraph styles to each paragraph in the cell. This seems to be falling apart, and I can't figure out why.
A short code fragment:
For i = i to ActiveDocuments.Paragraphs.Count
If ...
...
Else 'Any Paragraph Style Not "Heading 1" or "Heading 2"
With myTable
.Cell(.Rows.Count, 2).Range.Paragraphs.Last.Range.Text = .Cell(.Rows.Count, 2).Range.Paragraphs.Last.Text & Left(ActiveDocument.Paragraphs(i).Range.Text, Len(ActiveDocument.Paragraphs(i).Range.Text) - 1)
.Cell(.Rows.Count, 2).Range.Paragraphs.Last.Style = ActiveDocument.Paragraphs(i).Style
End With
End If
Next
The result is that only the last paragraph in the cell has the correct style, and the rest seem to lose the style they were supposed to have.
I cannot do an individual cell for each paragraph by creating a new row due to the aformentioned hidden text styles, since when they turn off, the result would be a blank line adjacent to the Heading 1 or Heading 2.
Any help is appreciated.
The background: client wants document formatted with a certain look. Essentially, anything I have as a "Heading 1" or "Heading 2" has to end up on the left side of the page, and anything else has to end up on the right side of the page (Heading 3, Heading 4, etc, as well as a few hidden text styles. The first line of text on the right has to be vertically aligned with the preceding heading 2.
The first thing I tried was to use text columns, and through a combination of section breaks and column breaks. While I can get this to work, as soon as there is a page break, the system falls apart.
The 2nd thing I tried was to use a table with two columns. If the text is Heading 1 or Heading 2, add a row and set the left cell's text as the Heading 1 or Heading 2, respectively, and if anything else, add the text to the (now existing) empty right cell, and apply paragraph styles to each paragraph in the cell. This seems to be falling apart, and I can't figure out why.
A short code fragment:
For i = i to ActiveDocuments.Paragraphs.Count
If ...
...
Else 'Any Paragraph Style Not "Heading 1" or "Heading 2"
With myTable
.Cell(.Rows.Count, 2).Range.Paragraphs.Last.Range.Text = .Cell(.Rows.Count, 2).Range.Paragraphs.Last.Text & Left(ActiveDocument.Paragraphs(i).Range.Text, Len(ActiveDocument.Paragraphs(i).Range.Text) - 1)
.Cell(.Rows.Count, 2).Range.Paragraphs.Last.Style = ActiveDocument.Paragraphs(i).Style
End With
End If
Next
The result is that only the last paragraph in the cell has the correct style, and the rest seem to lose the style they were supposed to have.
I cannot do an individual cell for each paragraph by creating a new row due to the aformentioned hidden text styles, since when they turn off, the result would be a blank line adjacent to the Heading 1 or Heading 2.
Any help is appreciated.