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

    Home » Forums » AskWoody support » Productivity software by function » MS Word and word processing help » Cell property not obeying recorded macro (2002/SP-1)

    Author
    Topic
    #376026

    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

    Viewing 1 reply thread
    Author
    Replies
    • #614433

      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)

      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
      • #614445

        >> 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

        Dim myTable as Table
        For Each myTable in Selection.Tables
           With myTable
              ' ...
           End With
        Next myTable

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

        cheers Klaus

        • #614448

          [indent]


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


          [/indent]
          doh woopsQuite right. I guess that’s what happens when you are trying to do 14 things at once

        • #614454

          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.

          • #614457

            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 Sub Tablespace() at the beginning and End Sub 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

            • #614468

              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.

            • #614473

              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…

              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
              

              StuartR

            • #614630

              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.

            • #614649

              Sorry about that. There was an error in my previous post, which I have now corrected by adding an extra

              With cl
              End with
              

              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

            • #614667

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

    • #614437
    Viewing 1 reply thread
    Reply To: Reply #614457 in Cell property not obeying recorded macro (2002/SP-1)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information:




    Cancel