Consulting

Results 1 to 6 of 6

Thread: VBA Script to extract MS Word document to Excel

  1. #1
    VBAX Newbie
    Joined
    Jun 2016
    Posts
    3
    Location

    VBA Script to extract MS Word document to Excel

    Hi guys,

    I would need some assistance with regards to extracting some specific Content Control field in MS Word to reflect in MS Excel. Currently I have the following code

    Option Explicit
    
    Sub ImportWordTable()
    
    Dim wdDoc As Object
    Dim wdFileName As Variant
    Dim tableNo As Integer 'table number in Word
    Dim iRow As Long 'row index in Excel
    Dim iCol As Integer 'column index in Excel
    Dim resultRow As Long
    Dim tableStart As Integer
    Dim tableTot As Integer
    
    On Error Resume Next
    
    ActiveSheet.Range("A:AZ").ClearContents
    
    wdFileName = Application.GetOpenFilename("Word files (*.docx),*.docx", , _
    "Browse for file containing table to be imported")
    
    If wdFileName = False Then Exit Sub '(user cancelled import file browser)
    
    Set wdDoc = GetObject(wdFileName) 'open Word file
    
    With wdDoc
        tableNo = wdDoc.tables.Count
        tableTot = wdDoc.tables.Count
        If tableNo = 0 Then
            MsgBox "This document contains no tables", _
            vbExclamation, "Import Word Table"
        ElseIf tableNo > 1 Then
            tableNo = InputBox("This Word document contains " & tableNo & " tables." & vbCrLf & _
            "Enter the table to start from", "Import Word Table", "1")
        End If
    
        resultRow = 1
    
        For tableStart = 1 To tableTot
            With .tables(tableStart)
                'copy cell contents from Word table cells to Excel cells
                For iRow = 1 To .Rows.Count
                    For iCol = 1 To .Columns.Count
                        Cells(resultRow, iCol) = WorksheetFunction.Clean(.cell(iRow, iCol).Range.Text)
                    Next iCol
                    resultRow = resultRow + 1
                Next iRow
            End With
            resultRow = resultRow + 1
        Next tableStart
    End With
    
    End Sub
    As the above codes works for MS Word in the table, I would like to modify in such a way that I can be able to extract data outside of the MS Word table.

    Thanks guys!

  2. #2
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    If you are able to write such code you must be able to adapt it to your wishes.

  3. #3
    VBAX Newbie
    Joined
    Jun 2016
    Posts
    3
    Location
    I got this code from browsing through this forum. Things went out well, but I need some modification out of it so that it can include outside of Microsoft Word table.

  4. #4

  5. #5
    VBAX Newbie
    Joined
    Jun 2016
    Posts
    3
    Location
    Thank you for your suggestion. But I'm actually new to VBA, as I've got task on hand right now. Thus, I posted my help on this thread. Would appreciate if anyone can shred some light on making this work.

    Thanks in advance

  6. #6
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Untested:
    wdDoc.Paragraphs(wdDoc.Paragraphs.Count).Range.Copy
    Then use Excel Range commands to Paste or PasteSpecial as needed.

    Here are some threads that I bookmark for Excel<->MSWord:
    'Automate MSWord from another VBA Application like Excel
    ' http://www.globaliconnect.com/excel/...=79&Itemid=475


    'TypeText method
    ' http://www.excelforum.com/excel-prog...ml#post1946784
    ' http://www.excelforum.com/showthread.php?p=1946784
    ' http://vbaexpress.com/forum/showthread.php?p=169877
    ' http://vbaexpress.com/forum/showthread.php?t=24693
    ' http://www.excelforum.com/excel-prog...age-setup.html
    ' http://www.vbaexpress.com/forum/showthread.php?50947


    'Copy from Excel, paste to Word
    'Lucas, http://vbaexpress.com/forum/showthread.php?p=178364
    'http://www.vbaexpress.com/forum/showthread.php?50947


    'FormFields
    ' http://www.mrexcel.com/forum/showthread.php?p=1639696
    ' http://www.mrexcel.com/forum/showthread.php?t=333200
    ' http://www.excelforum.com/excel-prog...from-word.html
    ' Content Controls
    ' http://www.vbaexpress.com/forum/showthread.php?t=39654


    'Add Hyperlink to Bookmark
    ' http://www.excelforum.com/excel-prog...ml#post2006430
    'Steiner, http://www.vbaexpress.com/kb/getarticle.php?kb_id=126
    'Colin_L, http://www.mrexcel.com/forum/showthread.php?t=358054


    'Save OLEObject as MSWord Document
    ' http://vbaexpress.com/forum/showthread.php?t=21619
    ' http://vbaexpress.com/forum/showthread.php?t=24292
    ' http://www.excelforum.com/excel-prog...html?p=3336342


    'Add Table to MSWord
    ' http://vbaexpress.com/forum/showthread.php?t=23975
    ' http://vbaexpress.com/forum/showthread.php?p=168731
    ' http://www.vbaexpress.com/forum/show...el-to-word-doc


    'Import Word Tables
    'vog, http://www.mrexcel.com/forum/showthread.php?t=382541
    'Ruddles, http://www.mrexcel.com/forum/showthread.php?t=524091


    'snb, Word Tables
    ' http://www.vbaexpress.com/forum/showthread.php?t=45520
    ' http://www.vbaexpress.com/forum/showthread.php?t=46472




    'Get Optionbutton info from MSWord DOC
    ' http://vbaexpress.com/forum/showthread.php?t=22454


    'FindReplace Text
    ' http://www.excelforum.com/excel-prog...-ms-excel.html
    ' http://www.vbaexpress.com/forum/showthread.php?t=38958
    ' http://www.vbaexpress.com/forum/showthread.php?p=250215
    ' http://www.vbaexpress.com/forum/showthread.php?t=42833
    ' http://support.microsoft.com/kb/240157
    ' http://word.tips.net/T001833_Generat...currences.html


    ' http://www.excelforum.com/excel-prog...o-to-word.html


    'Bookmarks
    ' http://vbaexpress.com/forum/showthread.php?p=185718
    'Colin_L, http://www.mrexcel.com/forum/showthread.php?t=358054
    ' http://www.vbaexpress.com/forum/showthread.php?p=253277


    'Mail Merge
    ' http://www.excelforum.com/excel-prog...rom-excel.html
    ' http://www.excelforum.com/excel-prog...-document.html
    'Word 's Catalogue/Directory Mailmerge facility (the terminology depends on the Word version). _
    To see how to group records with any mailmerge data source supported by Word, _
    check out my Microsoft Word Catalogue/Directory Mailmerge Tutorial at:
    ' http://lounge.windowssecrets.com/ind...owtopic=731107
    ' or
    ' http://www.gmayor.com/Zips/Catalogue%20Mailmerge.zip
    ' Mail Merge from Excel to MSWord: ElephantsRus
    ' https://app.box.com/s/0zlydxinl10t23mifkrr
    ' and, https://www.dropbox.com/s/fx4r06xq4m...sDocs.zip?dl=0


    ' Control Word from Excel
    ' http://word.mvps.org/faqs/InterDev/C...WordFromXL.htm
    ' http://word.mvps.org/FAQs/InterDev/MakeAppInvisible.htm

Posting Permissions

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