Results 1 to 12 of 12
  1. #1
    2 Star Lounger
    Join Date
    Feb 2001
    Location
    McLean, VA
    Posts
    125
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Cell property not obeying recorded macro (2002/SP-1)

    I recorded a macro to select a table and insert .03" left padding into all cells (using the Cell Options dialog). Aftrer recording it, everything looks fine, all the cells have the same .03" of left padding. But when I run the macro on any other table in the same file (I have a file with many similar tables) the change doesn't take.
    I made sure I used only the keyboard when recording the macro. I also had the same results when recording a similar macro that selects just a column, rather than the whole table.
    I don't know VBA well enough to write custom macros, but here's what it looks like for anyone who can detect a problem with it.
    Sub Tablespace()
    '
    ' Tablespace Macro
    ' Macro recorded 9/5/2002 by George Tipton
    '
    Selection.Tables(1).Select
    With Selection.Tables(1)
    .TopPadding = InchesToPoints(0.01)
    .BottomPadding = InchesToPoints(0.01)
    .LeftPadding = InchesToPoints(0.03)
    .RightPadding = InchesToPoints(0.01)
    .Spacing = 0
    .AllowPageBreaks = True
    .AllowAutoFit = True
    End With
    With Selection.Cells(1)
    .TopPadding = InchesToPoints(0.01)
    .BottomPadding = InchesToPoints(0.01)
    .LeftPadding = InchesToPoints(0.03)
    .RightPadding = InchesToPoints(0.01)
    .FitText = False
    End With
    End Sub

  2. #2
    5 Star Lounger
    Join Date
    Jul 2002
    Location
    Toronto, Ontario, Canada
    Posts
    1,139
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Cell property not obeying recorded macro (2002/SP-1)

    George,

    The problem is the first two lines.

    Selection.Tables(1).Select
    With Selection.Tables(1)

    They are pointing to the FIRST table in the document.

    If you want to do this to EVERY table in the document, you can try this (I only have W2K and not W2002/XP)

    <pre>Dim tbl As Table
    Dim cl As Cell

    For Each tbl In ActiveDocument.Tables
    With tbl
    .TopPadding = InchesToPoints(0.01)
    .BottomPadding = InchesToPoints(0.01)
    .LeftPadding = InchesToPoints(0.03)
    .RightPadding = InchesToPoints(0.01)
    .Spacing = 0
    .AllowPageBreaks = True
    .AllowAutoFit = True
    End With

    With tbl.Range.Cells(1)
    .TopPadding = InchesToPoints(0.01)
    .BottomPadding = InchesToPoints(0.01)
    .LeftPadding = InchesToPoints(0.03)
    .RightPadding = InchesToPoints(0.01)
    .FitText = False
    End With
    Next</pre>

    --
    Bryan Carbonnell - Toronto <img src=/S/flags/Ontario.gif border=0 alt=Ontario width=30 height=18> <img src=/S/flags/Canada.gif border=0 alt=Canada width=30 height=18>
    Unfortunately common sense isn't so common!!
    Visit my website for useful Word, Excel and Access code, templates and Add-Ins

  3. #3
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Cell property not obeying recorded macro (2002/SP-1)


  4. #4
    5 Star Lounger
    Join Date
    May 2001
    Location
    Stuttgart, Baden-W, Germany
    Posts
    931
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Cell property not obeying recorded macro (2002/SP-1)

    >> Selection.Tables(1).Select
    >> With Selection.Tables(1)

    > They are pointing to the FIRST table in the document.

    No, the first table in the Selection (or the table the selection is in).

    If you want to format each table in the Selection, use

    <pre>Dim myTable as Table</pre>

    <pre>For Each myTable in Selection.Tables
    With myTable
    ' ...
    End With
    Next myTable</pre>


    You could do Rows, Columns, and Cells the same way (For Each myRow in Selection.Rows, For Each myCell in Selection.Cells ...).

    <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16> Klaus

  5. #5
    5 Star Lounger
    Join Date
    Jul 2002
    Location
    Toronto, Ontario, Canada
    Posts
    1,139
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Cell property not obeying recorded macro (2002/SP-1)

    <hr>No, the first table in the Selection (or the table the selection is in).<hr>
    <img src=/S/doh.gif border=0 alt=doh width=15 height=15> <img src=/S/woops.gif border=0 alt=woops width=58 height=36>Quite right. I guess that's what happens when you are trying to do 14 things at once
    --
    Bryan Carbonnell - Toronto <img src=/S/flags/Ontario.gif border=0 alt=Ontario width=30 height=18> <img src=/S/flags/Canada.gif border=0 alt=Canada width=30 height=18>
    Unfortunately common sense isn't so common!!
    Visit my website for useful Word, Excel and Access code, templates and Add-Ins

  6. #6
    2 Star Lounger
    Join Date
    Feb 2001
    Location
    McLean, VA
    Posts
    125
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Re: Cell property not obeying recorded macro (2002/SP-1)

    Thanks, Klaus and Bryan. But since I'm a VB-newbie I'm confused. I intended to record a macro that would reformat all the cells of the table the insertion point was located in, but for this document doing all the tables at once would be even better! Unfortunately since I'm not very knowledgable about VB, I couldn't get past the first WIth statement when typing in Bryan's code. I kept getting "this action will reset your project". Reminded me of why I always hated programming. And I couldn't tell exactly how Klaus's code was meant to modify Bryan's, or if any particluar table-- or all tables? -- needed to be selected first before running the Klausian modification of the Bryanian macro.

  7. #7
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Cell property not obeying recorded macro (2002/SP-1)

    The message "this action will reset your project" means that there is a Macro currently running, and that you are trying to edit the code in a way that will prevent it from continuing to run. Just click on the square stop button before trying to make the changes.

    If you just replace all of your original code (Except for <font color=blue>Sub Tablespace()</font color=blue> at the beginning and <font color=blue>End Sub</font color=blue> at the end) with the code provided by Bryan then it should do the deed on every table in the document.

    I think that your original code should have worked, so long as you moved the selection to a point in another table between each time you ran it, and Bryan and Klaus were discussing the exact meaning of "Selection.Tables(1)" which means the first table in the current selection.

    regards,

    StuartR

  8. #8
    2 Star Lounger
    Join Date
    Feb 2001
    Location
    McLean, VA
    Posts
    125
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Re: Cell property not obeying recorded macro (2002/SP-1)

    Thanks for the clarification, Stuart! I typed in Bryan's code and it ran -- took about 30 seconds to complete the 23 page, 50 table document. But after the macro finished, the cells all had a left padding of .01 instead of .03. The default cell margins in the Table Options did reflect the values the macro specified, but those values appear to be subservient to the values in the Cell Options tab, which are different and don't appear to have been affected by the macro.

  9. #9
    2 Star Lounger
    Join Date
    Feb 2001
    Location
    McLean, VA
    Posts
    125
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Re: Cell property not obeying recorded macro (2002/SP-1)

    Thanks again, Stuart. I really appreciate all your help.
    When I tried your macro to set every cell in every table, I got a "Compile error. Method or member not found" at .FitText = False. When I removed it, all I got was a steady hourglass and had to close Word.

  10. #10
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Cell property not obeying recorded macro (2002/SP-1)

    That makes sense, the code is setting the Table level value and the Cell specific value for the first cell in each table.

    This version sets the values for every cell in every table...

    <pre>Public Sub TableSpace()
    Dim tbl As Table
    Dim cl As Cell

    For Each tbl In ActiveDocument.Tables
    With tbl
    .TopPadding = InchesToPoints(0.01)
    .BottomPadding = InchesToPoints(0.01)
    .LeftPadding = InchesToPoints(0.03)
    .RightPadding = InchesToPoints(0.01)
    .Spacing = 0
    .AllowPageBreaks = True
    .AllowAutoFit = True
    For Each cl In tbl.Range.Cells
    With cl
    .TopPadding = InchesToPoints(0.01)
    .BottomPadding = InchesToPoints(0.01)
    .LeftPadding = InchesToPoints(0.03)
    .RightPadding = InchesToPoints(0.01)
    .FitText = False
    End With
    Next cl
    End With
    Next tbl

    End Sub
    </pre>

    StuartR

  11. #11
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Cell property not obeying recorded macro (2002/SP-1)

    Sorry about that. There was an error in my previous post, which I have now corrected by adding an extra
    <pre>With cl
    End with
    </pre>

    In the correct places.

    If this appears to hang then please be patient, if you have lots of tables with lots of cells in each then it has to visit each one.

    StuartR

  12. #12
    2 Star Lounger
    Join Date
    Feb 2001
    Location
    McLean, VA
    Posts
    125
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Re: Cell property not obeying recorded macro (2002/SP-1)

    Stuart,
    Your fixes worked, and the Tablespace macro runs beautifully now. Thanks so much!
    Woody's Lounge sure is a valuable resource!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •