Solved

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

Posted on 2013-05-13
14
542 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

As with any other System Center product, the installation for the Authoring Tool can be quite a pain sometimes. This article serves to help you avoid making these mistakes and hopefully save you a ton of time on troubleshooting :)  Step 1: Make sur…
The new Microsoft OS looks great, is easier than ever to upgrade to, it is even free.  So what's the catch?  If you don't change the privacy settings, Microsoft will, in accordance with the (EULA) you clicked okay to without reading, collect all the…
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

896 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

13 Experts available now in Live!

Get 1:1 Help Now