PDA

View Full Version : [SLEEPER:] VBA code to format tables and text



rasika99
12-06-2014, 08:40 PM
Guys,

I have a word document containing number of tables and paragraphs. I want to format it as follows,

Table headers: font size = 12; type = "Arial";row height = 0.18''; cell margins from bottom = 0.05''

Table other rows: font size = 10; type = "Arial";row height = 0.15''; cell margins from bottom = 0.00''

Paragraphs of the document: font size = 11;; type = "calibri";

(an example doc is attached)
12589
can i do this using a vba code?

gmayor
12-06-2014, 11:01 PM
You could, but you should start by creating the styles you want for your table. Your example uses only the normal style (with manual formatting, which is not good practice), but as your normal style already has Calibri 11 point font, select the whole document and press CTRL+Q and CTRL+Space which will remove the manual formatting. Create two styles for the table - Table Header and Table Body and use the following macro to apply it to all your tables.

You should then create or modify the styles suitable for your headings etc and apply those styles as required. Avoid manual formatting as it makes the document difficult to change. With styles you simply have to modify the styles to change the appearance of the whole document.


Sub FormatTables()
Dim oTable As Table
For Each oTable In ActiveDocument.Tables
oTable.Range.Style = "Table Body"
oTable.Rows(1).Range.Style = "Table Header"
oTable.Rows(1).Shading.BackgroundPatternColor = RGB(191, 191, 191)
Next oTable
End Sub

rasika99
12-08-2014, 10:53 AM
Thanks Graham. By the way, can we create styles by writing vba codes?

gmaxey
12-08-2014, 01:47 PM
I'm not Graham, but sure:


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oStyle As Style
Set oStyle = ActiveDocument.Styles.Add("New Style", 1)
With oStyle
.ParagraphFormat.LineSpacing = 2
'etc.
End With
End Sub

macropod
12-09-2014, 09:27 PM
can we create styles by writing vba codes?
Better to create the Styles and add them to the appropriate template. That way, you don't need any code to re-create them each time you create a new document - simply use them as you would any other Style.

rasika99
12-11-2014, 07:05 PM
Thanks Greg and Paul.

I tried to create a style and format the document. However I could not do it successfully since I'm new to this Styles and VBA. Simply what i want is,

font size of document =11. Type =Arial

In tables, font size=10; type= calibri ; row height= 0.15 inches ("at least", not "exactly").; alignment= "left".

In header rows, font should be "bold &underlined". Bottom padding= 0.05inches; Shading= 12.5%.

Can u guys give me a vba code for this?

I have to do this daily for huge number of documents and it was really a hard work.

I really greatful if u guys can help me.
Thanks.

macropod
12-11-2014, 07:27 PM
As I have already said, if you define a Style and add it to the document template, you don't need any code for it. If you really want to create a Style with code every time you create a document, though, you could record a macro to do so with the macro recorder.

macropod
12-12-2014, 12:29 AM
Cross-posted at: http://www.msofficeforums.com/word-vba/23937-vba-code-format-document-containing-tables.html
For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184