Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #16  
Old 05-23-2014, 04:23 AM
macropod's Avatar
macropod macropod is offline Copy data from Word into Excel Windows 7 32bit Copy data from Word into Excel Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Try the following Excel macro:


Code:
Sub GetWordTableData()
     'Note: this code requires a reference to the Word object model
    Application.ScreenUpdating = False
    Dim wdApp As New Word.Application
    Dim wdDoc As Word.Document
    Dim wdTbl As Word.Table
    Dim wdRng As Word.Range
    Dim strFolder As String, strFile As String
    Dim WkSht As Worksheet, i As Long, j As Long
    strFolder = GetFolder
    If strFolder = "" Then Exit Sub
    Set WkSht = ActiveSheet
    i = WkSht.Cells(WkSht.Rows.Count, 1).End(xlUp).Row
    strFile = Dir(strFolder & "\*.docx", vbNormal)
    While strFile <> ""
        i = i + 1
        Set wdDoc = wdApp.Documents.Open(Filename:=strFolder & "\" & strFile, _
          AddToRecentFiles:=False, Visible:=False)
        With wdDoc
            For j = 1 To 3
                With .Tables(1)
                    Set wdRng = .Cell(2, j).Range
                    With wdRng
                        .End = .End - 1
                        WkSht.Cells(i, j).Value = .Text
                    End With
                End With
                With .Tables(2)
                    Set wdRng = .Cell(2, j).Range
                    With wdRng
                        .End = .End - 1
                        WkSht.Cells(i, j + 3).Value = .Text
                    End With
                End With
            Next
        End With
        wdDoc.Close SaveChanges:=False
        strFile = Dir()
    Wend
    wdApp.Quit
    Set wdDoc = Nothing: Set wdApp = Nothing: Set WkSht = Nothing
    Application.ScreenUpdating = True
End Sub
 
Function GetFolder() As String
    Dim oFolder As Object
    GetFolder = ""
    Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
    If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
    Set oFolder = Nothing
End Function
Note: When you add the macro to Excel, you must set a reference to the Word object model in the VBE, via Tools>References. The macro includes its own folder browser, so all you need to do is run the macro and use the browser to select the folder to process. That folder must not contain any documents that you don't want to process.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #17  
Old 05-23-2014, 05:20 AM
chinchee chinchee is offline Copy data from Word into Excel Windows 7 64bit Copy data from Word into Excel Office 2010 64bit
Novice
Copy data from Word into Excel
 
Join Date: May 2014
Posts: 10
chinchee is on a distinguished road
Default

Hi macropod,

Thank you for the code. I will have a good go at it - will need some time to digest them. Just a few more questions which I hope you could help me with, please:

- Your GetWordTableData uses sub instead of function - from my
understanding, to return a value we need to use function. Is it not so?

Thank you.
Reply With Quote
  #18  
Old 05-23-2014, 05:24 AM
macropod's Avatar
macropod macropod is offline Copy data from Word into Excel Windows 7 32bit Copy data from Word into Excel Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Be assured, I know how to code... The code doesn't merely return a value, it does something with a whole series of values.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #19  
Old 05-23-2014, 05:25 AM
chinchee chinchee is offline Copy data from Word into Excel Windows 7 64bit Copy data from Word into Excel Office 2010 64bit
Novice
Copy data from Word into Excel
 
Join Date: May 2014
Posts: 10
chinchee is on a distinguished road
Default

Works like a charm! Thank you very much... Now i need to understand what you have done.

p/s: I have no doubt about the code. It's just for my own learning that I am asking the question.

Thank you!
Reply With Quote
Reply

Tags
copy/paste, multiple files, words



Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy Excel data into an existing Word Table JJG Word 1 12-18-2013 05:41 PM
How to Copy data from Outlook mail and Paste it in a Excel sheet? padhu1989 Outlook 0 09-11-2012 04:07 AM
how to copy all ms word tables into excel rehan129 Word 0 01-28-2012 10:17 AM
Copy data from Word into Excel How to copy automatically data from Excel file to Word file? fuchsd Word 6 10-25-2011 05:52 AM
Copy all comments & cell contents (i.e. data) to word? IanM Excel 0 07-03-2010 11:14 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:53 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft