#1
|
|||
|
|||
Automatically edit formatting af tables across a document
Dear Word VBA users
I am writing on a rather long Word document (150+ pages) containing a hugh amount of Tables each containing one cell only. My job is to edit the tables: Typically changing the outline width (eventually changing to no outline) and the fill color. All of them should be handled in the same way. I don't want to do it manually, because it is a huge job. I wonder if it can be done using some VBA code automatically? Earlier I had some experience with VBA code in Excel, but I am rather rusty on this topic. Beside I have never before used VBA code for MS Word. Hope someone can point me in the right direction. Regards, Erik |
#2
|
||||
|
||||
You could use something like the following which will set no border and fill the cells with a light grey shading for all tables. Change or add whatever else you need in the FormatTable macro.
Code:
Sub UpdateAllTables() Dim oStory As Range Dim oTable As Table For Each oStory In ActiveDocument.StoryRanges For Each oTable In oStory.Tables FormatTable oTable Next oTable If oStory.StoryType <> wdMainTextStory Then While Not (oStory.NextStoryRange Is Nothing) Set oStory = oStory.NextStoryRange For Each oTable In oStory.Tables FormatTable oTable Next oTable Wend End If Next oStory Set oStory = Nothing lbl_Exit: Exit Sub End Sub Private Sub FormatTable(oTable As Table) With oTable .Borders.OutsideLineStyle = wdLineStyleNone .Shading.BackgroundPatternColor = wdColorGray10 End With lbl_Exit: Exit Sub End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#3
|
|||
|
|||
Thank you very much, gmayor. Your solution works perfectly. And I could easily change for another color, replacing wdColorGray with a specific RGB command. Great that such a Forum exists with experts like you!
In fact my tables have identical formatting, except for maybe 10 tables, which are different. Now using your script will change all of them to the same. I can of course change those few tables manually back to their original state, but I wonder if it would be possible to improve the script without too much work to make it go through the document stopping at each table asking me - pretty much like the ordinary Search/Replace box in Word does. The buttons could be: Find Next: Going to the next table Replace: Replace the formatting in the current table Replace Forward: Replace the formatting in all tabels from the cursor's position and forward Skip: Skipping, while keeping the tables, which have already been changed I don't know if this task require a lot of code. If so, forget about it. I am already very happy with your current code. Again thanks a lot for your help! Erik |
#4
|
||||
|
||||
The macro process all story ranges which can prove awkward when selecting the tables in order to determine whether to process them, so for the following I have added a prompt for each table in the body of the document that does not match the chosen criteria. Those tables not in the body of the document are simply processed without a prompt.
Change the part of the code that processes the tables as follows Code:
Private Sub FormatTable(oTable As Table) Const lngBackColor As Long = wdColorGray10 If oTable.Range.InRange(ActiveDocument.Range) = True Then With oTable If Not .Borders.OutsideLineStyle = wdLineStyleNone And _ Not .Borders.InsideLineStyle = wdLineStyleNone And _ Not .Shading.BackgroundPatternColor = lngBackColor Then .Select If MsgBox("Format the selected table?", vbYesNo, "Format tables") = vbYes Then .Borders.OutsideLineStyle = wdLineStyleNone .Borders.InsideLineStyle = wdLineStyleNone .Shading.BackgroundPatternColor = lngBackColor End If End If End With Else With oTable .Borders.OutsideLineStyle = wdLineStyleNone .Borders.InsideLineStyle = wdLineStyleNone .Shading.BackgroundPatternColor = lngBackColor End With End If lbl_Exit: Exit Sub End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#5
|
|||
|
|||
Hi gmayor
I did replace the old FormatTable procedure with the new, but I have had some troubles making it work. First of all, I am not sure what you mean when you say "In the body of the document"? Isn't that the entire document or do I need to select something in the document? Anyway I didn't receive a single MsgBox whatever I did (selected some part of the document or not) when running the script. I don't understand why you are asking for .Shading.BackgroundPatternColor NOT to be lngBackColor (set to wdColorGray10 in line 2)? I guess maybe it is getting too complicated. Then I would rather like the script to ask me for every single Table if I want it changed or not. NB! By the way: Obviously wdLineStyleNone, means no outline for the table. But how do you set a table to have no fill (background)? Thanks, Erik |
#6
|
||||
|
||||
A document comprises several story ranges. In this instance the macro processes all the story ranges, but only prompts for tables in the document that don't meet the required criteria that are in the main body of the document as opposed to (say) being in headers and footers.
Your original post said that the document had many tables, so to avoid prompting for tables that already met your criteria, the macro checks whether the tables meet the criteria before prompting. As the tables are single cell you can remove the lines that relate to the insidelinestyle. If you want to select regardless.then remove the checks e.g. Code:
Private Sub FormatTable(oTable As Table) Const lngBackColor As Long = wdColorGray10 If oTable.Range.InRange(ActiveDocument.Range) = True Then With oTable .Select If MsgBox("Format the selected table?", vbYesNo, "Format tables") = vbYes Then .Borders.OutsideLineStyle = wdLineStyleNone .Shading.BackgroundPatternColor = lngBackColor End If End With Else With oTable .Borders.OutsideLineStyle = wdLineStyleNone .Shading.BackgroundPatternColor = lngBackColor End With End If lbl_Exit: Exit Sub End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#7
|
|||
|
|||
Thank you very much, Graham, for your contribution. It really helped me. And best wishes for the new year!
Erik |
Tags |
table, table across, table border |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Tables in Word Document - Formatting | Networkrail | Word Tables | 0 | 08-22-2017 05:49 AM |
Edit one document, have it automatically update other connected documents. | bradyb90 | Word | 1 | 05-10-2015 10:53 AM |
Forms fields: edit content easily in tables | kirby gilman | Word | 8 | 12-04-2014 08:43 AM |
Formatting all tables | knightmetal | Word | 2 | 09-10-2012 10:53 PM |
How to create an Excel add in that will automatically build tables from data | selvamariappan | Excel Programming | 1 | 12-12-2011 03:11 AM |