Solved

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

Posted on 2007-03-31
8
400 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.

911 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

25 Experts available now in Live!

Get 1:1 Help Now