Solved

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

Posted on 2013-05-13
14
577 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
[X]
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
  • 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Lync meeting or Lync conferencing is what many organizations would like to deploy to allow them save money. But companies are now giving up for various reasons, one of which is that they cannot join external meetings (non-federated company meetings)…
User Beware!  This is a rather permanent solution to removing your email from an exchange server.  The only way to truly go back is to have your exchange administrator restore your mailbox from backups.  This is usually the option of last resort.  A…
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …

696 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