Solved

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

Posted on 2013-11-11
5
351 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
ID: 39640625
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
ID: 39640650
The topics retain their column widths.
0
 

Author Comment

by:tomfolinsbee
ID: 39640852
0
 
LVL 59

Accepted Solution

by:
Chris Bottomley earned 500 total points
ID: 39641522
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
ID: 39649886
Thanks Chris. Worked perfectly the first time.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Shortcuts in Word Just the other day I had a training for Microsoft and they wanted me to show how well the new Windows and Office behaved on a touch device, which by the way is great, but it was only then that I realized that using keyboard shortc…
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 walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…

733 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