Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-31-2019, 10:27 AM
edgar edgar is offline Replace style in all tables in a word document. Windows 10 Replace style in all tables in a word document. Office 2019
Novice
Replace style in all tables in a word document.
 
Join Date: Mar 2019
Posts: 3
edgar is on a distinguished road
Default Replace style in all tables in a word document.

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
Reply With Quote
  #2  
Old 03-31-2019, 07:59 PM
Guessed's Avatar
Guessed Guessed is offline Replace style in all tables in a word document. Windows 10 Replace style in all tables in a word document. Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,161
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #3  
Old 04-01-2019, 02:02 PM
edgar edgar is offline Replace style in all tables in a word document. Windows 10 Replace style in all tables in a word document. Office 2019
Novice
Replace style in all tables in a word document.
 
Join Date: Mar 2019
Posts: 3
edgar is on a distinguished road
Default

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
Attached Files
File Type: docx test.docx (23.2 KB, 10 views)
Reply With Quote
  #4  
Old 04-01-2019, 03:24 PM
Guessed's Avatar
Guessed Guessed is offline Replace style in all tables in a word document. Windows 10 Replace style in all tables in a word document. Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,161
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #5  
Old 04-02-2019, 03:41 PM
edgar edgar is offline Replace style in all tables in a word document. Windows 10 Replace style in all tables in a word document. Office 2019
Novice
Replace style in all tables in a word document.
 
Join Date: Mar 2019
Posts: 3
edgar is on a distinguished road
Default

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.
Attached Images
File Type: png 5917.PNG (4.0 KB, 20 views)
File Type: png each.PNG (9.2 KB, 20 views)
Reply With Quote
  #6  
Old 04-02-2019, 04:25 PM
Guessed's Avatar
Guessed Guessed is offline Replace style in all tables in a word document. Windows 10 Replace style in all tables in a word document. Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,161
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Replace style in all tables in a word document. Using tables in a Word document 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
Replace style in all tables in a word document. Use multiple style sets in the same Word document (depending on which section the style is in) Ricyteach Word VBA 6 03-09-2015 07:11 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 11:42 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft