Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-03-2016, 08:24 AM
Ulodesk Ulodesk is offline Adding to a macro Windows 7 64bit Adding to a macro Office 2013
Word 2013 Expert Cert
Adding to a macro
 
Join Date: Sep 2009
Location: Virginia
Posts: 872
Ulodesk is on a distinguished road
Default Adding to a macro

Allen Wyatt posted this useful macro on WordTips.net quite a while ago. I would like an addition to it that will, after pasting, strip all styles from the tables, including table styles. I would normally use Ctrl+spacebar, Ctrl+Q, and Alt+Ctrl+u.
This would be useful for anyone needing to bring the tables from another document into one in, for instance, a custom template with its own table styles.


I'm sure this is a simple matter for someone who knows VBA, and I thank him or her in advance.

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 03-06-2016, 04:37 PM
macropod's Avatar
macropod macropod is offline Adding to a macro Windows 7 64bit Adding to a macro Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,345
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Try:
Code:
Sub CopyTables()
Application.ScreenUpdating = False
Dim DocSrc As Document, DocTgt As Document, Tbl As Table, Rng As Range
Set DocSrc = ActiveDocument: Set DocTgt = Documents.Add
For Each Tbl In DocSrc.Tables
  Set Rng = DocTgt.Characters.Last
  With Rng
    .Collapse wdCollapseEnd
    .FormattedText = Tbl.Range.FormattedText
    .Tables(1).Style = "Table Grid"
    .Collapse wdCollapseEnd
    .InsertAfter vbCr
  End With
Next
Set DocSrc = Nothing: Set DocTgt = Nothing
Application.ScreenUpdating = True
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 03-07-2016, 07:43 AM
Ulodesk Ulodesk is offline Adding to a macro Windows 7 64bit Adding to a macro Office 2013
Word 2013 Expert Cert
Adding to a macro
 
Join Date: Sep 2009
Location: Virginia
Posts: 872
Ulodesk is on a distinguished road
Default modification

Paul, thank you, as always. I can certainly use this when maintaining the paragraph styles in the original table will be the best procedure. I would like, however, to be able to strip the paragraph and character styles as well, as I tried to indicate in my post, so that I have a "style-less" (I know Normal will be applied) table and text.
My reason is, that in my proposal work, I occasionally have a long document with lots of tables and a style list from, shall we say, the subterranean, to import, clean, into a custom template. Making short work of the table reformatting will be a very significant time-saver in such cases.

Much obliged, and best wishes.
Philip
Reply With Quote
  #4  
Old 03-07-2016, 01:31 PM
macropod's Avatar
macropod macropod is offline Adding to a macro Windows 7 64bit Adding to a macro Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,345
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by Ulodesk View Post
I would like, however, to be able to strip the paragraph and character styles as well
In that case, simply insert:
.Style = wdStyleNormal
.Font.Reset
after:
.FormattedText = Tbl.Range.FormattedText
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 03-07-2016, 01:40 PM
Ulodesk Ulodesk is offline Adding to a macro Windows 7 64bit Adding to a macro Office 2013
Word 2013 Expert Cert
Adding to a macro
 
Join Date: Sep 2009
Location: Virginia
Posts: 872
Ulodesk is on a distinguished road
Default Addition

Splendid! Thanks again.

P
Reply With Quote
Reply

Tags
strip styles, table styles



Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding to a macro Adding a save as macro button to a template nparsons75 Word 6 01-08-2016 07:47 AM
Adding to a macro VBA Code for adding Macro to QAT OTPM Project 4 08-22-2013 01:33 PM
Adding to a macro Adding macro to normal.dotm programmatically etippelt Word VBA 6 04-08-2013 05:55 PM
Adding Macro menu to the right click menu (Word 2007) masam123 Word 0 10-14-2011 04:05 AM
Macro for automatically adding filename without .doc RPM7 Word VBA 0 04-29-2010 01:43 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 08:25 PM.


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