+ Reply to Thread
Results 1 to 7 of 7

Thread: Macro not picking up all tables

  1. #1
    Registered User
    Join Date
    04-27-2010
    Location
    Louisville KY USA
    MS-Off Ver
    Excel 2003
    Posts
    17

    Question Macro not picking up all tables

    I realize this is an Excel forum but have a general VBA question I think. I'm trying to copy all tables in a Word document into a new document. This script is only pasting the first one, even though I can debug through it and see that the count of tables is 48 and I can watch i go from 1-48. It pastes the first one time, and then never pastes anything else again. I have a feeling it has to do with either selecting the table or using the correct activated document. I have only one document open at the time it runs (oDoc) and the only other one is the one that's created (oNewDoc). Any help appreciated.

    Thanks!

    Set oDoc = ActiveDocument
      
     If oDoc.ProtectionType <> wdNoProtection Then
      oDoc.Unprotect
     End If
     
     Set oNewDoc = Documents.Add
     
     oNewDoc.PageSetup.Orientation = wdOrientLandscape
     
     With oNewDoc.PageSetup
      .LeftMargin = CentimetersToPoints(0.75)
      .RightMargin = CentimetersToPoints(0.63)
      .TopMargin = CentimetersToPoints(0.75)
      .BottomMargin = CentimetersToPoints(0.63)
     End With
     
     For i = 1 To oDoc.Tables.Count
        oDoc.Activate
        oDoc.Tables(i).Select
        oDoc.Tables(i).Range.Copy
            
        oNewDoc.Activate
        oNewDoc.Range.MoveEnd
        oNewDoc.Range.Paste
      Next i
    Last edited by mateoc15; 06-09-2011 at 10:09 AM.

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    MSO2007 on WinXP/MSO2000 on Win7/winXP
    Posts
    2,183

    Re: Macro not picking up all tables

    Can you post a document with a few tables in it, I don't feel like guessing..
    Also, note that the site has a Word forum, so the question is ok.
    ---
    Ben Van Johnson

  3. #3
    Registered User
    Join Date
    04-27-2010
    Location
    Louisville KY USA
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: Macro not picking up all tables

    Unfortunately the file is somewhat proprietary and confidential. I can tell in debugging that the macro IS picking up that the document has 48 tables. Can you tell me if you think I'm activating and deactivating documents correctly, and if my copy/paste logic looks right? This is my first crack at copy/paste across documents.

  4. #4
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    Nowhere
    MS-Off Ver
    None
    Posts
    8,275

    Re: Macro not picking up all tables

    Did I miss the bit in the code where the copying occurs?

  5. #5
    Registered User
    Join Date
    04-27-2010
    Location
    Louisville KY USA
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: Macro not picking up all tables

    Quote Originally Posted by romperstomper View Post
    Did I miss the bit in the code where the copying occurs?
    Unfortunately I can't edit a post.
    oDoc.Tables(i).Range.Select
    is actually
    oDoc.Tables(i).Range.Copy
    The problem remains.

    EDIT: (haha) I did just change it in the original post to reflect the change noted in this post.

  6. #6
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    Nowhere
    MS-Off Ver
    None
    Posts
    8,275

    Re: Macro not picking up all tables

    Oh OK. I've moved this to the Word programming zone in the hope that someone who knows Word VBA will happen by.

  7. #7
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    MSO2007 on WinXP/MSO2000 on Win7/winXP
    Posts
    2,183

    Re: Macro not picking up all tables

    When I ran your code on a test document, it seemed to be pasting each new table on top of the previously pasted one. Therefore, try this one:
    Option Explicit
    Sub CopyTable()
        Application.ScreenUpdating = False
        Dim SourceDoc       As Document, _
            Destination     As Document, _
            TableCounter    As Long
            
        Set SourceDoc = ActiveDocument
        Set Destination = Documents.Add
        
        If SourceDoc.ProtectionType <> wdNoProtection Then
            SourceDoc.Unprotect
        End If
        
        Destination.PageSetup.Orientation = wdOrientLandscape
        
        With Destination.PageSetup
            .LeftMargin = CentimetersToPoints(0.75)
            .RightMargin = CentimetersToPoints(0.63)
            .TopMargin = CentimetersToPoints(0.75)
            .BottomMargin = CentimetersToPoints(0.63)
        End With
        Destination.Activate
        Selection.TypeParagraph
        Selection.TypeParagraph
        Selection.MoveUp Unit:=wdLine, Count:=1
        
        For TableCounter = 1 To SourceDoc.Tables.Count
            SourceDoc.Activate
            SourceDoc.Tables(TableCounter).Select
            SourceDoc.Tables(TableCounter).Range.Copy
            Destination.Activate
            
            'move down so that the new table is not pasted ON TOP
            'of the previous table and also not appended to the
            'bottom of the previous table
            
            Selection.TypeParagraph
            Selection.TypeParagraph
            
            'move up so that there is only one line between tables
            
            Selection.MoveUp Unit:=wdLine, Count:=1
            Selection.PasteAndFormat (wdPasteDefault)
            Selection.MoveDown Unit:=wdLine, Count:=1
        Next TableCounter
        
        Application.ScreenUpdating = True
    End Sub
    Attached Files Attached Files
    ---
    Ben Van Johnson

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.2.0