VBA/Excel/Access/Word/Word/Document Table
Содержание
- 1 Adding a Column to a Table
- 2 Adding a Row to a Table
- 3 Converting a Table or Rows to Text
- 4 Converts the current selection to a five-column table, separating the information at commas.
- 5 Declare the variable tempTable and then select the first table in the document named Log.doc and assign its Range object to tempTable
- 6 Deletes the first cell in the first row of the first table in the active document and shifts the other cells in the first row to the left to fill the gap:
- 7 delete the column in which the range testRange ends if the range is more than one column wide:
- 8 Deleting a Column from a Table
- 9 Deleting a Row from a Table
- 10 Deleting Cells
- 11 Entering Text in a Cell
- 12 Finding Out Where the Selection Is in the Table
- 13 Inserting a Cell
- 14 inserts a new, blank, non-autofitting table containing 10 rows and 5 columns at the current position of the insertion point in the active document:
- 15 Making Sure the Selection Is within a Table
- 16 Returning the Text within a Cell
- 17 Selecting a Column
- 18 Selecting a Range of Cells
- 19 Selecting a Row
- 20 Selecting a Table in the active document:
- 21 Selects the first table in the current selection:
- 22 Set the Height property of the row or rows in question by specifying the height in points
- 23 Setting the Height of One or More Rows
- 24 Setting the Width of a Column
- 25 Strip off the last two characters when assigning the Text property to a string:
- 26 The SetWidth method sets the width of one or more columns and specify how the other columns in the table should change as a result: expression.SetWidth ColumnWidth, RulerStyle
- 27 The Width property lets you change the width of a column without worrying about the effect on the other columns. Specify the width you want in points-for example:
- 28 Use the ConvertToText method with a Table object, a Row object, or a Rows collection: converts only the first row of the selected table to tab-delimited text
- 29 wdDeleteCellsEntireRow deletes the whole row.
- 30 wdDeleteCellsShiftLeft moves cells across to the left to fill the gap.
- 31 wdDeleteCellsShiftUp moves cells up to fill the gap.
- 32 wdEndOfRangeColumnNumber returns the number of the column in which the end of the selection or range falls.
Adding a Column to a Table
<source lang="vb">
"The following statements use the Count property to check the number of columns in the first table in the active document and Sub add()
With ActiveDocument.Tables(1) .Select If .Columns.Count < 5 Then Do Until .Columns.Count = 5 .Columns.add BeforeColumn:=.Columns(.Columns.Count) Loop End If End With
End Sub
</source>
Adding a Row to a Table
<source lang="vb">
Sub addRow()
Documents("yourDoc.doc").Tables(1).Rows.Add BeforeRow:=1
End Sub
</source>
Converting a Table or Rows to Text
<source lang="vb">
Sub sep()
Selection.Tables(1).ConvertToText Separator:="*"
End Sub
</source>
Converts the current selection to a five-column table, separating the information at commas.
<source lang="vb">
Sub sele()
Set myTable = Selection.ConvertToTable(wdSeparateByCommas, _ Selection.Paragraphs.Count, 5, , , , , , , , , , , True, _ wdAutoFitContent, wdWord9TableBehavior)
End Sub
</source>
Declare the variable tempTable and then select the first table in the document named Log.doc and assign its Range object to tempTable
<source lang="vb">
Sub tableSel()
Dim tempTable Documents("Log.doc").Tables(1).Select Set tempTable = Selection.Tables(1).Range tempRange.Tables(2).Select
End Sub
</source>
Deletes the first cell in the first row of the first table in the active document and shifts the other cells in the first row to the left to fill the gap:
<source lang="vb">
Sub del()
ActiveDocument.Tables(1).Rows(1).Cells(1).Delete _ ShiftCells:=wdDeleteCellsShiftLeft
End Sub
</source>
delete the column in which the range testRange ends if the range is more than one column wide:
<source lang="vb"> With testRange If .Information(wdStartOfRangeColumnNumber) <> _ .Information(wdEndOfRangeColumnNumber) Then _ .Tables(1).Columns(.Information _ (wdEndOfRangeColumnNumber)).Delete End With </source>
Deleting a Column from a Table
<source lang="vb">
Sub del()
ActiveDocument.Tables(1).Columns(1).Delete
End Sub
</source>
Deleting a Row from a Table
<source lang="vb">
Sub del()
Documents("yourDoc.doc").Tables(3).Rows(1).Delete
End Sub
</source>
Deleting Cells
<source lang="vb">
"wdDeleteCellsEntireColumn deletes the whole column the specified cell or cells is in. Sub del()
ActiveDocument.Tables(1).Rows(1).Cells(1).Delete _ ShiftCells:=wdDeleteCellsEntireColumn
End Sub
</source>
Entering Text in a Cell
<source lang="vb">
Sub text()
With Selection.Tables(1).Rows(1) .Cells(1).Range.text = "Sample text in first cell." .Cells(2).Range.text = "Sample text in second cell." .Cells(3).Range.text = "Sample text in third cell." End With
End Sub
</source>
Finding Out Where the Selection Is in the Table
<source lang="vb">
Sub sel()
If Selection.Information(wdAtEndOfRowMarker) = True Then _ Selection.MoveLeft Unit:=wdCharacter, Count:=1
End Sub
</source>
Inserting a Cell
<source lang="vb">
Sub insert()
Documents("Tables.doc").Tables(1).Rows(1).Cells.Add _ BeforeCell:=Documents("Tables.doc").Tables(1).Rows(1).Cells(2)
End Sub
</source>
inserts a new, blank, non-autofitting table containing 10 rows and 5 columns at the current position of the insertion point in the active document:
<source lang="vb">
Sub table()
ActiveDocument.Tables.add Range:=Selection.Range, NumRows:=10, _ NumColumns:=5, DefaultTableBehavior:=wdWord8TableBehavior
End Sub
</source>
Making Sure the Selection Is within a Table
<source lang="vb">
Sub withTable()
If Selection.Information(wdWithInTable) = True Then "take actions here End If
End Sub
</source>
Returning the Text within a Cell
<source lang="vb">
Sub text()
Dim strCellText As String strCellText = ActiveDocument.Tables(1).Rows(2).Cells(1).Range.Text Debug.Print strCellText
End Sub
</source>
Selecting a Column
<source lang="vb">
"To select a column, use the Select method with the appropriate Column object. Sub sel()
Documents("yourDoc.doc").Tables(3).Columns(2).Select
End Sub
</source>
Selecting a Range of Cells
<source lang="vb">
"To select a range of cells within a table, declare a Range variable, assign to it the cells you want to select, and then select the range Sub cellSel()
Dim myCells As Range With ActiveDocument Set myCells = .Range(Start:=.Tables(1).Cell(1, 1).Range.Start, _ End:=.Tables(1).Cell(1, 4).Range.End) myCells.Select End With
End Sub
</source>
Selecting a Row
<source lang="vb">
Sub sel()
Documents("Tables.doc").Tables(.Tables.Count).Rows.Last.Select
End Sub
</source>
Selecting a Table in the active document:
<source lang="vb">
Sub table1()
ActiveDocument.Tables(1).Select
End Sub
</source>
Selects the first table in the current selection:
<source lang="vb">
Sub tableSel1()
Selection.Tables(1).Select
End Sub
</source>
Set the Height property of the row or rows in question by specifying the height in points
<source lang="vb">
Sub height()
Documents("Tables.doc").Tables(3).Rows(3).Height = 33
End Sub
</source>
Setting the Height of One or More Rows
<source lang="vb">
Sub rowHeight()
ActiveDocument.Tables(4).Rows(2).HeightRule = wdRowHeightAuto
End Sub
</source>
Setting the Width of a Column
<source lang="vb">
Sub auto()
ActiveDocument.Tables(1).Columns.AutoFit
End Sub
</source>
Strip off the last two characters when assigning the Text property to a string:
<source lang="vb">
Sub strip()
Dim strCellText As String strCellText = ActiveDocument.Tables(3).Rows(2).Cells(1).Range.Text Debug.Print strCellText strCellText = Left(strCellText, Len(strCellText) - 2) Debug.Print strCellText
End Sub
</source>
The SetWidth method sets the width of one or more columns and specify how the other columns in the table should change as a result: expression.SetWidth ColumnWidth, RulerStyle
<source lang="vb">
Sub ruler()
ActiveDocument.Tables(1).Columns(2).SetWidth ColumnWidth:=50, _ RulerStyle:=wdAdjustProportional
End Sub
</source>
The Width property lets you change the width of a column without worrying about the effect on the other columns. Specify the width you want in points-for example:
<source lang="vb">
Sub width()
ActiveDocument.Tables(11).Columns(44).Width = 100
End Sub
</source>
Use the ConvertToText method with a Table object, a Row object, or a Rows collection: converts only the first row of the selected table to tab-delimited text
<source lang="vb">
Sub tab()
Selection.Tables(1).Rows(1).ConvertToText Separator:=wdSeparateByTabs
End Sub
</source>
wdDeleteCellsEntireRow deletes the whole row.
<source lang="vb">
Sub del()
ActiveDocument.Tables(1).Rows(1).Cells(1).Delete _ ShiftCells:=wdDeleteCellsEntireRow
End Sub
</source>
wdDeleteCellsShiftLeft moves cells across to the left to fill the gap.
<source lang="vb">
Sub del()
ActiveDocument.Tables(1).Rows(1).Cells(1).Delete _ ShiftCells:=wdDeleteCellsShiftLeft
End Sub
</source>
wdDeleteCellsShiftUp moves cells up to fill the gap.
<source lang="vb">
Sub del()
ActiveDocument.Tables(1).Rows(1).Cells(1).Delete _ ShiftCells:=wdDeleteCellsShiftUp
End Sub
</source>
wdEndOfRangeColumnNumber returns the number of the column in which the end of the selection or range falls.
<source lang="vb"> With testRange If .Information(wdStartOfRangeColumnNumber) <> _ .Information(wdEndOfRangeColumnNumber) Then _ .Tables(1).Columns(.Information _ (wdEndOfRangeColumnNumber)).Delete End With </source>