Reply
 
Thread Tools Display Modes
  #1   Report Post  
Posted to microsoft.public.word.tables
Tom Tom is offline
external usenet poster
 
Posts: 61
Default identifying a specific table in a macro?

I have two types of tables in my document -- one for reference
information, and one for notes, cautions, etc. The former tables have
borders, but the latter do not.

I currently have a macro that formats all tables with a specific
format.

How would I make it so that the macro only formats one type of tables
in my document? In other words, so that it formats reference tables
with borders, and note tables without borders?

Here's the macro (actually from Peter Grainge's site) that I'm using to
format all tables:

Sub FormatTablesAll()
' Runs through all tables in the document and applies the properties
defined.
' Does not give user option to skip a table.
' This starts the macro at the start of the document
Selection.HomeKey Unit:=wdStory
'This declares the variable that counts the number of tables.
Dim iNumber As Integer
iNumber = ActiveDocument.Tables.Count
' This stops the macro running if there are no tables
If iNumber = 0 Then
MsgBox "There are no tables in this document"
End
Else
'This declares the variable that numbers the current table.
Dim iCurrent As Integer
For iCurrent = 1 To iNumber
' This finds the next table
Selection.GoTo what:=wdGoToTable, which:=wdGoToNext, Count:=1, Name:=""
' This selects the table
With Selection
.SelectColumn
.SelectRow
End With
' This formats the table
With Selection.Tables(1)
With .Borders(wdBorderLeft)
.LineStyle = wdLineStyleSingle
.Color = RGB(0, 0, 0)
End With
With .Borders(wdBorderRight)
.LineStyle = wdLineStyleSingle
.Color = RGB(0, 0, 0)
End With
With .Borders(wdBorderTop)
.LineStyle = wdLineStyleSingle
.Color = RGB(0, 0, 0)
End With
With .Borders(wdBorderBottom)
.LineStyle = wdLineStyleSingle
.Color = RGB(0, 0, 0)
End With
With .Borders(wdBorderVertical)
.LineStyle = wdLineStyleSingle
.Color = RGB(0, 0, 0)
End With
With .Borders(wdBorderHorizontal)
.LineStyle = wdLineStyleSingle
.Color = RGB(0, 0, 0)
End With
' Enter other requirements here
End With
Selection.Collapse
' This loops to the next table until all the tables have been
processed.
Next iCurrent
End If
MsgBox "All tables have been formatted."
End Sub

Thanks for your help.

  #2   Report Post  
Posted to microsoft.public.word.tables
Jay Freedman Jay Freedman is offline
external usenet poster
 
Posts: 9,854
Default identifying a specific table in a macro?

Before anyone can propose an answer to your question, you have to explain
how the macro can distinguish one type of table from the other. Is there any
text, style, or formatting that the program can look at to say "this table
is a reference table" or "this is not a reference table"? Unless that
decision can be made automatically, you'll be forced to make the decision
manually.

Once that hurdle is passed, the macro you have is unnecessarily complicated.
Instead of counting the tables and selecting each one in turn to operate on
it, the macro should use a For Each loop and modify the appropriate members
of the ActiveDocument.Tables collection directly:

Dim oTbl As Table
For Each oTbl In ActiveDocument.Tables
If oTbl is a reference table Then == whatever this test is!
With oTbl
With .Borders(wdBorderLeft)
.LineStyle = wdLineStyleSingle
.Color = RGB(0, 0, 0)
End With
' etc. ...
End With
End If
Next

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.

Tom wrote:
I have two types of tables in my document -- one for reference
information, and one for notes, cautions, etc. The former tables have
borders, but the latter do not.

I currently have a macro that formats all tables with a specific
format.

How would I make it so that the macro only formats one type of tables
in my document? In other words, so that it formats reference tables
with borders, and note tables without borders?

Here's the macro (actually from Peter Grainge's site) that I'm using
to format all tables:

Sub FormatTablesAll()
' Runs through all tables in the document and applies the properties
defined.
' Does not give user option to skip a table.
' This starts the macro at the start of the document
Selection.HomeKey Unit:=wdStory
'This declares the variable that counts the number of tables.
Dim iNumber As Integer
iNumber = ActiveDocument.Tables.Count
' This stops the macro running if there are no tables
If iNumber = 0 Then
MsgBox "There are no tables in this document"
End
Else
'This declares the variable that numbers the current table.
Dim iCurrent As Integer
For iCurrent = 1 To iNumber
' This finds the next table
Selection.GoTo what:=wdGoToTable, which:=wdGoToNext, Count:=1,
Name:="" ' This selects the table
With Selection
.SelectColumn
.SelectRow
End With
' This formats the table
With Selection.Tables(1)
With .Borders(wdBorderLeft)
.LineStyle = wdLineStyleSingle
.Color = RGB(0, 0, 0)
End With
With .Borders(wdBorderRight)
.LineStyle = wdLineStyleSingle
.Color = RGB(0, 0, 0)
End With
With .Borders(wdBorderTop)
.LineStyle = wdLineStyleSingle
.Color = RGB(0, 0, 0)
End With
With .Borders(wdBorderBottom)
.LineStyle = wdLineStyleSingle
.Color = RGB(0, 0, 0)
End With
With .Borders(wdBorderVertical)
.LineStyle = wdLineStyleSingle
.Color = RGB(0, 0, 0)
End With
With .Borders(wdBorderHorizontal)
.LineStyle = wdLineStyleSingle
.Color = RGB(0, 0, 0)
End With
' Enter other requirements here
End With
Selection.Collapse
' This loops to the next table until all the tables have been
processed.
Next iCurrent
End If
MsgBox "All tables have been formatted."
End Sub

Thanks for your help.



  #3   Report Post  
Posted to microsoft.public.word.tables
Tom Tom is offline
external usenet poster
 
Posts: 61
Default identifying a specific table in a macro?

Thanks Jay. In my situation, I receive the tables without any
particular table style (it's a generated output from RoboHelp). I was
thinking that I might be able to apply a top and bottom border to the
entire table, and also a border to the bottom of the first row. That
formatting might actually work for both my tables.

I was looking in VBA's help to figure this out, but haven't quite
gotten it. Is the following close?

ActiveDocument.Tables(1).Rows (1)
.Borders (wdBorderBottom)
.LineStyle = wdLineStyleSingle
.Color = RGB(0, 0, 0)

I am trying to format a table like the official microsoft table style
-- kind of like this:


---------------------------------------------------------------------------------------------------
Top left cell Top right cell
---------------------------------------------------------------------------------------------------
second row
third row
fourth row
fifth row
sixth row
last row
----------------------------------------------------------------------------------------------------

Any ideas?

  #4   Report Post  
Posted to microsoft.public.word.tables
Jay Freedman Jay Freedman is offline
external usenet poster
 
Posts: 9,854
Default identifying a specific table in a macro?

As I tried to say before, any discussion of how to format a table is
premature until you know _which tables_ you need to format.
Concentrate on figuring out how the macro can distinguish between the
two kinds of table. That will probably be the hard part of the work;
after that, the formatting itself is just a few lines.

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.

On 18 Dec 2006 13:20:28 -0800, "Tom" wrote:

Thanks Jay. In my situation, I receive the tables without any
particular table style (it's a generated output from RoboHelp). I was
thinking that I might be able to apply a top and bottom border to the
entire table, and also a border to the bottom of the first row. That
formatting might actually work for both my tables.

I was looking in VBA's help to figure this out, but haven't quite
gotten it. Is the following close?

ActiveDocument.Tables(1).Rows (1)
.Borders (wdBorderBottom)
.LineStyle = wdLineStyleSingle
.Color = RGB(0, 0, 0)

I am trying to format a table like the official microsoft table style
-- kind of like this:


---------------------------------------------------------------------------------------------------
Top left cell Top right cell
---------------------------------------------------------------------------------------------------
second row
third row
fourth row
fifth row
sixth row
last row
----------------------------------------------------------------------------------------------------

Any ideas?

  #5   Report Post  
Posted to microsoft.public.word.tables
Tom Tom is offline
external usenet poster
 
Posts: 61
Default identifying a specific table in a macro?

Unfortunately I do not think it would be possible to distinguish
between the two tables. The only way I can distinguish them in RoboHelp
is by setting a style for the text *inside* the table, but not the
table itself. Is it possible for a macro to find all instances of
tables containing text with style X and format those tables a certain
way?

If not, I could just use the following macro (again, from Peter
Grainge's site) and apply the style to each selected table on a yes or
no basis. I would essentially have two table macros like the code that
follows, each with differing formats.

Sub FormatReferenceTables()

' Runs through all tables from a user defined start point asking user
to indicate whether formatting is to be applied.
' This determines the start point
Response = MsgBox("Start here? Select No to start at beginning of
document.", vbYesNoCancel)
If Response = vbYes Then
Selection.GoTo what:=wdGoToTable, which:=wdGoToNext, Count:=1, Name:=""
' This gives warning message if there are no tables beyond start point.
If Selection.Information(wdWithInTable) = True Then
Else
MsgBox "There are no tables after this position"
End
End If
ElseIf Response = vbNo Then
Selection.HomeKey Unit:=wdStory
Selection.GoTo what:=wdGoToTable, which:=wdGoToNext, Count:=1, Name:=""
' This gives warning message if there are no tables in the document.
If Selection.Information(wdWithInTable) = True Then
Else
MsgBox "There are no tables in this document"
End
End If
Else
End
End If
' This finds the next table from start point and starts the formatting
Do
Response = MsgBox("Apply changes to this table?", vbYesNoCancel)
If Response = vbYes Then
' This selects the table
With Selection
..SelectColumn
..SelectRow
End With
' This formats the table
With Selection.Tables(1)
With .Borders(wdBorderLeft)
.LineStyle = wdLineStyleSingle
.Color = RGB(50, 100, 155)
End With
With .Borders(wdBorderRight)
.LineStyle = wdLineStyleSingle
.Color = RGB(50, 100, 155)
End With
With .Borders(wdBorderTop)
.LineStyle = wdLineStyleSingle
.Color = RGB(50, 100, 155)
End With
With .Borders(wdBorderBottom)
.LineStyle = wdLineStyleSingle
.Color = RGB(50, 100, 155)
End With
With .Borders(wdBorderVertical)
.LineStyle = wdLineStyleSingle
.Color = RGB(50, 100, 155)
End With
With .Borders(wdBorderHorizontal)
.LineStyle = wdLineStyleSingle
.Color = RGB(50, 100, 155)
End With
' Enter other requirements here
End With
' This collapses the selection and moves to the next table or displays
the end of tables message
Selection.Collapse wdCollapseEnd
Selection.GoTo what:=wdGoToTable, which:=wdGoToNext, Count:=1, Name:=""
If Selection.Information(wdWithInTable) = True Then
Else
MsgBox "End of tables in this document"
End
End If
ElseIf Response = vbNo Then
' Selects Table and then collapses to point beyond the table
With Selection
..SelectColumn
..SelectRow
End With
Selection.Collapse wdCollapseEnd
' Looks for next table and loops if found or ends macro
Selection.GoTo what:=wdGoToTable, which:=wdGoToNext, Count:=1, Name:=""
If Selection.Information(wdWithInTable) = True Then
Else
MsgBox "End of tables in this document"
End
End If
Else
End
End If
Loop
MsgBox "End of tables in this document"
End Sub



My question is, with the formatting, how would I make it so the first
row shows a border on the bottom?



  #6   Report Post  
Posted to microsoft.public.word.tables
Jay Freedman Jay Freedman is offline
external usenet poster
 
Posts: 9,854
Default identifying a specific table in a macro?

Hi Tom,

Yes, a macro can check the style of the table's text to distinguish
reference vs. non-reference tables. It would go something like this
(checking only the style of the text in the top left cell, in case there are
other styles in the table's other cells):

Sub ReferenceTableBorders()
Dim oTbl As Table
For Each oTbl In ActiveDocument.Tables
If oTbl.Cell(1, 1).Range.Style = _
ActiveDocument.Styles("Reference") Then
With oTbl
' do your formatting here, using oTbl
' instead of Selection.Tables(1)
End With
End If
Next oTbl
End Sub

To answer your last question, the .Borders(wdBorderHorizontal) part of the
macro already draws a line at the bottom of the first row and every other
row. If you want the line at the bottom of the first row to be different,
you need a similar group of statements that starts with the line With
..Rows(1).Borders(wdBorderBottom) -- notice how the .Rows(1) limits the
effect of the operation to only the first row instead of the entire set of
rows.

Since you're formatting all the borders of the table the same, except for
the bottom of Row 1, you can simplify the formatting code by using
..Borders.OutsideLineStyle and .Borders.OutsideLineStyle (and the equivalent
pair for the inside borders) instead of individually setting six different
sets of borders. This example makes the Row 1 border thicker than the others
(the default line width is 0.5 pt, equivalent to the constant
wdLineWidth050pt).

With oTbl
With .Borders
.OutsideLineStyle = wdLineStyleSingle
.OutsideColor = RGB(50, 100, 155)
.InsideLineStyle = wdLineStyleSingle
.InsideColor = RGB(50, 100, 155)
End With
With .Rows(1).Borders(wdBorderBottom)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth150pt
.Color = RGB(50, 100, 155)
End With
End With

You could make the Row(1) border a different color, or a different line
style (such as wdLineStyleDouble).

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.

Tom wrote:
Unfortunately I do not think it would be possible to distinguish
between the two tables. The only way I can distinguish them in
RoboHelp is by setting a style for the text *inside* the table, but
not the table itself. Is it possible for a macro to find all
instances of tables containing text with style X and format those
tables a certain way?

If not, I could just use the following macro (again, from Peter
Grainge's site) and apply the style to each selected table on a yes or
no basis. I would essentially have two table macros like the code that
follows, each with differing formats.

Sub FormatReferenceTables()

' Runs through all tables from a user defined start point asking user
to indicate whether formatting is to be applied.
' This determines the start point
Response = MsgBox("Start here? Select No to start at beginning of
document.", vbYesNoCancel)
If Response = vbYes Then
Selection.GoTo what:=wdGoToTable, which:=wdGoToNext, Count:=1,
Name:="" ' This gives warning message if there are no tables beyond
start point. If Selection.Information(wdWithInTable) = True Then
Else
MsgBox "There are no tables after this position"
End
End If
ElseIf Response = vbNo Then
Selection.HomeKey Unit:=wdStory
Selection.GoTo what:=wdGoToTable, which:=wdGoToNext, Count:=1,
Name:="" ' This gives warning message if there are no tables in the
document. If Selection.Information(wdWithInTable) = True Then
Else
MsgBox "There are no tables in this document"
End
End If
Else
End
End If
' This finds the next table from start point and starts the formatting
Do
Response = MsgBox("Apply changes to this table?", vbYesNoCancel)
If Response = vbYes Then
' This selects the table
With Selection
.SelectColumn
.SelectRow
End With
' This formats the table
With Selection.Tables(1)
With .Borders(wdBorderLeft)
.LineStyle = wdLineStyleSingle
.Color = RGB(50, 100, 155)
End With
With .Borders(wdBorderRight)
.LineStyle = wdLineStyleSingle
.Color = RGB(50, 100, 155)
End With
With .Borders(wdBorderTop)
.LineStyle = wdLineStyleSingle
.Color = RGB(50, 100, 155)
End With
With .Borders(wdBorderBottom)
.LineStyle = wdLineStyleSingle
.Color = RGB(50, 100, 155)
End With
With .Borders(wdBorderVertical)
.LineStyle = wdLineStyleSingle
.Color = RGB(50, 100, 155)
End With
With .Borders(wdBorderHorizontal)
.LineStyle = wdLineStyleSingle
.Color = RGB(50, 100, 155)
End With
' Enter other requirements here
End With
' This collapses the selection and moves to the next table or displays
the end of tables message
Selection.Collapse wdCollapseEnd
Selection.GoTo what:=wdGoToTable, which:=wdGoToNext, Count:=1,
Name:="" If Selection.Information(wdWithInTable) = True Then
Else
MsgBox "End of tables in this document"
End
End If
ElseIf Response = vbNo Then
' Selects Table and then collapses to point beyond the table
With Selection
.SelectColumn
.SelectRow
End With
Selection.Collapse wdCollapseEnd
' Looks for next table and loops if found or ends macro
Selection.GoTo what:=wdGoToTable, which:=wdGoToNext, Count:=1,
Name:="" If Selection.Information(wdWithInTable) = True Then
Else
MsgBox "End of tables in this document"
End
End If
Else
End
End If
Loop
MsgBox "End of tables in this document"
End Sub



My question is, with the formatting, how would I make it so the first
row shows a border on the bottom?



  #7   Report Post  
Posted to microsoft.public.word.tables
Tom Tom is offline
external usenet poster
 
Posts: 61
Default identifying a specific table in a macro?

Jay,

Wow, you can really do a lot of incredible things with VB. I had no
idea that was possible. This has helped tremendously. I was able to
input it and it really works. Thank you!

Reply
Thread Tools
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Change Table Background Macro Jeff Ditty Tables 1 March 28th 06 04:32 AM
Macros:How to select a specific column of a table and format it using macro? evg999 Tables 3 January 31st 06 11:20 AM
Select specific cells in table via macro Bill Sturdevant Microsoft Word Help 1 July 27th 05 03:01 PM
How do I create a macro that is a table in word InlovewithJJ Tables 1 April 29th 05 12:42 AM
Table headers/footers and layout Keith Page Layout 1 April 8th 05 07:37 PM


All times are GMT +1. The time now is 01:51 PM.

Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 Microsoft Office Word Forum - WordBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Word"