Solved

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

Posted on 2013-11-11
5
346 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VBS Script not working correctly. 1 40
Word 2016 - Comments on one page contracted 2 51
Can we place a tooltip on the actual vb6 form 5 41
VBScript Write Column Headers 3 31
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
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…
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…

815 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

7 Experts available now in Live!

Get 1:1 Help Now