Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-25-2021, 12:22 AM
RobertDany RobertDany is offline Applying a VBA code only on a selected table Windows 7 64bit Applying a VBA code only on a selected table Office 2013
Novice
Applying a VBA code only on a selected table
 
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
  #2  
Old 10-25-2021, 01:32 AM
gmayor's Avatar
gmayor gmayor is offline Applying a VBA code only on a selected table Windows 10 Applying a VBA code only on a selected table Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

You need to exit the sub before the error message, or forget the error message and make sure it doesn't appear e.g.
Code:
Sub FormatTable()
    If Selection.Information(wdWithInTable) = True Then
        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 = True
        Selection.Font.BoldBi = True
        Selection.Font.Size = 14
        Selection.Font.Name = "Times New Roman"
    Else
        MsgBox "Select a Table First", vbCritical
    End If
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 10-25-2021, 02:59 AM
RobertDany RobertDany is offline Applying a VBA code only on a selected table Windows 7 64bit Applying a VBA code only on a selected table Office 2013
Novice
Applying a VBA code only on a selected table
 
Join Date: Jul 2021
Posts: 22
RobertDany is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
You need to exit the sub before the error message.
Thank you very much

Just to learn and according to your instructions, the code will be

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"
Exit Sub
ErrMsg: MsgBox "Select a Table Firstly", vbCritical

End Sub
Reply With Quote
  #4  
Old 10-25-2021, 05:09 AM
gmayor's Avatar
gmayor gmayor is offline Applying a VBA code only on a selected table Windows 10 Applying a VBA code only on a selected table Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

That should work, but it is better that the error doesn't occur in the first place.
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #5  
Old 10-25-2021, 02:43 PM
Guessed's Avatar
Guessed Guessed is offline Applying a VBA code only on a selected table Windows 10 Applying a VBA code only on a selected table Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

I would write it so it can do zero to n selected tables without needing to handle errors
Code:
Dim aTbl as Table
For each aTbl in Selection.Range.Tables
  With aTbl.Borders(wdBorderLeft)
    ....
  End With
Next aTbl
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #6  
Old 10-25-2021, 09:28 PM
RobertDany RobertDany is offline Applying a VBA code only on a selected table Windows 7 64bit Applying a VBA code only on a selected table Office 2013
Novice
Applying a VBA code only on a selected table
 
Join Date: Jul 2021
Posts: 22
RobertDany is on a distinguished road
Default

Graham and Andrew
Thanks with my love
Reply With Quote
Reply

Tags
word table, word vba, word vba code

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Applying a VBA code only on a selected text or range RobertDany Word VBA 2 10-09-2021 08:31 AM
Run code if selected cell is in column Javi78503 Word VBA 0 09-21-2021 12:31 PM
Applying a VBA code only on a selected table Applying a style to a table row pops out the row mazloum91 Word Tables 2 06-29-2021 07:21 AM
Applying a VBA code only on a selected table run code only when certain cells selected trevorc Excel 2 06-19-2018 02:40 AM
Applying a VBA code only on a selected table After applying a title style, the row in a table doesn't break Salix Word 6 05-05-2016 12:48 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:56 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft