罫線の描画に関しても,[Borders] コレクションと [Border] オブジェクトが関わるため,一旦 [Borders] コレクションの参照を作成する必要があります。
Dim targetRange As Excel.Range = Nothing
Dim targetBorders As Excel.Borders = Nothing
Dim targetRightBorder As Excel.Border = Nothing
Dim targetLeftBorder As Excel.Border = Nothing
Dim targetTopBorder As Excel.Border = Nothing
Dim targetBottomBorder As Excel.Border = Nothing
Dim targetVerticalBorder As Excel.Border = Nothing
Dim targetHorizontalBorder As Excel.Border = Nothing
Try
targetRange = [worksheet].Range("A3:C5")
targetBorders = targetRange.Borders
targetRightBorder = targetBorders.Item(Excel.XlBordersIndex.xlEdgeRight)
targetLeftBorder = targetBorders.Item(Excel.XlBordersIndex.xlEdgeLeft)
targetTopBorder = targetBorders.Item(Excel.XlBordersIndex.xlEdgeTop)
targetBottomBorder = targetBorders.Item(Excel.XlBordersIndex.xlEdgeBottom)
targetVerticalBorder = targetBorders.Item(Excel.XlBordersIndex.xlInsideVertical)
targetHorizontalBorder = targetBorders.Item(Excel.XlBordersIndex.xlInsideHorizontal)
With targetRightBorder
.LineStyle = Excel.XlLineStyle.xlContinuous
.Weight = Excel.XlBorderWeight.xlThin
.ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic
End With
With targetLeftBorder
.LineStyle = Excel.XlLineStyle.xlContinuous
.Weight = Excel.XlBorderWeight.xlThin
.ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic
End With
With targetTopBorder
.LineStyle = Excel.XlLineStyle.xlContinuous
.Weight = Excel.XlBorderWeight.xlThin
.ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic
End With
With targetBottomBorder
.LineStyle = Excel.XlLineStyle.xlContinuous
.Weight = Excel.XlBorderWeight.xlThin
.ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic
End With
With targetVerticalBorder
.LineStyle = Excel.XlLineStyle.xlContinuous
.Weight = Excel.XlBorderWeight.xlHairline
.ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic
End With
With targetHorizontalBorder
.LineStyle = Excel.XlLineStyle.xlContinuous
.Weight = Excel.XlBorderWeight.xlHairline
.ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic
End With
Finally
If Not targetHorizontalBorder Is Nothing Then
Marshal.ReleaseComObject(targetVerticalBorder)
End If
If Not targetVerticalBorder Is Nothing Then
Marshal.ReleaseComObject(targetVerticalBorder)
End If
If Not targetBottomBorder Is Nothing Then
Marshal.ReleaseComObject(targetBottomBorder)
End If
If Not targetTopBorder Is Nothing Then
Marshal.ReleaseComObject(targetTopBorder)
End If
If Not targetLeftBorder Is Nothing Then
Marshal.ReleaseComObject(targetLeftBorder)
End If
If Not targetRightBorder Is Nothing Then
Marshal.ReleaseComObject(targetRightBorder)
End If
If Not targetBorders Is Nothing Then
Marshal.ReleaseComObject(targetBorders)
End If
If Not targetRange Is Nothing Then
Marshal.ReleaseComObject(targetRange)
End If
End Try
それぞれの [Border] オブジェクトに対して,[LineStyle],[Weight],[ColorIndex] の3つのプロパティを指定することで基本的な罫線を描画することができます。