Consulting

Results 1 to 6 of 6

Thread: Erase Table line in VBA

  1. #1
    VBAX Regular
    Joined
    Nov 2017
    Posts
    10
    Location

    Erase Table line in VBA

    Hi,

    Is it possible to erase lines from a table using VBA, like you would using this tool:

    biFhpGS.png

    I want to erase specific lines from a table in word.
    I tried recording it, but the tool greyed out.

    All help is greatly appreciated!
    - Jeremy

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    By using the linestyle:

    Sub ScratchMacro()
    'A basic Word macro coded by Greg Maxey, http://gregmaxey.com/word_tips.html, 11/17/2017
    Dim oTbl As Table
      Set oTbl = Selection.Tables(1)
      With oTbl.Range
        .Cells(2).Borders(wdBorderLeft).LineStyle = wdLineStyleNone
        .Cells(3).Borders(wdBorderBottom).LineStyle = wdLineStyleNone
      End With
    lbl_Exit:
      Exit Sub
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    VBAX Regular
    Joined
    Nov 2017
    Posts
    10
    Location
    Works like a charm!
    Thank you Greg !

  4. #4
    VBAX Regular
    Joined
    Nov 2017
    Posts
    10
    Location
    Hey,

    When I add multiple lines of text to the table once I've removed the lines using VBA,
    I get something like this:

    etk00Hq.png

    However, when using the eraser tool it looks like this:

    B0PfX1T.png

    Which is how I'd like it to look.
    I must me missing something.

    This is the code I'm using:
    Dim T1 As table
    'Create the table
        Set T1 = ActiveDocument.Tables.Add(ActiveDocument.Bookmarks("TABEL").Range.Paragraphs(1).Range, 3, 7, wdWord9TableBehavior, wdAutoFitContent)
    'Populate the table
    T1.Cell(1, 1).Range.Text = frmMood.TitelWerkstuk1.Value 'Fetch Value from Userform Textbox
        T1.Cell(1, 2).Range.Text = "Beoordeling:"
        T1.Cell(1, 3).Range.Text = "ZW"
        T1.Cell(1, 4).Range.Text = "M"
        T1.Cell(1, 5).Range.Text = "V"
        T1.Cell(1, 6).Range.Text = "G"
        T1.Cell(1, 7).Range.Text = "ZG"
    'Format Table
        T1.Rows(1).Select
        Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
        Selection.Font.Bold = True
        T1.Borders.Enable = True
    T1.Cell(3, 1).Borders(wdBorderTop).LineStyle = wdLineStyleNone
    'Enter text in the Table
    T1.Cell(2, 1).Range.Text = "This is text" & vbNewLine & "Some more"
    All help is greatly appreciated!
    - Jeremy

  5. #5
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    The eraser actually merges internal cells (not just remove the border). Try:

    Sub ScratchMacro()
    'A basic Word macro coded by Greg Maxey, http://gregmaxey.com/word_tips.html, 11/21/2017
    Dim T1 As Table
     'Create the table
    Set T1 = ActiveDocument.Tables.Add(ActiveDocument.Bookmarks("TABEL").Range.Paragraphs(1).Range, 3, 7, wdWord9TableBehavior, wdAutoFitContent)
     'Populate the table
    T1.Cell(1, 1).Range.Text = "Blah, blah" 'Fetch Value from Userform Textbox
    T1.Cell(1, 2).Range.Text = "Beoordeling:"
    T1.Cell(1, 3).Range.Text = "ZW"
    T1.Cell(1, 4).Range.Text = "M"
    T1.Cell(1, 5).Range.Text = "V"
    T1.Cell(1, 6).Range.Text = "G"
    T1.Cell(1, 7).Range.Text = "ZG"
     'Format Table
    T1.Rows(1).Select
    Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
    Selection.Font.Bold = True
    T1.Borders.Enable = True
    T1.Cell(2, 1).Merge MergeTo:=T1.Cell(Row:=3, Column:=1)
     'Enter text in the Table
    T1.Cell(2, 1).Range.Text = "This is text" & vbNewLine & "Some more"
    lbl_Exit:
      Exit Sub
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  6. #6
    VBAX Regular
    Joined
    Nov 2017
    Posts
    10
    Location
    Thanks once again!
    Worked perfectly, thank you
    - Jeremy

Posting Permissions

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