Solved

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

Posted on 2007-03-31
8
403 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 143

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 143

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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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 143

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 143

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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.
This video shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…

792 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