I have many different text files in the same format from which I need to pull some pieces of data. I'm doing this by having a list of filenames in excel which are looped through. The macro then opens the document in Word and calls a macro in Word. This macro then uses the fact that all the documents have the same format to pull the necessary strings out through moving and extending certain character lengths through the document. It does this for 3 strings per document and names them, eg the Year string will be the four character string of 2012 or 2011 or etc.
My problem lies with when the Word routine finishes and back in Excel, I want to put these strings into cells adjacent to the filename. I presume that as the Word macro has finished, the names are forgotten, or the Excel macro doesn't recognise the names as they've been created somewhere else. I've tried putting my Word code into the Excel macro (by using WordApp.Selection...) but this causes Excel to crash without fail.
Any ideas for getting what I need from Word across to Excel? Simple copy and paste looks out as I have more than one piece of data to copy. Here's my code so far; the Excel bit and then the Word bit:
Code:Sub GetDates() Dim WordApp As Word.Application Dim WordDoc As Word.Document Set WordApp = CreateObject("Word.Application") WordApp.Visible = True Z = Range("A1").End(xlDown).Row Path = "C:\" For i = 2 To Z Set WordDoc = WordApp.Documents.Open(Path & "\" & Cells(i, 1)) WordApp.Run ("GetDates") Cells(i, 2) = GotDate Cells(i, 4) = GotTime Cells(i, 3) = GotYear WordDoc.Close Next i WordApp.Quit End SubCode:Sub GetDates() With Selection.Find .Text = "Created" .Forward = True .MatchWholeWord = True .Execute End With Selection.MoveRight Unit:=wdCharacter, Count:=8 Selection.Extend With Selection.Find .Text = "*" .Forward = True .MatchWholeWord = True .Execute End With Selection.MoveLeft Unit:=wdCharacter, Count:=14 GotDate = Selection Selection.ExtendMode = False Selection.MoveRight Unit:=wdCharacter, Count:=8 Selection.Extend Selection.MoveRight Unit:=wdCharacter, Count:=8 GotTime = Selection Selection.ExtendMode = False Selection.MoveRight Unit:=wdCharacter, Count:=54 Selection.Extend Selection.MoveRight Unit:=wdCharacter, Count:=4 GotYear = Selection End Sub


Reply With Quote

