VBA/Excel/Access/Word/Word/Document Table

Материал из VB Эксперт
Версия от 12:48, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Содержание

Adding a Column to a Table

 
"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



Adding a Row to a Table

 
Sub addRow()
    Documents("yourDoc.doc").Tables(1).Rows.Add BeforeRow:=1
End Sub



Converting a Table or Rows to Text

 
Sub sep()
    Selection.Tables(1).ConvertToText Separator:="*"
End Sub



Converts the current selection to a five-column table, separating the information at commas.

 
Sub sele()
    Set myTable = Selection.ConvertToTable(wdSeparateByCommas, _
        Selection.Paragraphs.Count, 5, , , , , , , , , , , True, _
        wdAutoFitContent, wdWord9TableBehavior)
End Sub



Declare the variable tempTable and then select the first table in the document named Log.doc and assign its Range object to tempTable

 
Sub tableSel()
    Dim tempTable
    Documents("Log.doc").Tables(1).Select
    Set tempTable = Selection.Tables(1).Range
    tempRange.Tables(2).Select
End Sub



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:

 
Sub del()
    ActiveDocument.Tables(1).Rows(1).Cells(1).Delete _
        ShiftCells:=wdDeleteCellsShiftLeft
End Sub



delete the column in which the range testRange ends if the range is more than one column wide:

 
 
     With testRange
         If .Information(wdStartOfRangeColumnNumber) <> _
             .Information(wdEndOfRangeColumnNumber) Then _
             .Tables(1).Columns(.Information _
             (wdEndOfRangeColumnNumber)).Delete
     End With



Deleting a Column from a Table

 
Sub del()
    ActiveDocument.Tables(1).Columns(1).Delete
End Sub



Deleting a Row from a Table

 
Sub del()
    Documents("yourDoc.doc").Tables(3).Rows(1).Delete
End Sub



Deleting Cells

 
"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



Entering Text in a Cell

 
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



Finding Out Where the Selection Is in the Table

 
Sub sel()
    If Selection.Information(wdAtEndOfRowMarker) = True Then _
        Selection.MoveLeft Unit:=wdCharacter, Count:=1
End Sub



Inserting a Cell

 
Sub insert()
    Documents("Tables.doc").Tables(1).Rows(1).Cells.Add _
        BeforeCell:=Documents("Tables.doc").Tables(1).Rows(1).Cells(2)
End Sub



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:

 
Sub table()
    ActiveDocument.Tables.add Range:=Selection.Range, NumRows:=10, _
        NumColumns:=5, DefaultTableBehavior:=wdWord8TableBehavior
End Sub



Making Sure the Selection Is within a Table

 
Sub withTable()
    If Selection.Information(wdWithInTable) = True Then
        "take actions here
    End If
End Sub



Returning the Text within a Cell

 
Sub text()
    Dim strCellText As String
    strCellText = ActiveDocument.Tables(1).Rows(2).Cells(1).Range.Text
    Debug.Print strCellText
End Sub



Selecting a Column

 
"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



Selecting a Range of Cells

 
"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



Selecting a Row

 
Sub sel()
    Documents("Tables.doc").Tables(.Tables.Count).Rows.Last.Select
End Sub



Selecting a Table in the active document:

 
Sub table1()
    ActiveDocument.Tables(1).Select
End Sub



Selects the first table in the current selection:

 
Sub tableSel1()
    Selection.Tables(1).Select
End Sub



Set the Height property of the row or rows in question by specifying the height in points

 
Sub height()
    Documents("Tables.doc").Tables(3).Rows(3).Height = 33
End Sub



Setting the Height of One or More Rows

 
Sub rowHeight()
    ActiveDocument.Tables(4).Rows(2).HeightRule = wdRowHeightAuto
End Sub



Setting the Width of a Column

 
Sub auto()
    ActiveDocument.Tables(1).Columns.AutoFit
End Sub



Strip off the last two characters when assigning the Text property to a string:

 
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



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

 
Sub ruler()
    ActiveDocument.Tables(1).Columns(2).SetWidth ColumnWidth:=50, _
        RulerStyle:=wdAdjustProportional
End Sub



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:

 
Sub width()
    ActiveDocument.Tables(11).Columns(44).Width = 100
End Sub



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

 
Sub tab()
    Selection.Tables(1).Rows(1).ConvertToText Separator:=wdSeparateByTabs
End Sub



wdDeleteCellsEntireRow deletes the whole row.

 
Sub del()
    ActiveDocument.Tables(1).Rows(1).Cells(1).Delete _
        ShiftCells:=wdDeleteCellsEntireRow
End Sub



wdDeleteCellsShiftLeft moves cells across to the left to fill the gap.

 
Sub del()
    ActiveDocument.Tables(1).Rows(1).Cells(1).Delete _
        ShiftCells:=wdDeleteCellsShiftLeft
End Sub



wdDeleteCellsShiftUp moves cells up to fill the gap.

 
Sub del()
    ActiveDocument.Tables(1).Rows(1).Cells(1).Delete _
        ShiftCells:=wdDeleteCellsShiftUp
End Sub



wdEndOfRangeColumnNumber returns the number of the column in which the end of the selection or range falls.

 
 
     With testRange
         If .Information(wdStartOfRangeColumnNumber) <> _
             .Information(wdEndOfRangeColumnNumber) Then _
             .Tables(1).Columns(.Information _
             (wdEndOfRangeColumnNumber)).Delete
     End With