?
Solved

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

Posted on 2013-05-13
14
Medium Priority
?
602 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 2000 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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Article by: Leon
Software Metering within our group of companies has always been an afterthought until auditing of software and licensing became a pain point. Orchestrator and SCCM metering gave us the answer and it was an exciting process.
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

801 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