Solved

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

Posted on 2013-11-11
5
362 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
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…

624 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