Home > OS >  Checking if a table has more than one row VBA
Checking if a table has more than one row VBA

Time:08-25

I am writing a document in word using VBA. I have made a table inside a rich text content control and also have made a button which can be pressed to open up a user form. The problem I am facing is every time I run the code it replaces the table within the rich text control. I wanted to know if there is a 'if' function to say if there is more than one row in the table then do something else? The code I have to make the table is

Sub TableCRT()
Selection.Range.ContentControls.Add (wdContentControlRichText)
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=1, NumColumns:= _
    4, defaulttablebehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
    wdAutoFitFixed
    
With ActiveDocument.Tables(1).Rows(1)
    .Cells(1).Range.Text = "heading 1"
    .Cells(2).Range.Text = "heading 2"
    .Cells(3).Range.Text = "heading 3"
    .Cells(4).Range.Text = "heading 4"
End With
End Sub

I have also tried

Private Sub CommandButton1_Click()
If ActiveDocument.Tables(1).Count < 0 Then
    Selection.Range.ContentControls.Add (wdContentControlRichText)
    ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=1, NumColumns:= _
    4, defaulttablebehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
    wdAutoFitFixed
    
    With ActiveDocument.Tables(1).Rows(1)
        .Cells(1).Range.Text = "heading 1"
        .Cells(2).Range.Text = "heading 2"
        .Cells(3).Range.Text = "heading 3"
        .Cells(4).Range.Text = "heading 4"
    End With
    Dim Form As Object
    Set Form = UserForm1
    Form.Show
Else
    Dim Form As Object
    Set Form = UserForm1
    Form.Show
End Sub

Thanks

CodePudding user response:

You add every time a ContentControll but you allways refer to the first table in the document.

Try something like this

Dim g As ContentControl
Dim t As Table
Set g = Selection.Range.ContentControls.Add(wdContentControlRichText)
Set t = g.Range.Tables.Add(g.Range, 1, 4) ' change this to your code
With t.Rows(1)
.Cells(1).Range.Text = "heading 1"
.Cells(2).Range.Text = "heading 2"
.Cells(3).Range.Text = "heading 3"
.Cells(4).Range.Text = "heading 4"
End With

CodePudding user response:

This code checks if the number of content controls and tables are 0 before trying to add them. Declaring a table variable and setting the added table to that variable makes it easier to refer to the table and modify it later. Finally, if the table has more than one row, you can replace the MsgBox line with whatever processing you want to happen:

Sub TableCRT()
    Dim oTable As Table

    If ActiveDocument.ContentControls.Count = 0 Then
        Selection.Range.ContentControls.Add (wdContentControlRichText)
    End If
    If ActiveDocument.Tables.Count = 0 Then
        Set oTable = ActiveDocument.Tables.Add(Range:=Selection.Range, NumRows:=1, NumColumns:= _
            4, defaulttablebehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
            wdAutoFitFixed)
            
        With oTable.Rows(1)
            .Cells(1).Range.Text = "heading 1"
            .Cells(2).Range.Text = "heading 2"
            .Cells(3).Range.Text = "heading 3"
            .Cells(4).Range.Text = "heading 4"
        End With
    Else
        Set oTable = ActiveDocument.Tables(1)
    End If
    If oTable.Rows.Count > 1 Then
        MsgBox "Hello"
        'Do something here
    End If
End Sub
  • Related