PDA

View Full Version : Change the colour of the font of the first few characters in cell



yahoo
08-23-2012, 05:12 AM
Hi there

I'm struggling with changing colour of first 11 chars in cell(I'll use some instrrev etc to get the accurate number later on). Didnt expect it to be so difficult as it's so easy in excel...
Anyway here is the part of my code where I go through all cells in one column and make the text bold. Would appreciate if somebody come up with an idea how to make first few chars red. I should mention that I write the code in Access 2010 (and open Word 2010 template) hence the "With appword" bit

With appWord.Tables(1)
For Each oCell In .Range.Columns(3).Cells
oCell.Range.Bold = True
Next
.
.
.
End With

gmaxey
08-23-2012, 02:28 PM
Here is one way:

Sub ScratchMacro()
'A quick macro scratch pad created by Greg Maxey
Dim oTbl As Word.Table
Dim oCell As Cell
Dim oRng As Word.Range
Set oTbl = Selection.Tables(1)
For Each oCell In oTbl.Range.Cells
Set oRng = oCell.Range
oRng.Collapse wdCollapseStart
oRng.MoveEnd wdCharacter, 3
oRng.Font.Color = wdColorRed
Next
End Sub

yahoo
08-24-2012, 12:43 AM
Fantastic! Thanks a lot. I guess it should be doable to add an if statement like: if count of paragraphs is higher than 1 then do the same for each paragraph? I'd do 'for each' paragraph range in cell, then Collapse bit etc, wouldnt I? Cant test it now. Will post the code later on if it works
cheers

fumei
08-24-2012, 11:42 PM
Just a comment. Executing Greg's code means that if a cell has NO text in it, its "base" will become red. That is, any text, ALL text, inserted into it after the fact will be red - not just the first three characters. As for each paragraph...Sub ScratchMacro()
'A quick macro scratch pad created by Greg Maxey
Dim oTbl As Word.Table
Dim oCell As Cell
Dim oRng As Word.Range
Dim oPara As Paragraph
Set oTbl = Selection.Tables(1)
For Each oCell In oTbl.Range.Cells
For Each oPara In oCell.Range.Paragraphs
Set oRng = oPara.Range
oRng.Collapse wdCollapseStart
oRng.MoveEnd wdCharacter, 3
oRng.Font.Color = wdColorRed
Next
Next
End Sub