Microsoft Word ConvertToTable method Visual Basic for Applications

Microsoft Word ConvertToTable method Visual Basic for Applications

ConvertToTable

This method converts text within a range to a table. Returns the table as a Table object.

Syntax

expression.ConvertToTable (SeparatorNumRowsNumColumnsInitialColumnWidthFormatApplyBordersApplyShadingApplyFontApplyColorApplyHeadingRowsApplyLastRowApplyFirstColumnApplyLastColumnAutoFitAutoFitBehaviorDefaultTableBehavior)

Parameters

  1. Separator: Specifies the character used to separate text into cells. It can be a character or one of the following WdTableFieldSeparator constant. If this argument is not specified, the value of the DefaultTableSeparator property is used as shown in the code example:
    • wdSeparateByDefaultListSeparator
    • wdSeparateByCommas
    • wdSeparateByParagraphs
    • wdSeparateByTabs
    • wdSeparatorColon
    • wdSeparatorEmDash
    • wdSeparatorEnDash
    • wdSeparatorHyphen
    • wdSeparatorPeriod
  2. NumRows: The number of rows in the table. If this argument is not specified, Microsoft Word sets the number of rows, based on the contents of the selected range.
  3. NumColumns: The number of columns in the table. If this argument is removed, Microsoft Word sets the number of columns, based on the contents of the given range.
  4. InitialColumnWidth: The initial width of each column, It is referred in points. If this argument is removed, Microsoft Word calculates and adjusts the column width so that the table stretches from margin to margin.
  5. Format: Specifies one of the predefined formats listed in the Table AutoFormat dialog box. It can be one of the WdTableFormat constants as listed below:
    • wdTableFormat3DEffects1 to wdTableFormat3DEffects3
    • wdTableFormatClassic1 to wdTableFormatClassic4
    • wdTableFormatColorful1 to wdTableFormatColorful3
    • wdTableFormatColumns1 to wdTableFormatColumn4
    • wdTableFormatContemporary
    • wdTableFormatElegant
    • wdTableFormatGrid1 to wdTableFormatGrid7
    • wdTableFormatList1 to wdTableFormatList8
    • wdTableFormatNone
    • wdTableFormatProfessional
    • wdTableFormatSimple1 to wdTableFormatSimple3
    • wdTableFormatSubtle1
    • wdTableFormatSubtle2
    • wdTableFormatWeb1 to wdTableFormatWeb3
  6. ApplyBorders: True to apply the border.
  7. ApplyShadding: True to apply the shading.
  8. ApplyFont: True to apply the font.
  9. ApplyColor: True to apply the color.
  10. ApplyHeadingRows: True to apply the heading-row.
  11. ApplyLastRow: True to apply the last-row.
  12. ApplyFirstColumn: True to apply the first-column.
  13. ApplyLastColumn: True to apply the last-column.
  14. AutoFit: True to decrease the width of the table columns as much as possible without changing the way text wraps in the cells.
  15. AutoFitBehavior: Sets the AutoFit rules for how Microsoft Word sizes a table. It can be one of the following WdAutoFitBehavior constant. If Default Table Behavior is wdWord8TableBehavior, this argument is ignored.
    • wdAutoFitContent
    • wdAutoFitFixed
    • wdAutoFitWindow
  16. DefaultTableBehavior: Sets a value that specifies whether Microsoft Word automatically resizes cells in a table to fit the contents (AutoFit). It can be one of the WdDefaultTableBehavior constant:
    • wdWord8TableBehavior
    • wdWord9TableBehavior

Note: all parameters are optional and variant data type by nature for ConvertToTable method

Example

Code example

Public Sub ConvertToTable()
    'Grabe Selection
    Dim oRange As Range
    Set oRange = Selection.Range
        
    'Declare a table object to hold the result
    Dim oTable As Table
    'Query to convert text to table
    Set oTable = oRange.ConvertToTable(Separator:=wdSeparatorEmDash, Format:=wdTableFormatApplyBorders, ApplyBorders:=True, ApplyHeadingRows:=True)
    oTable.Select
    
    'memory cleanup
    Set oRange = Nothing
    Set oTable = Nothing
End Sub

Output

Please leave your comments or queries under comment section also please do subscribe to out blogs to keep your self upto date.

Leave a Reply

Your email address will not be published.