Solved

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

Posted on 2007-03-31
8
397 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
This article describes how to use the Send to Mail Recipient command. The instructions apply generally to Office 2007 and later versions, but Microsoft® Word 2013 was used for the specific steps and figures.  What is Send to Mail Recipient? Send…
This video walks the viewer through the process of creating an MLA formatted document, as well as a bibliography with citations.
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…

746 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

13 Experts available now in Live!

Get 1:1 Help Now