Microsoft Office Forums

Go Back   Microsoft Office Forums > Microsoft Word > Word VBA

Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 01-29-2012, 12:29 AM
Jennifer Murphy Jennifer Murphy is offline Windows XP Office 2007
Competent Performer
 
Join Date: Aug 2011
Posts: 178
Jennifer Murphy is on a distinguished road
Default Please tell me it doesn't take 7 statements to turn table borders off


I want to add code to a macro to turn off all but the bottom border in the header row of a table. I started the macro recorder, selected the first row, turned off all borders, then turned the bottom border back on. Here's the code I got:

Code:
Sub Macro1()
  Selection.Borders(wdBorderTop).LineStyle = wdLineStyleNone
  Selection.Borders(wdBorderLeft).LineStyle = wdLineStyleNone
  Selection.Borders(wdBorderBottom).LineStyle = wdLineStyleNone
  Selection.Borders(wdBorderRight).LineStyle = wdLineStyleNone
  Selection.Borders(wdBorderVertical).LineStyle = wdLineStyleNone
  Selection.Borders(wdBorderDiagonalDown).LineStyle = wdLineStyleNone
  Selection.Borders(wdBorderDiagonalUp).LineStyle = wdLineStyleNone
  With Selection.Borders(wdBorderBottom)
    .LineStyle = Options.DefaultBorderLineStyle
    .LineWidth = Options.DefaultBorderLineWidth
    .Color = Options.DefaultBorderColor
  End With
End Sub
Is there really no wdBorderAll variable or something equivalent?
Reply With Quote
  #2  
Old 01-29-2012, 04:09 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 16,296
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

Hi Jennifer,

You could use a loop:
Code:
Sub Demo()
Dim i As Long
With Selection.Tables(1)
  With .Rows(1).Range
    For i = 1 To .Borders.Count
      .Borders(i).LineStyle = wdLineStyleNone
    Next
    .Borders(wdBorderBottom).LineStyle = Options.DefaultBorderLineStyle
  End With
End With
End Sub
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #3  
Old 01-29-2012, 05:41 PM
Jennifer Murphy Jennifer Murphy is offline Windows XP Office 2007
Competent Performer
 
Join Date: Aug 2011
Posts: 178
Jennifer Murphy is on a distinguished road
Default

That's pretty clever. I never would have thought of that.

So I take it there is no wdBordersAll or something like that, right?
Reply With Quote
  #4  
Old 01-29-2012, 05:56 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 16,296
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

Quote:
Originally Posted by Jennifer Murphy View Post
So I take it there is no wdBordersAll or something like that, right?
You could use something like:
ActiveDocument.Tables(1).Rows(1).Borders.Enable = False
but that would remove the bottom border from the row as well. You might remedy that with code like:
Code:
With ActiveDocument.Tables(1)
  .Rows(1).Borders.Enable = False
  .Rows(2).Borders.Enable = True
End With
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #5  
Old 07-25-2012, 08:13 AM
sydneytroz sydneytroz is offline Windows 7 64bit Office 2012
Novice
 
Join Date: Jul 2012
Posts: 1
sydneytroz is on a distinguished road
Default

Quote:
Originally Posted by Jennifer Murphy View Post
So I take it there is no wdBordersAll or something like that, right?
Since the border styles are stored in an array or some sort of indexed list, it looks like the other wd* variables are just indices, so a wdBordersAll wouldn't make sense.

If VBA was kept up with the .NET languages, you could do something like (I'm not familiar with VB .NET, so this is borrowing from the C# syntax)
Code:
Selection.Borders.ForEach(x => x.LineStyle = wdLineStyleNone)
...but that will probably never happen.
Reply With Quote
  #6  
Old 07-25-2012, 03:12 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 16,296
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

Yes, one could loop through the collection, but that still requires turning on/off seven separate border attributes.
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
Reply
Please reply to this thread with any new information or opinions.

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
showing results of If statements in a mail merge m&co Mail Merge 3 01-06-2012 07:27 PM
Word VBA: add borders to active table tinfanide Word VBA 2 12-02-2011 05:53 AM
Insert table button doesn't work in Outlook 2010 e-mail message ironwoods Outlook 0 08-02-2010 12:24 PM
Converting document to PDF but table of contents doesn't work luthers Word Tables 0 07-30-2009 03:29 PM
IF statements that shifts to right. kent Excel 0 01-19-2006 02:23 PM


All times are GMT -7. The time now is 03:22 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
MSOfficeForums.com is not affiliated with Microsoft