PDA

View Full Version : Hiding a linked Excel chart in Microsoft Word with VBA



varunc777999
04-05-2017, 08:24 AM
I'm trying to write a VBA script in Microsoft Word 2010 that will let me show / hide tables.
Basically I want to have a button on the header of the table saying "Hide", which will collapse the table and everything within the table when pressed.
The code I've written has been working great, except for tables that contain a chart linked to Excel. In these cases, it doesn't hide the linked chart when the table is collapsed.

Regular charts or images hide with no issue, it's just the linked Excel tables that are problematic. How should I change this code so that it will hide linked tables?


Sub ToggleCommand(btn As MSForms.ToggleButton, Optional TableNumber As Integer)

Application.ScreenUpdating = False

Dim objShape As Word.InlineShape

Dim i As Integer

Dim NumberOfRows As Integer

NumberOfRows = ActiveDocument.Tables(TableNumber).Rows.Count



For i = 2 To NumberOfRows

With ActiveDocument.Tables(TableNumber).Rows(i)

If .HeightRule = wdRowHeightExactly Then
.HeightRule = wdRowHeightAuto
btn.Caption = "Hide"
.Borders(wdBorderBottom).LineStyle = wdLineStyleSingle
.Borders(wdBorderLeft).LineStyle = wdLineStyleSingle
.Borders(wdBorderRight).LineStyle = wdLineStyleSingle
For Each objShape In ActiveDocument.Tables(TableNumber).Range.InlineShapes
objShape.Range.Borders(wdBorderBottom).LineStyle = wdLineStyleSingle
objShape.Range.Borders(wdBorderLeft).LineStyle = wdLineStyleSingle
objShape.Range.Borders(wdBorderRight).LineStyle = wdLineStyleSingle
Next

Else
btn.Caption = "Show"
.HeightRule = wdRowHeightExactly
.Height = ".5"
.Borders(wdBorderBottom).LineStyle = wdLineStyleNone
.Borders(wdBorderLeft).LineStyle = wdLineStyleNone
.Borders(wdBorderRight).LineStyle = wdLineStyleNone
For Each objShape In ActiveDocument.Tables(TableNumber).Range.InlineShapes
objShape.Range.Borders(wdBorderBottom).LineStyle = wdLineStyleNone
objShape.Range.Borders(wdBorderLeft).LineStyle = wdLineStyleNone
objShape.Range.Borders(wdBorderRight).LineStyle = wdLineStyleNone
Next

End If

End With

Next i


lbl_Exit:

Exit Sub
End Sub