View Single Post
 
Old 10-25-2021, 12:22 AM
RobertDany RobertDany is offline Windows 7 64bit Office 2013
Novice
 
Join Date: Jul 2021
Posts: 22
RobertDany is on a distinguished road
Default Applying a VBA code only on a selected table

Hello,

I was made this macro to format a selected table

Code:
Sub FormatTable()
'
On Error GoTo ErrMsg
    Selection.Tables(1).Select
    With Selection.Tables(1)
        With .Borders(wdBorderLeft)
            .LineStyle = wdLineStyleThinThickSmallGap
            .LineWidth = wdLineWidth300pt
            .Color = wdColorAutomatic
        End With
        With .Borders(wdBorderRight)
            .LineStyle = wdLineStyleThinThickSmallGap
            .LineWidth = wdLineWidth300pt
            .Color = wdColorAutomatic
        End With
        With .Borders(wdBorderTop)
            .LineStyle = wdLineStyleThinThickSmallGap
            .LineWidth = wdLineWidth300pt
            .Color = wdColorAutomatic
        End With
        With .Borders(wdBorderBottom)
            .LineStyle = wdLineStyleThinThickSmallGap
            .LineWidth = wdLineWidth300pt
            .Color = wdColorAutomatic
        End With
        With .Borders(wdBorderHorizontal)
            .LineStyle = wdLineStyleSingle
            .LineWidth = wdLineWidth050pt
            .Color = wdColorAutomatic
        End With
        With .Borders(wdBorderVertical)
            .LineStyle = wdLineStyleSingle
            .LineWidth = wdLineWidth050pt
            .Color = wdColorAutomatic
        End With
        .Borders(wdBorderDiagonalDown).LineStyle = wdLineStyleNone
        .Borders(wdBorderDiagonalUp).LineStyle = wdLineStyleNone
        .Borders.Shadow = False
    End With
    With Options
        .DefaultBorderLineStyle = wdLineStyleSingle
        .DefaultBorderLineWidth = wdLineWidth050pt
        .DefaultBorderColor = wdColorAutomatic
    End With
    Selection.Font.Bold = wdToggle
    Selection.Font.BoldBi = wdToggle
    Selection.Font.Size = 14
    Selection.Font.Name = "Times New Roman"

ErrMsg: MsgBox "Select a Table Firstly", vbCritical

End Sub
The problem with this macro is, when a non-table is selected, the ErrMsg correctly appears, but when a table is selected, the macro format the selected table and also shows ErrMsg.

I used if conditions like,
if Activedocument.Selection.Tables = 1 then
my macro
else
error message
but it fails.

In summary, I want to apply a macro to a selected table only

Thanks a lot.
Reply With Quote