Solved

Macro to Make Table Borders Invisible Depending on Style Used - Inner Border Persists

Posted on 2008-10-29
14
414 Views
Last Modified: 2012-06-22
Hi

I'm having trouble getting rid of table borders, the following code was for the most part recorded. It looks for Style Heading 2 and if that style exists in the table (anywhere in the table) removes the borders. Only problems are:

1. Vertical borders are not hidden
2. I also want to perform the same thing for tables containing the style "TableHeading" where the 1st word in the 1st cell is "Environment".

I could write a separate macro, but would be nice to kill 2 birds with 1 stone.

Top Code seems to work to some degree, bottom code a dismal failure...

Please help

Kind regards

Steve
Sub A_FindStyleClearTable() 'Top code kinda works... 
 

Dim tTable As Table
 

For Each tTable In ActiveDocument.Tables
 

    Selection.Find.ClearFormatting

    Selection.Find.Style = ActiveDocument.Styles("Heading 2")

    With Selection.Find

        .Text = ""

        .Replacement.Text = ""

        .Forward = True

        .Wrap = wdFindContinue

        .Format = True

        .MatchCase = False

        .MatchWholeWord = False

        .MatchWildcards = False

        .MatchSoundsLike = False

        .MatchAllWordForms = False

    End With

    Selection.Find.Execute

    

    

    With Selection.Tables(1)

        .Borders(wdBorderLeft).LineStyle = wdLineStyleNone

        .Borders(wdBorderRight).LineStyle = wdLineStyleNone

        .Borders(wdBorderTop).LineStyle = wdLineStyleNone

        .Borders(wdBorderBottom).LineStyle = wdLineStyleNone

        .Borders(wdBorderHorizontal).LineStyle = wdLineStyleNone

        .Borders(wdBorderDiagonalDown).LineStyle = wdLineStyleNone

        .Borders(wdBorderDiagonalUp).LineStyle = wdLineStyleNone

        .Borders.Shadow = False

    End With

Next tTable

End Sub
 
 
 

Sub RemBorders()  ' Bottom code, a dismal failure
 

Dim tTable As Table

Dim rRange As Range

   

  

For Each tTable In ActiveDocument.Tables

Set rRange = bTable
 

            If Selection.Range.Find.Execute("Task Completed") Then

            If rRange.Find.Execute = ActiveDocument.Styles("Heading 2") Then

                MsgBox "H2"

                GoTo CleanUpTable

            Else

            If Selection.Find.Style = ActiveDocument.Styles("Table Sub Heading") Then

                        If rRange.Find.Execute("Responsibility:") Then

                            MsgBox "Res"

                            GoTo CleanUpTable

                                Else

                            If rRange.Find.Execute("Environmental:") Then

                                MsgBox "Env"

                                GoTo CleanUpTable

                                    Else

                                If rRange.Find.Execute("References:") Then

                                    MsgBox "Refs"

                                    GoTo CleanUpTable

                                        Else

                                    If rRange.Find.Execute("Tools and Equipment:") Then

                                        MsgBox "Tool"

                                        GoTo CleanUpTable
 

                                    End If

                                End If

                            End If

                        End If

                   End If

            End If

GoTo SkipThis

CleanUpTable:

            

    Selection.ParagraphFormat.Borders.Shadow = False
 

    With Selection.Tables(1)

        .Borders(wdBorderLeft).LineStyle = wdLineStyleNone

        .Borders(wdBorderRight).LineStyle = wdLineStyleNone

        .Borders(wdBorderTop).LineStyle = wdLineStyleNone

        .Borders(wdBorderBottom).LineStyle = wdLineStyleNone

        .Borders(wdBorderHorizontal).LineStyle = wdLineStyleNone

        .Borders(wdBorderDiagonalDown).LineStyle = wdLineStyleNone

        .Borders(wdBorderDiagonalUp).LineStyle = wdLineStyleNone

        .Borders.Shadow = False

    End With

    With Options

        .DefaultBorderLineStyle = wdLineStyleSingle

        .DefaultBorderLineWidth = wdLineWidth050pt

        .DefaultBorderColor = wdColorAutomatic

    End With
 

SkipThis:

        

    Next bTable
 

End Sub

Open in new window

0
Comment
Question by:DrTribos
  • 8
  • 5
14 Comments
 
LVL 17

Expert Comment

by:aflockhart
Comment Utility
To remove vertical lines you need :

  .Borders(wdBorderVertical).LineStyle = wdLineStyleNone


That's the easy bit ...
0
 
LVL 17

Expert Comment

by:aflockhart
Comment Utility
You also ( in the first macro) need an extra line at the start of the loop:

For Each tTable In ActiveDocument.Tables
 tTable.Select
   ....

Your code, at the moment, repeatedly checks the first table that it finds, because once you it has been through the loop once, there is a paragraph of text selected, and the selection.find command only searches within the currently selected area.

0
 
LVL 17

Expert Comment

by:aflockhart
Comment Utility
Try this for the second one.  It loops round the tables in the same way, but checks the first cell to see if it begins with the word "environment" before doing anything.
Sub B_FindStyleAndTExtClearTable()

Dim tTable As Table

 

For Each tTable In ActiveDocument.Tables

 tTable.Select

    Selection.Find.ClearFormatting

    Selection.Find.Style = ActiveDocument.Styles("TableHeading")

    With Selection.Find

        .Text = ""

        .Replacement.Text = ""

        .Forward = True

        .Wrap = wdFindContinue

        .Format = True

        .MatchCase = False

        .MatchWholeWord = False

        .MatchWildcards = False

        .MatchSoundsLike = False

        .MatchAllWordForms = False

    End With

    Selection.Find.Execute

    

    'we are in a table that contains a Heading Text cell.

    'does the first cell contain "Environment"
 

If InStr(1, Selection.Tables(1).Cell(1, 1), "environment", vbTextCompare) =1 Then
 

    

    

    With Selection.Tables(1)

        .Borders(wdBorderLeft).LineStyle = wdLineStyleNone

        .Borders(wdBorderRight).LineStyle = wdLineStyleNone

        .Borders(wdBorderTop).LineStyle = wdLineStyleNone

        .Borders(wdBorderBottom).LineStyle = wdLineStyleNone

        .Borders(wdBorderHorizontal).LineStyle = wdLineStyleNone

        .Borders(wdBorderVertical).LineStyle = wdLineStyleNone

        .Borders(wdBorderDiagonalDown).LineStyle = wdLineStyleNone

        .Borders(wdBorderDiagonalUp).LineStyle = wdLineStyleNone

        .Borders.Shadow = False

    End With

End If

    

Next tTable

End Sub

Open in new window

0
 
LVL 14

Author Comment

by:DrTribos
Comment Utility
Hi aflockhart

Thanks - I tried the mod to the 1st macro (insert tTable.Select) and now have run time errror #5941 member of collection does not exits - debug highlights line starting "With" per below.
_______________________________
End With
    Selection.Find.Execute
     
    With Selection.Tables(1)                                                                  <--Highlighted
        .Borders(wdBorderLeft).LineStyle = wdLineStyleNone
______________________________

0
 
LVL 14

Author Comment

by:DrTribos
Comment Utility
Regarding macro 2 - your change seems to work well, now the challenge (well I still find it challenging) is to search for the other words (cases?) in my original macro 2.

So, in addition to finding Environment, I want the macro to be expandable to find other words (i.e. Responsibility, References, Tools... etc.) this is what I was trying to achieve with the use of all those If statements... any suggestions welcome.

Kind regards

Steve
0
 
LVL 14

Author Comment

by:DrTribos
Comment Utility
I guess - really - I have a few ideas how to achieve the above (thanks to your input) but would like neat solution to deal with the other situations.... :-)
0
 
LVL 17

Expert Comment

by:aflockhart
Comment Utility
Error 5941  : Probably being caused by the macro finding an instance of the "Heading 2" style that is not contained in any table, so when it tries to find the containing table, it fails. We can just trap and ignore this error.

Revised version with some error checking.



Sub A_FindStyleClearTable() 'Top code kinda works...

 

Dim tTable As Table

 

For Each tTable In ActiveDocument.Tables

 tTable.Select

    Selection.Find.ClearFormatting

    Selection.Find.Style = ActiveDocument.Styles("Heading 2")

    With Selection.Find

        .Text = ""

        .Replacement.Text = ""

        .Forward = True

        .Wrap = wdFindContinue

        .Format = True

        .MatchCase = False

        .MatchWholeWord = False

        .MatchWildcards = False

        .MatchSoundsLike = False

        .MatchAllWordForms = False

    End With

    Selection.Find.Execute

    

    'we are now on a heading2 paragraph but may not be in the orioginal table

    'are we actually still in a table ? if not, we'll get an error whihc we can ignore, and skip to the next part

    

      On Error GoTo notInTable:

    

    

    With Selection.Tables(1)

        .Borders(wdBorderLeft).LineStyle = wdLineStyleNone

        .Borders(wdBorderRight).LineStyle = wdLineStyleNone

        .Borders(wdBorderTop).LineStyle = wdLineStyleNone

        .Borders(wdBorderBottom).LineStyle = wdLineStyleNone

        .Borders(wdBorderHorizontal).LineStyle = wdLineStyleNone

        .Borders(wdBorderVertical).LineStyle = wdLineStyleNone

        .Borders(wdBorderDiagonalDown).LineStyle = wdLineStyleNone

        .Borders(wdBorderDiagonalUp).LineStyle = wdLineStyleNone

        .Borders.Shadow = False

    End With

    

    

notInTable:

 'restore error checking

 On Error GoTo 0

    

Next tTable

End Sub

Open in new window

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 17

Accepted Solution

by:
aflockhart earned 500 total points
Comment Utility
You may need similar error checking in the second macro, although you are less likely to have the "TableHeader" style used outside a table.

On dealing with other words to search for:

You could hard code multiple words into the macro with something like:

If InStr(1, Selection.Tables(1).Cell(1, 1), "environment", vbTextCompare) =1  OR InStr(1, Selection.Tables(1).Cell(1, 1), "responsibility", vbTextCompare) =1 OR ...    Then ...


or ask for input at the start of the macro each time you run it , and look for whatever the user types:

WordToSearchFor = InputBox("ENter word to search for")
...
...
If InStr(1, Selection.Tables(1).Cell(1, 1), WordToSearchFor, vbTextCompare) = 1 Then ...


 
0
 
LVL 14

Author Comment

by:DrTribos
Comment Utility
Hi - I think you're right about the 'Heading 2' - I substituted Heading 2 into the macro that worked with TableHeading and it fell over - will try the error trapping. Thanks (I was going stir crazy)
0
 
LVL 14

Author Comment

by:DrTribos
Comment Utility
I like the 'OR' option - did not know I could do that...  
0
 
LVL 14

Author Comment

by:DrTribos
Comment Utility
Seems to have a problem here too if I try to replace TableHeading with Heading 2

If InStr(1, Selection.Tables(1).Cell(1, 1), "refer", vbTextCompare) = 1 Then

Not sure if it is because Heading 2 has numbering... get message Requested Member of the Collection does not exist.

My battery is about to die (both the PC and the one keeping me awake) will sleep on it and try more in the morn - thanks heaps for your help.

Cheers

Steve
0
 
LVL 9

Expert Comment

by:jkunrein
Comment Utility
The WithinTable property is also helpful for conditions.  You can avert errors by having an If statement using this snippet as a model.

Incidentally, unless I miss my guess, the second macro may have failed also in part because I see bTable referenced but not defined.  Was that supposed to be tTable?  Also, when you define variables, keep the type in mind.  If tTable is a table, then you cannot set rRange to that.  But you can set rRange equal to tTable.Range.  

Just some additional thoughts.  Good luck.
Public Sub Test()

    Dim rRange As Range

    Set rRange = ActiveDocument.Paragraphs(1).Range

    

    If rRange.Information(wdWithInTable) = True Then

        MsgBox "In table"

    Else

        MsgBox "Not in table"

    End If
 

End Sub

Open in new window

0
 
LVL 14

Author Closing Comment

by:DrTribos
Comment Utility
Thanks - got it all working now, although did not end up looking for the words, only the style.

Cheers

Steve
0
 
LVL 14

Author Comment

by:DrTribos
Comment Utility
Thanks aflockhart - works :-)

Hi jkunrein Thanks for your input, I have another question if you can help...  hope the link works

http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Word/Q_23839091.html

cheers

Steve
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

The System Center Operations Manager 2012, known as SCOM, is a part of the Microsoft system center product that provides the user with infrastructure monitoring and application performance monitoring. SCOM monitors:   Windows or UNIX/LinuxNetwo…
Article by: Leon
Software Metering within our group of companies has always been an afterthought until auditing of software and licensing became a pain point. Orchestrator and SCCM metering gave us the answer and it was an exciting process.
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now