PDA

View Full Version : Word table to Excel



nigel1
2019-09-16, 10:06 PM
Hello,

I'm trying to get VBA in Excel open a Word Doc and after finding a specific word to go to the 1st table below that word and copy specific cells from it back to Excel.


Sub Test()
Dim ws As Worksheet
Dim objWord AsObject
Dim i AsInteger
Dim strValue AsString
Set objWord = CreateObject("Word.Application")

objWord.Visible =True
objWord.Documents.Open "C:\Users\Nigel\Desktop\Test2.docx"

objWord.Activate

With objWord.Selection.Find
.Text ="Test"
If objWord.Selection.Find.Text >0Then
MsgBox "Found"

TableStart = Selection.Tables(1).Range.Start
Selection.GoTo wdGoToTable, wdGoToNext,1
If Selection.Tables(1).Range.Start = TableStart ThenExitSub

Selection.GoToNext wdGoToTable = a
Dim x AsLong, y AsLong
x =8: y =1
With.Tables(a)
For rowNb =1To1
For colNb =2To2
Cells(x, y)= WorksheetFunction.Clean(.Cell(rowNb, colNb).Range.Text)
y = y +1
Next colNb
y =1
x = x +1
Next rowNb
EndWith
x = x +2
Else
MsgBox "Didn't"

EndIf
EndWith

EndSub

macropod
2019-09-18, 12:00 AM
Cross-posted at:

http://www.vbaexpress.com/forum/showthread.php?65907-Word-table
https://stackoverflow.com/questions/57959335/specific-word-table-into-excel
Please read: http://www.excelguru.ca/content.php?184

macropod
2019-09-19, 05:28 AM
Now also cross-posted at: https://stackoverflow.com/questions/57991687/search-in-word-doc-from-page-5-onwards-find-specific-word-and-copy-from-the-1st