PDA

View Full Version : Formatting Individual lines in a table cell



STEVE-BART
06-06-2016, 02:24 AM
Hi, I'm looking for help as I'm completely baffled!

Hi have VBA code which opens a word document and writes text into individual cells.

This is fine and I can also format individual cells

What I cant work out how to do is format individual lines separately within a cell.

Heres my code so far:-

Set wrdApp = CreateObject("Word.Application")
Set wrddoc = wrdApp.Documents.Open("C:\Table Trials.docx")
wrdApp.Visible = True


wrddoc.Tables(2).Cell(2, 4).Range.ParagraphFormat.Alignment = wdAlignParagraphCenter

t = "Start line" '& vbNewLine & "Mid line" & Chr(10) & "last line"

wrddoc.Tables(2).Cell(2, 4).Range.Text = t

With wrddoc.Tables(2).Cell(2, 4).Range
.Font.ColorIndex = wdBlue
End With

end sub


Many thanks in anticipation

gmaxey
06-06-2016, 04:14 AM
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oRng As Range
Dim lngIndex As Long
'Paragraphs. Format second paragraph
Set oRng = Selection.Tables(1).Cell(1, 1).Range
oRng.Paragraphs(2).Range.Font.ColorIndex = wdBrightGreen
'Line breaks format line 1
Set oRng = Selection.Tables(1).Cell(1, 2).Range
oRng.Collapse wdCollapseStart
oRng.MoveEndUntil Chr(11)
oRng.Font.ColorIndex = wdBrightGreen
'Line breaks format line 2
Set oRng = Selection.Tables(1).Cell(1, 2).Range
oRng.Collapse wdCollapseStart
For lngIndex = 1 To 2
oRng.MoveEndUntil Chr(11)
oRng.MoveEnd wdCharacter, 1
oRng.Collapse wdCollapseEnd
Next
oRng.MoveEnd wdCharacter, -1
oRng.MoveStartUntil Chr(11), wdBackward
oRng.Font.ColorIndex = wdBrightGreen
'Line breaks format last line
Set oRng = Selection.Tables(1).Cell(1, 2).Range
oRng.Collapse wdCollapseEnd
oRng.MoveEnd wdCharacter, -1
oRng.MoveStartUntil Chr(11), wdBackward
oRng.Font.ColorIndex = wdBrightGreen
lbl_Exit:
Exit Sub
End Sub

STEVE-BART
06-06-2016, 05:02 AM
Great!<br><br>Thanks for that Greg<br>
<br>