Solved

Word VBA to select only the first three tables and change the cell margin properties

Posted on 2013-05-13
14
557 Views
Last Modified: 2013-12-19
Greetings Experts! I'm in need of some VBA for Word that will select the first three tables in a document and then change the left and right cell margins of those tables only.

Would appreciate some help! Thanks so much, Jan
0
Comment
Question by:jbarcher13
  • 7
  • 6
14 Comments
 
LVL 15

Expert Comment

by:Ess Kay
ID: 39162428
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 39162509
In VBA, you can adjust the margins (padding) without Selecting the tables:
Sub AdjustMargins()
    Dim tbl As Table
    Dim t As Integer
    Dim cl As Cell
    
    For t = 1 To 3
        Set tbl = ActiveDocument.Tables(t)
        For Each cl In tbl.Range.Cells
            tbl.LeftPadding = CentimetersToPoints(0.1)
            tbl.RightPadding = CentimetersToPoints(0.1)
        Next cl
    Next t
End Sub

Open in new window

0
 

Author Comment

by:jbarcher13
ID: 39162833
So Graham,

Actually, I forgot...it's table 2, 3 & 4...do I just change "For t = 2 to 4"?

Thanks, Jan
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 76

Expert Comment

by:GrahamSkan
ID: 39163072
Why not try it? But, yes.
0
 

Author Comment

by:jbarcher13
ID: 39163123
Hi Graham,

Well, I did and nothing happened. I inserted a module in my template and before I called it in my AutoOpen, I tried it using F5. Am I doing something wrong?

Thanks so much! Jan
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 39163239
It isn't clear whether it works when you try it with F5.

If not, make sure that the adjustment is sufficient to make a visible difference and see if runs through the code by stepping through with the F8 key.
If it does, are you sure that it runs at all? Try putting a Stop instruction or a breakpoint at the beginning.

If the code is in a Template and you are using it to create a new Document in the usual way, then AutoNew would be the correct auto macro to use.
0
 

Author Comment

by:jbarcher13
ID: 39163346
Hi Graham,

I'll try this tomorrow am when I get back to work...I'll let you know what happens, ok?

Thanks, Jan
0
 

Author Comment

by:jbarcher13
ID: 39169156
Hi Graham,

Well, I can't get this to work for me. I changed the "CentimetersToPoints(.1)" to "InchesToPoints(.04)" and it still didn't work.

I then did the manual steps of going to the 2nd table, selecting the table (Table Tools > Layout > Select > Select Table), opened Properties, clicked the Cell > Options, and entered the left & right padding value. Then clicked OK and OK again. That works. It would seem that if I recorded those steps, it should work, right? So I tried it wil one table. Here's the code.

Sub AdjustTableMargins()
'
' Macro2 Macro
'
'
    Selection.GoTo What:=wdGoToTable, Which:=wdGoToFirst, Count:=2, Name:=""
    Selection.Find.ClearFormatting
    With Selection.Find
        .Text = ""
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchKashida = False
        .MatchDiacritics = False
        .MatchAlefHamza = False
        .MatchControl = False
        .MatchByte = False
        .CorrectHangulEndings = True
        .HanjaPhoneticHangul = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchWildcards = False
        .MatchFuzzy = False
    End With
    Selection.Tables(1).Select
    With Selection.Cells(1)
        .TopPadding = InchesToPoints(0)
        .BottomPadding = InchesToPoints(0)
        .LeftPadding = InchesToPoints(0.04)
        .RightPadding = InchesToPoints(0.04)
        .WordWrap = True
        .FitText = False
    End With
    
End Sub

Open in new window


Even that didn't work when I ran it....hmmmmm. I'm stumped.

Thoughts? Jan
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 39170646
It does sound a bit strange. Do other macros work? Is it just the one document?
You might have some sort of problem with Word, so try the fixes here:
http://support.microsoft.com/default.aspx/kb/921541.

Meanwhile, I notice that I have missed a trick. It is much faster to do the whole table instead of going cell by cell.
Sub AdjustMargins2()
    Dim tbl As Table
    Dim t As Integer
   
    For t = 1 To 3
        Set tbl = ActiveDocument.Tables(t)
        With tbl
            .LeftPadding = InchesToPoints(0.04)
            .RightPadding = InchesToPoints(0.04)
        End With
    Next t
End Sub
0
 

Author Comment

by:jbarcher13
ID: 39171306
Hi Graham,

Well, I copied your solution into my template macros, called it from the AutoOpen, and tried it, but it still didn't work. So, here's what I came up with:

Sub FixCellMarginsT2()

Dim myCell As Cell
Dim myTable As Table
Dim t As Integer

'
' Macro2 Macro
'
'
    Selection.GoTo What:=wdGoToTable, Which:=wdGoToFirst, Count:=2, Name:=""
    Selection.Find.ClearFormatting
    With Selection.Find
        .Text = ""
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchKashida = False
        .MatchDiacritics = False
        .MatchAlefHamza = False
        .MatchControl = False
        .MatchByte = False
        .CorrectHangulEndings = True
        .HanjaPhoneticHangul = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchWildcards = False
        .MatchFuzzy = False
    End With
    
    Set myTable = Selection.Tables(1)
            For Each myCell In myTable.Range.Cells
            With myCell
                .TopPadding = InchesToPoints(0)
                .BottomPadding = InchesToPoints(0)
                .LeftPadding = InchesToPoints(0.04)
                .RightPadding = InchesToPoints(0.04)
                .WordWrap = True
                .FitText = False
            End With
        Next myCell
End Sub

Open in new window


I created three of these (in the same module) and call each one from the AutoOpen macro...it works. It's not very sophisticated, but it works.

The document is created (or rather published) from an Author-it database so I'm not creating a new document. It's frustrating because it seems so simple, yet so hard. I wish I could find an in-depth class to help me really learn how to do this...do you know of any?

Thanks so much...Jan
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 39174043
I can't guess why the macro doesn't work for you. It does for me. Perhaps there is something about your document that I haven't though of. Could you post a redacted example?

Lines 11 to 32 in your recorded code don't seem to do anything (Find with no Execute), so you could probably tidy the code by deleting them.
0
 

Author Comment

by:jbarcher13
ID: 39174649
Hi Graham,

Well, I was thinking about this and there is a macro that runs during the publishing process (in an Author-it AfterPublish macro) that sets the left & right padding to zero for ALL tables in the doc. I'm wondering if that gets in the way of this macro?

Let me publish a doc that I can send you...thanks! Jan
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 39175061
That would explain it.
0
 

Author Comment

by:jbarcher13
ID: 39175269
Thought so...since I have something that works, I'll keep it for now until I find a better way to exclude those three tables from the afterpublish macro.

Thanks so much for your help! Jan
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

The canonical version of this article is on my web site here: http://iconoun.com/articles/collisions/ A companion presentation is available here: http://iconoun.com/articles/collisions/Unicode_Presentation.pdf
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 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…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…

813 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

18 Experts available now in Live!

Get 1:1 Help Now