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
Solved

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

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

This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
This video walks the viewer through the process of creating an MLA formatted document, as well as a bibliography with citations.
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.

789 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