Solved

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

Posted on 2007-03-31
8
411 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Introduction This tutorial provides instructions on how to properly format your Word document using the inbuilt tools provided. The benefits of using these tools means your documents are more accessible and easily portable to other applications an…
A few years ago I was very much a beginner at VBA, and that very much remains the case today.  I'll do my best to explain things as I go in the hope that other beginners can follow.  If you just want to check out a tool that creates a Select Case fu…
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 Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …
Suggested Courses
Course of the Month9 days, 10 hours left to enroll

623 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