Solved

Apply a custom paragraph style to the first column of a selected table

Posted on 2007-03-31
8
401 Views
Last Modified: 2008-02-01
Below macro applies a custom paragraph style to the first row of a selected table. The macro is running fine.
I also tried to apply a custom paragraph style to the first column of a selected table using VBA, but to  no avail. How do I have to re-write below code so that the first column of the selected table gets the custom paragraph style?

Help is appreciated. Thank you very much in advance.  Regards, Andreas

Sub ApplyStyleFirstRow()
'apply a custom paragraph style to the first row of selected table
ActiveDocument.Tables(1).Select
Selection.SetRange _
        Start:=Selection.Rows(1).Range.Start, _
        End:=Selection.Rows(1).Range.End
 Selection.Style = "Dipl_Tbl_Kopf"

End Sub
0
Comment
Question by:AndreasHermle
  • 4
  • 3
8 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18828458
what about making that a one-liner:
Sub ApplyStyleFirstRow()
ActiveDocument.Tables(1).Range.Style = "Dipl_Tbl_Kopf"

End Sub
0
 

Author Comment

by:AndreasHermle
ID: 18829359
AngelIII:
Thank you. You led me on the  right track. I altered the code to fully suit my needs, i.e. only the first row should get the custom paragraph style and secondly it should be applied to any table that is currently selected, not just the first table. It is working. Great!


Sub ApplyStyleFirstRow()
Selection.Tables(1).Select
Selection.Rows(1).Range.Style = "Dipl_Tbl_Kopf"
End Sub
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18829559
glad I could help.
still, again avoid the selection object:

Sub ApplyStyleFirstRow()
  ActiveDocument.Tables(1).Rows(1).Range.Style = "Dipl_Tbl_Kopf"
End Sub
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

Author Comment

by:AndreasHermle
ID: 18831690
AngelIII:

Ok, but how I can I avoid the selection object if I would like to apply the formatting just to say, table no 3 of a given document and not to the others tables. I guess I have not made myself clear in the former messages.
Regards, Andreas
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18832717
>table no 3 of a given document
ActiveDocument.Tables(3).Rows(1).Range.Style = "Dipl_Tbl_Kopf"

to explain, why to avoid the selection object:
first, the selection is nothing else than a range object itself, with a simple "additional" property: it is the range that is currently selected.
for the macro, that property is not needed, and eventually dangerous: if the user has a chance to click around (especially with longer-running macros), it will make your macro fail.

0
 

Author Comment

by:AndreasHermle
ID: 18834567
AngelIII:
thank you for  your statements. Ok, I fully got what  you mean and on most of the  macros I already  wrote I avoided the range  object.
COMING back  to above example. Say the table that is to be worked on with above macro (apply custom style to table header) has not caption whatsoever, so the user or I do not know whether it is the first, or second or third etc. table in the document. I do not want to call up the visual basic  editor and change the number of the table in the code whenever I work on a different table.

Or is it possible to let Word do  the search for the table number and automatically input it as number in above code?

I hope I have made myself clear (English being not my mother tongue).
Regards, Andreas
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 18834606
so you want the table to be worked on the first one (or all) that are selected?

then, indeed the selection object IS needed, like this:

Sub ApplyStyleFirstRow()
  ActiveDocument.Selection.Tables(1).Rows(1).Range.Style = "Dipl_Tbl_Kopf"
End Sub
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
using web browser with BING 40 119
Converting large .PDF files. 6 56
Problem to With line 4 57
Replacing hyphens with non-breaking hyphens only in certain numbers 2 17
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

785 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