Solved

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

Posted on 2013-05-13
14
526 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
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 …
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

759 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

19 Experts available now in Live!

Get 1:1 Help Now