+ Reply to Thread
Results 1 to 12 of 12

Importing Multiple Word Documents Into Excel

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-23-2012
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    128

    Importing Multiple Word Documents Into Excel

    I’m hoping someone can help with importing Word docs into Excel. We get sent multiple word documents a day, each is exactly the same layout. I can’t post the document here, but it is made up like the following:
    Header Form Name, always the same.

    Table 1 – 3 Columns, 2 rows
    Space
    Table 2 – 4 Columns, 2 rows
    Space
    Table 3 – 2 columns, 4 rows

    Text statement about the form, always the same.

    I’ve looked into importing this automatically with limited success. Ideally, I would like to scan a folder (or manually pick) the documents and Excel import that data into a spreadsheet. But it needs to update the spreadsheet when more are scanned for rather than make a new one. So future docs will be added to the list like a database.


    The end result would be in Excel
    Table 1 Header Rows, Table 2 Header Rows, Table 3 Header Rows
    Table 1 Content Table 2 Content Table 3 Content

    So it all imports it horizontally. Can this be done easily?

  2. #2
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,852

    Re: Importing Multiple Word Documents Into Excel

    Do these imported tables all go onto the same worksheet (in which case you presumably want only one set of header rows), or onto separate worksheets? If all go onto the same worksheet then, given that table 3 has 3 data rows, what is supposed to happen regarding the lack of data for the corresponding Excel rows for tables 1 & 2? Given that What kind of data are in the cells (e.g. a single paragraph or multiple paragraphs), and is there formatting such as bold, underline, etc. you need to preserve?
    Cheers,
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    Forum Contributor
    Join Date
    08-23-2012
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    128

    Re: Importing Multiple Word Documents Into Excel

    There is no requirement to keep any formatting no.

    Me being smart, I don't have a copy of the form to hand at the moment. So I made some mockups of what I think it should be. Hopefully if I am wrong, things can be changes easily.

    Firstly here is the document form layout.
    Just a note, I am not 100% that the 3rd table is laid out like that. As in, 3 separate entries. Can confirm Monday.

    Here is what I would like the end result to look like.

    Obviously further added documents would add in underneath using the same headers.

    All data goes into one sheet on one row.

  4. #4
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,852

    Re: Importing Multiple Word Documents Into Excel

    For what you've described, the following should suffice:
    Sub GetData()
    Application.ScreenUpdating = False
    Dim StrFolder As String, StrFile As String, StrFnd As String
    Dim wdApp As Object, wdDoc As Object, bStrt As Boolean
    Dim WkSht As Worksheet, LRow As Long, i As Long, j As Long
    StrFolder = GetFolder
    If StrFolder = "" Then Exit Sub
    Set WkSht = ThisWorkbook.Sheets("Sheet1")
    LRow = WkSht.Range("A" & WkSht.UsedRange.Rows.Count).End(xlUp).Row
    ' Test whether Word is already running.
    On Error Resume Next
    bStrt = False ' Flag to record if we start Word, so we can close it later.
    Set wdApp = GetObject(, "Word.Application")
    'Start Excel if it isn't running
    If wdApp Is Nothing Then
      Set wdApp = CreateObject("Word.Application")
      If wdApp Is Nothing Then
        MsgBox "Can't start Word.", vbExclamation
        Exit Sub
      End If
      ' Record that we've started Word.
      bStrt = True
    End If
    On Error GoTo 0
    StrFile = Dir(StrFolder & "\*.doc")
    While StrFile <> ""
      LRow = LRow + 1
      Set wdDoc = wdApp.Documents.Open(Filename:=StrFolder & "\" & StrFile, AddToRecentFiles:=False, Visible:=False, ReadOnly:=True)
      With wdDoc
        'Get the data for each table
        With .Tables(1)
          For i = 1 To 3
            WkSht.Cells(LRow, i).Value = Split(.Cell(2, i).Range.Text, vbCr)(0)
          Next
        End With
        With .Tables(2)
          For i = 1 To 4
            WkSht.Cells(LRow, i + 3).Value = Split(.Cell(2, i).Range.Text, vbCr)(0)
          Next
        End With
        With .Tables(3)
          For i = 1 To 3
            WkSht.Cells(LRow, i + 7).Value = Split(.Cell(2, i).Range.Text, vbCr)(0)
            WkSht.Cells(LRow, i + 10).Value = Split(.Cell(3, i).Range.Text, vbCr)(0)
            WkSht.Cells(LRow, i + 13).Value = Split(.Cell(4, i).Range.Text, vbCr)(0)
          Next
        End With
        'Close without saving changes
        .Close SaveChanges:=False
      End With
      StrFile = Dir()
    Wend
    If bStrt = True Then 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
    The macro includes a folder browser, so all you need do is select the folder concerned.

    Note: The macro will fail if the folder contains a document not containing the three tables you described as the first three tables in it.
    Last edited by macropod; 09-25-2016 at 02:41 AM.

  5. #5
    Forum Contributor
    Join Date
    08-23-2012
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    128

    Re: Importing Multiple Word Documents Into Excel

    Thanks for that, truly.

    I just tried it with my temp documents and it works, but oddly.
    If I import the file it places it perfectly. But if I run it again, nothing appears to happen. In fact it seems to be replacing rows 2 onwards with whatever is in the directory, rather than adding to it. Am I missing something?
    I made 10 duplicate documents which it throws in from row 2 to 11. But if I change those 10 docs and re-run the macro, rows 2 to 11 are replaced.

  6. #6
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,852

    Re: Importing Multiple Word Documents Into Excel

    Each time you run the macro, it starts adding data immediately after the last existing used row. If you re-run it with the same data files in the folder that you used on a previous data, those same files will be added again after the existing data; the existing data won't be replaced. I've just re-tested and confirmed that's what it's doing.

  7. #7
    Forum Contributor
    Join Date
    08-23-2012
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    128

    Re: Importing Multiple Word Documents Into Excel

    I think I understand. So what needs to be different? The file name or the content? Just in case the files we use are all named the same but the content is different.
    Actually, am I missing something? I tried 10 documents, then I tried the same 10 and it just replaced them, nothing was placed after the last used row.

  8. #8
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,852

    Re: Importing Multiple Word Documents Into Excel

    If you want to avoid duplicates, either:
    • wait until you have all the data before importing
    • clear all the existing data before re-importing
    • use a temporary folder to hold files that have not yet been imported, do the import from that folder, then move the imported files to another folder.

    As I said before, the macro does not replace any data.

  9. #9
    Forum Contributor
    Join Date
    08-23-2012
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    128

    Re: Importing Multiple Word Documents Into Excel

    Ok. To make sure I am doing something right and not wrong, I will list the steps and the results.

    I have a folder with 10 docs.
    The spreadsheet contains one row of headers.
    I run the macro on the directory and all 10 docs appear perfectly.

    I modify some details in the doc and duplicate it 10 times and put it in another directory. I delete the other 10, so there is 10 "new" files.
    I run the script and all the rows used (2 to 11 in this case) update with the new data, overriding the old.

    I've put the macro in a module in the workbook, not in my personal macro workbook.

    Is this how it is supposed to work?

    The solution would be to create 2 tabs, one for the temp drop of data and another with the live data. Then just cut'n'paste.
    Sorry if I am being dumb here.

    Edit: Bizarrely if I take the 10 rows of data and move them down to like row 30 and run it again, it replaces the data in the rows with data only. Rows 2 to 29 remain empty.

    Edit 2: Well I dunno what happened. I deleted all the rows of data and tried it once more. Now it works exactly like you said it does. Puzzling.

    But as I said, we can always move the data over after running it. So it has saved us a long tedious task. Thanks so much!
    Last edited by X82; 09-25-2016 at 04:26 AM.

  10. #10
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,852

    Re: Importing Multiple Word Documents Into Excel

    The macro was written to be run from the workbook you're populating, not from some other workbook (including your personal macro workbook). The 'ThisWorkbook' reference means the macro is trying to update your personal macro workbook, which I'm sure is not what you want to happen. If you want to run the macro from your personal macro workbook, change the 'ThisWorkbook' reference to 'ActiveWorkbook'.

  11. #11
    Forum Contributor
    Join Date
    08-23-2012
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    128

    Re: Importing Multiple Word Documents Into Excel

    I've just gotten into work to check my results and it appears I was wrong in my initial description of the document.
    I am unable to post a version of it at work, but I can try to describe what is different.

    Table 1, 3 columns and 2 rows. First row is headers
    Table 2, 4 columns and 2 rows. First row is headers
    Table 3, 2 columns and 2 rows. First row is headers
    Table 4, 2 columns and 4 rows. First column is headers.

    Apologies to be way off with my initial description.

  12. #12
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,852

    Re: Importing Multiple Word Documents Into Excel

    Change:
        With .Tables(3)
          For i = 1 To 3
            WkSht.Cells(LRow, i + 7).Value = Split(.Cell(2, i).Range.Text, vbCr)(0)
            WkSht.Cells(LRow, i + 10).Value = Split(.Cell(3, i).Range.Text, vbCr)(0)
            WkSht.Cells(LRow, i + 13).Value = Split(.Cell(4, i).Range.Text, vbCr)(0)
          Next
        End With
    to:
        With .Tables(3)
          For i = 1 To 2
            WkSht.Cells(LRow, i + 7).Value = Split(.Cell(2, i).Range.text, vbCr)(0)
          Next
        End With
        With .Tables(4)
          For i = 1 To 4
            WkSht.Cells(LRow, i + 9).Value = Split(.Cell(i, 2).Range.text, vbCr)(0)
          Next
        End With

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Importing Multiple text documents into Different Tabs in Excel
    By jzy2101 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-08-2014, 09:40 AM
  2. [SOLVED] Importing Data from Multiple Excel Documents Into a Single File (take 2)
    By YvonH in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-24-2013, 08:02 PM
  3. Importing Data from Multiple Excel Documents Into a Single File MAC Issues
    By Kal_2013 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 01-04-2013, 04:16 PM
  4. Replies: 10
    Last Post: 11-27-2012, 11:08 AM
  5. Importing Data from Multiple Excel Documents Into a Single File
    By DinahMarcinko in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 09-07-2012, 04:41 PM
  6. Importing Data from Multiple Excel Documents Into a Single File
    By DinahMarcinko in forum Excel General
    Replies: 2
    Last Post: 09-06-2012, 04:11 PM
  7. [SOLVED] importing and exporting documents using word/excel
    By Denise Camara in forum Excel General
    Replies: 0
    Last Post: 07-07-2006, 02:35 PM

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.6.0 RC 1