Results 1 to 2 of 2

Thread: VBA Copy Tables from Word to Excel

  1. #1
    Member
    Join Date
    Sep 2013
    Posts
    37
    Rep Power
    0

    VBA Copy Tables from Word to Excel

    Cross posted
    http://www.eileenslounge.com/viewtopic.php?f=26&t=37822





    hi all.

    i found code from google but i don't how the code is worked well, i have testing but not work
    i want the code can copy table from ms word into ms excel
    Code:
    Sub CopyTables()
        Dim oWord As Word.Application
        Dim WordNotOpen As Boolean
        Dim oDoc As Word.Document
        Dim oTbl As Word.Table
        Dim fd As Office.FileDialog
        Dim FilePath As String
        Dim wbk As Workbook
        Dim wsh As Worksheet
    
        ' Prompt for document
        Set fd = Application.FileDialog(msoFileDialogOpen)
        With fd
            .Filters.Clear
            .Filters.Add "Word Documents (*.docx)", "*.docx", 1
            .Title = "Choose a Word File"
            If .Show = True Then
                FilePath = .SelectedItems(1)
            Else
                Beep
                Exit Sub
            End If
        End With
    
        On Error Resume Next
    
        Application.ScreenUpdating = False
    
        ' Create new workbook
        Set wbk = Workbooks.Add(Template:=xlWBATWorksheet)
    
        ' Get or start Word
        Set oWord = GetObject(Class:="Word.Application")
        If Err Then
            Set oWord = New Word.Application
            WordNotOpen = True
        End If
    
        On Error GoTo Err_Handler
    
        ' Open document
        Set oDoc = oWord.Documents.Open(Filename:=FilePath)
        ' Loop through the tables
        For Each oTbl In oDoc.Tables
            ' Create new sheet
            Set wsh = wbk.Worksheets.Add(After:=wbk.Worksheets(wbk.Worksheets.Count))
            ' Copy/paste the table
            oTbl.Range.Copy
            wsh.Paste
        Next oTbl
    
        ' Delete the first sheet
        Application.DisplayAlerts = False
        wbk.Worksheets(1).Delete
        Application.DisplayAlerts = True
    
    Exit_Handler:
        On Error Resume Next
        oDoc.Close SaveChanges:=False
        If WordNotOpen Then
            oWord.Quit
        End If
        'Release object references
        Set oTbl = Nothing
        Set oDoc = Nothing
        Set oWord = Nothing
        Application.ScreenUpdating = True
        Exit Sub
    
    Err_Handler:
        MsgBox "Word caused a problem. " & Err.Description, vbCritical, "Error: " & Err.Number
        Resume Exit_Handler
    End Sub
    this original link
    https://answers.microsoft.com/en-us/...d-c9a981636d24

    anyone help me out..greatly appreciated
    .susanto
    Last edited by DocAElstein; 02-21-2022 at 02:17 PM.

  2. #2
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,453
    Rep Power
    10
    Hi
    I can’t help directly as I have little experience with Microsoft Word.
    We do not have many Word VBA experts looking in to excelfox

    However, I can tell you that the Author of that code, HansV, is quite active posting at Eileen’s Lounge, https://eileenslounge.com/app.php/portal

    Here is the link to the Word Sub Forum there:
    https://eileenslounge.com/viewforum.php?f=26

    You may be able to get help from that forum, if you join and post your question there
    If you decide to ask for help there, then I would suggest that you prepare and upload a Word File document example, and explain clearly exactly what you want the coding to do.

    ( Remember to tell them that you have also posted here:
    You must tell them, that you have also posted the same question at excelfox: Give them this link to your post here:
    https://excelfox.com/forum/showthread.php/2786-VBA-Copy-Tables-from-Word-to-Excel )



    Alan
    Last edited by DocAElstein; 02-16-2022 at 10:08 PM.

Similar Threads

  1. VBA How to pass formatted text from Excel to MS Word
    By johnweber in forum Excel Help
    Replies: 2
    Last Post: 03-01-2015, 08:41 PM
  2. Loop_Find & Copy to word document
    By Excelfun in forum Excel Help
    Replies: 3
    Last Post: 01-14-2014, 10:11 PM
  3. Replies: 7
    Last Post: 07-19-2013, 12:47 PM
  4. Excel Chart into Word VBA
    By dhiraj.ch185 in forum Word Help
    Replies: 3
    Last Post: 12-26-2012, 11:23 AM
  5. Speed up excel to word VBA
    By bcostin in forum Excel Help
    Replies: 3
    Last Post: 05-22-2012, 10:49 AM

Posting Permissions

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