PDA

View Full Version : [SOLVED] VBA Script to extract MS Word document to Excel



zhiyong
06-23-2016, 09:19 PM
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!

snb
06-24-2016, 02:17 AM
If you are able to write such code you must be able to adapt it to your wishes.

zhiyong
06-24-2016, 02:25 AM
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.

snb
06-24-2016, 02:39 AM
My suggestion; start with

Excel VBA Programming For Dummies by John Walkenbach | 9781119077398 | Paperback | Barnes & Noble (http://www.barnesandnoble.com/w/excel-vba-programming-for-dummies-john-walkenbach/1101874584)

zhiyong
06-24-2016, 02:53 AM
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

Kenneth Hobs
06-24-2016, 05:42 AM
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/index.php?option=com_content&view=article&id=169:automate-microsoft-word-from-excel-using-vba&catid=79&Itemid=475


'TypeText method
' http://www.excelforum.com/excel-programming/650672-populate-word-document-from-excel.html#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-programming/791302-excel-to-word-paragraph-and-page-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-programming/799070-import-text-fields-from-word.html
' Content Controls
' http://www.vbaexpress.com/forum/showthread.php?t=39654


'Add Hyperlink to Bookmark
' http://www.excelforum.com/excel-programming/664078-use-excel-vba-to-add-a-hyperlink-to-a-word-document.html#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-programming-vba-macros/940687-excel-export-to-ole-object-with-user-prompted-saveas-help.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/showthread.php?48227-Macro-to-copy-multiple-tables-in-a-single-sheet-from-excel-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-programming/682014-replace-word-in-ms-word-with-varable-from-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_Generating_a_Count_of_Word_Occurrences.html


' http://www.excelforum.com/excel-programming/794297-struggling-with-a-find-replace-macro-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-programming/796614-mail-merge-from-excel.html
' http://www.excelforum.com/excel-programming/798299-print-mail-merge-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/index.php?showtopic=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/fx4r06xq4mnlan6/ElephantsRusDocs.zip?dl=0


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