Table Styles and Automation

S

scorpion53061

I cannot figure out the equation of how this would work to automate MS Word
table style. This is the vba converted but it says that style is not a
member any ideas anyone?

With oDoc.Content.Style'Selection.Tables(1)
If .Style <> "Table Grid" Then
.Style = "Table Grid"
End If
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = True
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = True
End With
 
K

Klaus Linke

Hi scorpion,

"With oDoc.Content.Style" was something you tried in despair?
If you return to "With Selection.Tables(1)", you'll get the error you
mention if the selection isn't in a table.

In case you want to apply the formatting to all tables, loop them:

Dim myTable As Table
For Each myTable In ActiveDocument.Tables
With myTable
.Style = ActiveDocument.Styles("Tabellengitternetz")
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = True
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = True
End With
Next myTable

Greetings,
Klaus
 
S

scorpion53061

Thanks Klaus I will try this straight away.


Klaus Linke said:
Hi scorpion,

"With oDoc.Content.Style" was something you tried in despair?
If you return to "With Selection.Tables(1)", you'll get the error you
mention if the selection isn't in a table.

In case you want to apply the formatting to all tables, loop them:

Dim myTable As Table
For Each myTable In ActiveDocument.Tables
With myTable
.Style = ActiveDocument.Styles("Tabellengitternetz")
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = True
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = True
End With
Next myTable

Greetings,
Klaus
 
S

scorpion53061

This code does to want to produce a style.

oDoc.Content.Application.Selection.Style =
oDoc.Application.ActiveDocument.Styles.Item("Company Name")

What do you suppose is wrong with it? It says company style is not a
member....
 
K

Klaus Linke

This code does to want to produce a style.
oDoc.Content.Application.Selection.Style =
oDoc.Application.ActiveDocument.Styles.Item("Company Name")

What do you suppose is wrong with it? It says company style is
not a member....


Hi scorpion,

Looks a bit cumbersome (at least "Content" is redundant... not sure about
the rest because I never automate Word from somewhere else).
But it should apply the style "Company Name" to the current selection.
Was that the plan?

The error message might mean that there is no style with that name in the
active document.

If with "produce a style" you mean you want to create a style, look at the
help example for the Add method for the Style object.

Greetings,
Klaus
 
S

scorpion53061

Klaus,

So if we wanted to apply this style to our text how would you do it in code?

Thank you for your help!!
 
K

Klaus Linke

So if we wanted to apply this style to our text how would you do it in
code?

The code in my first post should do it.
I accidentally used the German "Tabellengitternetz" instead of "Table Grid".

With myTable
.Style = ActiveDocument.Styles("Table Grid")
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = True
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = True
End With

"Table Grid" doesn't seem to have any special heading rows, last rows, first
column or last column.
So the four .Apply... lines could probably be left away.

Greetings,
Klaus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top