here is some code using late binding (declare objects, not word.application, etc.). From Excel 2003 ,
- opens a WORD document
- looking for the string "minimum margin"
- moves the cursor a few lines / words further
- extends / selects the cursor WORD
- inserts this WORD selection into EXCEL
steps 2-5 are repeated for βReport Period:β (note that β:β is the word boundary, so we need to jump 8 words to the right to arrive at the date)
For WORD, I copied the text from your Q just as it is (no table, just text). If you use tables instead, you may need to play with units of various Move statements (for example, for unit:=12 cells unit:=12 ); the strategy remains the same: find the constant text, move the cursor to the final destination, expand the selection, create a range of words and drag.
Both elements are placed in the current cell in Excel and its right neighbor.
Sub GrabUsage() Dim FName As String, FD As FileDialog Dim WApp As Object, WDoc As Object, WDR As Object Dim ExR As Range Set ExR = Selection ' current location in Excel Sheet 'let select the WORD doc Set FD = Application.FileDialog(msoFileDialogOpen) FD.Show If FD.SelectedItems.Count <> 0 Then FName = FD.SelectedItems(1) Else Exit Sub End If ' open Word application and load doc Set WApp = CreateObject("Word.Application") ' WApp.Visible = True Set WDoc = WApp.Documents.Open(FName) ' go home and search WApp.Selection.HomeKey Unit:=6 WApp.Selection.Find.ClearFormatting WApp.Selection.Find.Execute "Minimum Stock" ' move cursor from find to final data item WApp.Selection.MoveDown Unit:=5, Count:=1 WApp.Selection.MoveRight Unit:=2, Count:=2 ' the miracle happens here WApp.Selection.MoveRight Unit:=2, Count:=1, Extend:=1 ' grab and put into excel Set WDR = WApp.Selection ExR(1, 1) = WDR ' place at Excel cursor 'repeat WApp.Selection.HomeKey Unit:=6 WApp.Selection.Find.ClearFormatting WApp.Selection.Find.Execute "Period of Report:" WApp.Selection.MoveRight Unit:=2, Count:=8 WApp.Selection.MoveRight Unit:=2, Count:=3, Extend:=1 Set WDR = WApp.Selection ExR(1, 2) = WDR ' place in cell right of Excel cursor WDoc.Close WApp.Quit End Sub
You can create a button and call this south from there, or associate GrabUsage () with a function key.
I commented on WApp.Visible = True , because during the production process you do not want the WORD to even appear, but you will need it for debugging and playback with cursor movements.
The disadvantage of late linking (and not using links to the Word library) is the hard coding of units (6 = history, 5 = string, 2 = word) instead of using Word enumerations, but sometimes I get OS crashes with early binding .... not very sexy but it seems to work.
The FileDialog object requires a link to the MS Office Office library. AFAIK is the standard in Excel 2003, but it's better to check than crash.
And I did not include the code to check if the elements were really found; I leave it for your creativity.
Hope this helps.