Solved

MS Word macro to switch columns in all tables, in all documents in folder

Posted on 2013-11-11
5
336 Views
Last Modified: 2013-11-14
I have folder with about 100 Word files, each with one song.  I would like a script that will, for all tables, in all files, switch the column order (ie, chords are in first column, lyrics are in 2nd column). The column width should not be changed.

Thanks !

p.s. Will have some follow up questions to this one...
Lantau-Songbook.zip
0
Comment
Question by:tomfolinsbee
  • 3
5 Comments
 
LVL 22

Expert Comment

by:Flyster
Comment Utility
The column width should not be changed
Does that mean 1) the wide column remains on the left or 2) the topics retain their column widths? If option 1, then realize you will have multiple pages.

Flyster
0
 

Author Comment

by:tomfolinsbee
Comment Utility
The topics retain their column widths.
0
 

Author Comment

by:tomfolinsbee
Comment Utility
0
 
LVL 59

Accepted Solution

by:
Chris Bottomley earned 500 total points
Comment Utility
Your sample files showed some errors with merged/split cells and different cell widths in some of the tables.  As a consequence the script speed is slightly reduced to address those issues.

The resultant script is as follows, save as a VBS file for example lantau.vbs, modify the files folder from "C:\DeleteMe\Lantau Songbook" to the folder where you have them stored  and then execute it.

Dim wdApp
Dim strErr
Dim tbl

On Error Resume Next
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder("C:\DeleteMe\Lantau Songbook")
Set Files = folder.Files

Set wdApp = CreateObject("Word.application")
For Each fil In Files
    Q28291498_1 fil
Next

If strErr = "" Then
    WScript.Echo "Finished"
Else
    WScript.Echo "Errors found as follows:" & vbCrLf & vbCrLf & strErr
End If

Sub Q28291498_1(fn)

    If Right(LCase(fn), 5) = ".docx" Then
        Set doc = wdApp.Documents.Open(fn.Path)
        If doc.Tables.Count > 0 Then
            For tbl = 1 To doc.Tables.Count
                With doc.Tables(tbl)
                    sngCol1 = .Rows(1).Cells(1).Width
                    sngCol2 = .Rows(1).Cells(2).Width
                    For intRow = 1 To .Rows.Count
                        strcol1 = .Rows(intRow).Cells(1).Range.Text
                        strcol2 = .Rows(intRow).Cells(2).Range.Text
                        .Rows(intRow).Cells(1).Range.Text = Left(strcol2, Len(strcol2) - 2)
                        .Rows(intRow).Cells(1).Width = sngCol2
                        .Rows(intRow).Cells(2).Range.Text = Left(strcol1, Len(strcol1) - 2)
                        .Rows(intRow).Cells(2).Width = sngCol1
                    Next
                    If Err = 0 Then
                        doc.Save
                    Else
                        strErr = strErr & vbCrLf & "Problem with layout in " & fn.Name & " Aborting file save"
                        Err = 0
                    End If
                    doc.Close False
                End With
            Next
        End If
    End If

End Sub

Open in new window



Chris
0
 

Author Closing Comment

by:tomfolinsbee
Comment Utility
Thanks Chris. Worked perfectly the first time.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
This article describes how to use the Send to Mail Recipient command. The instructions apply generally to Office 2007 and later versions, but Microsoft® Word 2013 was used for the specific steps and figures.  What is Send to Mail Recipient? Send…
This video teaches the viewer how to align pictures around text while keeping the text properly aligned in the document.
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now