![]() |
#1
|
|||
|
|||
![]()
Hello everyone.
I am trying to generate a macro code that will allow me to change all the styles in a word document. What seemed to me to be a simple function, it is starting to get really complicated since I can't manage to get it working. So, I have googled what I thought it would be a perfect function: Sub ApplyTableStyle() Dim t As Table For Each t In ActiveDocument.Tables t.Style = "Light Shading - Accent 3" Next End Sub And it changed the style of some of the tables, but not all of them. Some of the tables I have to click them individually and change its style individually.But I can only do it if I select the option "Apply and clear formatting" If I choose "Apply and maintain formating" it doesn't do anything. Is there any way of overcoming this? I have tried in two different laptops and it happens on both.. This document wasn't generated by me in the first place.. so Is there any way of clearing all the "nonsense" and do this from scratch? Any ideas? Thank you very much. Regards, Edgar |
#2
|
||||
|
||||
![]()
The command you are using appears to be correct. I suspect the tables it is failing on are already using that table style so the local overrides are not be wiped. What happens if you first move away from the preferred table style and then apply the right one?
Code:
Sub ApplyTableStyle() Dim t As Table For Each t In ActiveDocument.Tables t.Style = "Table Normal" t.Style = "Light Shading - Accent 3" Next End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#3
|
|||
|
|||
![]()
Unfortunately it didn't work. I have even tried to change the format manually and it doesn't accept it at all. I have attached an example of 2 tables that I am trying to edit. I copied them from the original file. I have a document full of this type of tables and need a macro to change its format all at once.
Any idea?suggestions? the document is named as test. I am looking forward to hear your suggestions. Thank you. Edgar |
#4
|
||||
|
||||
![]()
In your sample document, there is an extra level of complexity - you tables are contained inside text boxes. Try this code
Code:
Sub ApplyTableStyle() Dim t As Table, aShp As Shape For Each t In ActiveDocument.Tables t.Style = "Light Shading - Accent 3" Next For Each aShp In ActiveDocument.Shapes For Each t In aShp.TextFrame.TextRange.Tables t.Style = "Light Shading - Accent 3" Next t Next aShp End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#5
|
|||
|
|||
![]()
Hi there.
It did change the layout of some of the tables, however not all of them I am affraid. It comes up with the error "run time error 5917". I have attached 2 screenshots.. any other suggestions? Thanks you very much for all your support. |
#6
|
||||
|
||||
![]()
In that case, you must also have other shapes in your document that don't contain text frames. Try this klunky version to handle that
Code:
Sub ApplyTableStyle() Dim t As Table, aShp As Shape For Each t In ActiveDocument.Tables t.Style = "Light Shading - Accent 3" Next On Error GoTo JumpJump For Each aShp In ActiveDocument.Shapes For Each t In aShp.TextFrame.TextRange.Tables t.Style = "Light Shading - Accent 3" Next t JumpJump: Err.Clear Next aShp End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
oldyeller1938 | Word Tables | 4 | 03-17-2018 01:42 AM |
Tables in Word Document - Formatting | Networkrail | Word Tables | 0 | 08-22-2017 05:49 AM |
When changing table style in Word 2010, font size seems to change but doesn't show in new tables | heartsoulliving | Word | 1 | 12-07-2016 05:17 PM |
Adding tables to Created word document whilst other word document open Help | rpb925 | Word VBA | 18 | 03-30-2016 04:45 PM |
![]() |
Ricyteach | Word VBA | 6 | 03-09-2015 07:11 PM |