excelvbams-wordborderlinestyle

Remove part of the border of a table excel VBA


There is a set of forms that we sometimes have to fill out at work, and I'm trying to automate the task by having excel VBA recreate the form as a word document and populate with the appropriate data and print as a pdf. I'm getting stuck on removing the border line style. I want there to be no border line on the left side. I have tried different approaches, and the one that seems the most likely that it should work based on my understanding is below: (note: ".Border(xlEdgeleft).LineStyle = xlLineStyleNone" is the line that is giving me trouble)

Sub main()

Dim objWord As Object
Dim objDoc As Object
Dim objHdrRange As Object
Dim myTable As Object

Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Add
Set objHdrRange = objDoc.Sections(1).headers(1).Range
Set myTable = objWord.activedocument.tables.Add(objHdrRange, 5, 5)

With myTable
    .Borders.enable = True
    .Border(xlEdgeleft).LineStyle = xlLineStyleNone
   ‘more code goes here later
End With

Set objDoc = Nothing
Set objHdrRange = Nothing
objWord.Quit

End Sub


Solution

  • xlEdgeLeft and xlLineStyleNone are from the Excel Object Model, not from the Word Object Model, and you need the latter.

    Since you are late-binding, you could add the following lines:

    Const wdBorderLeft As Long = -2
    Const wdLineStyleNone As Long = 0
    

    and replace xlEdgeLeft and xlLineStyleNone with these, respectively.

    See the WdBorderType and WdLineStyle enum docs for more detail.