#1
|
|||
|
|||
Macro to strip table styles, please
Some years ago, someone here wrote me a macro to copy just the tables in a Word document and paste then into a new one. The purpose of this is to be able to reformat these tables with a custom table style and apply para styles, as necessary, local formatting, before replacing the tables in the original document with the new ones.
A further step that could be very useful would be replacing the table styles in the new document with the style upon which our custom tables are based, namely, Table Grid. (There are a bunch of MS built-in Table Grid styles, numbered, "light," etc.; I used plain Table Grid, finding it more amenable that Normal.) I am guessing that this action could be added to the existing macro (see below). One additional option that would be nice, if possible, would be a separate action to clear text styles. I say option, because, while usually the simplest path is to strip them all and apply the custom paragraph styles, there are times when in long tables, when text and bulleted paragraphs are frequently interspersed, it can be easier to leave bulleted paragraphs bulleted so that finding them is easier. This action might therefore be set off by apostrophe paragraphs and a description line, so that the user could disable that particular part of the macro when desired. (The only way I know to do this is to start each action line with an apostrophe; I'm assuming it would not be many lines long; I'm no coder.) Would someone be willing to provide this? Code:
Sub CopyTables() Dim Source As Document Dim Target As Document Dim tbl As Table Dim tr As Range Set Source = ActiveDocument Set Target = Documents.Add For Each tbl In Source.Tables Set tr = Target.Range tr.Collapse wdCollapseEnd tr.FormattedText = tbl.Range.FormattedText tr.Collapse wdCollapseEnd tr.Text = vbCrLf Next End Sub |
#2
|
|||
|
|||
Code:
Sub CopyTables() Dim oDoc As Document Dim oDocTarget As Document Dim oTbl As Table, oTblTarget As Table Dim oRng As Range Set oDoc = ActiveDocument Set oDocTarget = Documents.Add For Each oTbl In oDoc.Tables Set oRng = oDocTarget.Range With oRng .Collapse wdCollapseEnd oRng.FormattedText = oTbl.Range.FormattedText Set oTblT = oRng.Tables(1) oTblT.Style = "Table Grid" .Collapse wdCollapseEnd .Text = vbCrLf End With Next oDocTarget.Range.Select Selection.ClearFormatting Selection.Collapse wdCollapseStart oDoc.Activate lbl_Exit: Exit Sub End Sub |
#3
|
|||
|
|||
Greg, thank you very much again.
If I am making out the code, correctly, I think that the word Next is the beginning of action to clear paragraph styles after Table Grid has been applied to all the tables. But since I don't know coding, will you please 1) confirm or correct this, and 2) specify which lines I would precede with an apostrophe in order to leave the paragraph styles as-is? |
#4
|
|||
|
|||
Code:
Sub CopyTables() Dim oDoc As Document Dim oDocTarget As Document Dim oTbl As Table, oTblTarget As Table Dim oRng As Range Set oDoc = ActiveDocument Set oDocTarget = Documents.Add For Each oTbl In oDoc.Tables Set oRng = oDocTarget.Range With oRng .Collapse wdCollapseEnd oRng.FormattedText = oTbl.Range.FormattedText Set oTblT = oRng.Tables(1) oTblT.Style = "Table Grid" .Collapse wdCollapseEnd .Text = vbCrLf End With Next 'It is called stet: 'oDocTarget.Range.Select 'Selection.ClearFormatting 'Selection.Collapse wdCollapseStart oDoc.Activate lbl_Exit: Exit Sub End Sub Code:
Sub CopyTables() Dim oDoc As Document Dim oDocTarget As Document Dim oTbl As Table, oTblTarget As Table Dim oRng As Range Set oDoc = ActiveDocument Set oDocTarget = Documents.Add For Each oTbl In oDoc.Tables Set oRng = oDocTarget.Range With oRng .Collapse wdCollapseEnd .FormattedText = oTbl.Range.FormattedText Set oTblT = oRng.Tables(1) oTblT.Style = "Table Grid" .Collapse wdCollapseEnd .Text = vbCrLf End With Next 'It is called stet: 'oDocTarget.Range.Select 'Selection.ClearFormatting 'Selection.Collapse wdCollapseStart oDoc.Activate lbl_Exit: Exit Sub End Sub Last edited by Charles Kenyon; 11-02-2020 at 04:39 PM. |
#5
|
|||
|
|||
Greg, thank you. I figured our your message. Perfect!
|
Tags |
strip styles, table styles |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
strip characters from the right | jon.fallows@sedgman.com | Excel | 3 | 05-09-2018 11:38 PM |
Format shelf strip graphic and data | ShaunO | Mail Merge | 1 | 08-19-2016 04:20 PM |
Heading row disappears from table styles when pasted table is selected | andrewballem | Word Tables | 2 | 11-12-2013 05:18 AM |
strip extra information from email addresses | jbk1043 | Word | 1 | 03-03-2013 01:46 PM |
Strip lf/cr from text | p3aul | Word | 9 | 08-18-2009 12:42 AM |