Link to home
Start Free TrialLog in
Avatar of Kelvin Sparks
Kelvin SparksFlag for New Zealand

asked on

Hide a word table if no data being passed from Access

Hi Experts,

The code snippet below is used to populate a Word table from within Access. It works fine, except that the table exists in the Word Template and sometimes the recordset will not have data. In this case I want to hide the table and bookmarks.

Can anyone supply the code to hide/delete/make invisible the table and bookmarks if there is no data to be inserted i.e. if rsDetails has no data.

Thanks


Kelvin
i = 1
Do Until rsDetails.EOF
    If i = 1 Then
        objWord.ActiveDocument.Bookmarks("Concerns").Select
        objWord.Selection.Text = rsDetails!Concerns
        objWord.ActiveDocument.Bookmarks("DesiredOutcomes").Select
        objWord.Selection.Text = rsDetails![Desired Outcomes]
    Else
        ''Need new row so add it & populate
        objWord.Selection.InsertRowsBelow (1)
        objWord.Selection.MoveUp
        objWord.Selection.MoveDown
        
        objWord.ActiveDocument.Bookmarks.Add Name:="Concerns" & i
        objWord.Selection.MoveRight
        objWord.ActiveDocument.Bookmarks.Add Name:="DesiredOutcomes" & i
    
        objWord.ActiveDocument.Bookmarks("Concerns" & i).Select
        objWord.Selection.Text = rsDetails!Concerns
        objWord.ActiveDocument.Bookmarks("DesiredOutcomes" & i).Select
        objWord.Selection.Text = rsDetails![Desired Outcomes]
  
    End If
    
    i = i + 1
    rsDetails.MoveNext
Loop

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of irudyk
irudyk
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Kelvin Sparks

ASKER

Thanks

Just what I needed
You don't seem to have a table object in your code. That makes it a bit difficult. I suggest that you find the table at the bookmark.

You don't have to keep reproducing the bookmarks unless you want to refer to them later. You can just use the column numbers. I don't know what they are, so I have just guessed 1 and 2.

Also, you could consider not having a table until you know that one is needed, and then adding one in code.

There are two macros in the snippet window, one to delete the unwanted existing table, and the other to add a table if it is wanted.  In the absence of an appropriate database and template, neither is fully tested.
'delete unused table
Sub DelTable()
    Dim wdTable As Word.Table
    Dim wdDoc As Word.Document
    Dim objWord As Word.Application
    Dim wdRow As Word.Row
    
    Set wdDoc = objWord.ActiveDocument
    Set wdTable = wdDoc.Bookmarks("Concerns").Range.Tables(1)
    
    If rsDetails.EOF Then
        wdTable.Delete
    End If
    Do Until rsDetails.EOF
        If i = 1 Then
            Set wdRow = wdTable.Rows(1)
        Else
            Set wdRow = wdTable.Rows.Add
            wdRow.Cells(1).Range.Text = rsDetails!Concerns
            wdRow.Cells(2).Range.Text = rsDetails![Desired Outcomes]
        End If
        i = i + 1
        rsDetails.MoveNext
    Loop
 
End Sub
 
'Add table if needed
Sub AddTable()
    Dim wdTable As Word.Table
    Dim wdDoc As Word.Document
    Dim objWord As Word.Application
    Dim wdRow As Word.Row
    Dim wdRange As Word.Range
    
    Set wdDoc = objWord.ActiveDocument
    
    If Not rsDetails.EOF Then
        Set wdRange = wdDoc.Bookmarks("Concerns").Range
        Set wdTable = wdDoc.Tables.Add(wdRange, 1, 2)
        Do Until rsDetails.EOF
            If i = 1 Then
                Set wdRow = wdTable.Rows(1)
            Else
                Set wdRow = wdTable.Rows.Add
                wdRow.Cells(1).Range.Text = rsDetails!Concerns
                wdRow.Cells(2).Range.Text = rsDetails![Desired Outcomes]
            End If
            i = i + 1
            rsDetails.MoveNext
        Loop
    End If
End Sub

Open in new window