Consulting

Results 1 to 3 of 3

Thread: Formatting Individual lines in a table cell

  1. #1
    VBAX Newbie
    Joined
    Jun 2016
    Location
    Sheffield
    Posts
    2
    Location

    Formatting Individual lines in a table cell

    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

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    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
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    VBAX Newbie
    Joined
    Jun 2016
    Location
    Sheffield
    Posts
    2
    Location

    Thanks

    Great!<br><br>Thanks for that Greg<br>
    <br>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •