Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-30-2020, 06:32 AM
Ulodesk Ulodesk is offline Macro to strip table styles, please Windows 10 Macro to strip table styles, please Office 2016
Word 2013 Expert Cert
Macro to strip table styles, please
 
Join Date: Sep 2009
Location: Virginia
Posts: 866
Ulodesk is on a distinguished road
Default 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
Reply With Quote
  #2  
Old 10-30-2020, 08:17 AM
gmaxey gmaxey is offline Macro to strip table styles, please Windows 10 Macro to strip table styles, please Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,421
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 10-30-2020, 09:06 AM
Ulodesk Ulodesk is offline Macro to strip table styles, please Windows 10 Macro to strip table styles, please Office 2016
Word 2013 Expert Cert
Macro to strip table styles, please
 
Join Date: Sep 2009
Location: Virginia
Posts: 866
Ulodesk is on a distinguished road
Default

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?
Reply With Quote
  #4  
Old 10-30-2020, 01:14 PM
gmaxey gmaxey is offline Macro to strip table styles, please Windows 10 Macro to strip table styles, please Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,421
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
[moderator attempt to reformat - think I got it right - CK ]



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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/

Last edited by Charles Kenyon; 11-02-2020 at 04:39 PM.
Reply With Quote
  #5  
Old 11-02-2020, 06:38 AM
Ulodesk Ulodesk is offline Macro to strip table styles, please Windows 10 Macro to strip table styles, please Office 2016
Word 2013 Expert Cert
Macro to strip table styles, please
 
Join Date: Sep 2009
Location: Virginia
Posts: 866
Ulodesk is on a distinguished road
Default

Greg, thank you. I figured our your message. Perfect!
Reply With Quote
Reply

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
Macro to strip table styles, please Format shelf strip graphic and data ShaunO Mail Merge 1 08-19-2016 04:20 PM
Macro to strip table styles, please Heading row disappears from table styles when pasted table is selected andrewballem Word Tables 2 11-12-2013 05:18 AM
Macro to strip table styles, please strip extra information from email addresses jbk1043 Word 1 03-03-2013 01:46 PM
Macro to strip table styles, please Strip lf/cr from text p3aul Word 9 08-18-2009 12:42 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 08:40 AM.


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