View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Ed Ed is offline
external usenet poster
 
Posts: 399
Default Open Word and fetch specific data

Hi. This worked for me in Excel and Word XP. Better hurry with any
questions, though, 'cuz my weekend's about to start! <g
--
Ed
Chief Chef,
Kludge Code Cafe
"Spaghetti Is Our Specialty!"
'

Sub Foo_doWord()

Dim appWord As New Word.Application
Dim docWord As Word.Document
Dim rngWord As Word.Range
Dim strDoc As String
Dim numKey
Dim numTable

' Get Word doc
'strDoc = "C:\MyDocName"
strDoc = "C:\Documents and Settings\edward.millis\Desktop\TestDoc.doc"
Set docWord = appWord.Documents.Open(strDoc)
' Set Word range
Set rngWord = docWord.Content
' Find first term
rngWord.Find.Execute _
FindText:="Primary Key:", MatchWildcards:=False, _
Wrap:=wdFindStop, Forward:=True
' Reset range to get number
rngWord.Collapse wdCollapseEnd
Do
rngWord.MoveStart Unit:=wdCharacter, Count:=1
numKey = Trim(rngWord.Text)
Loop Until Right(numKey, 1) < " "
rngWord.MoveEnd Unit:=wdWord, Count:=1
' Put number into variable
numKey = Trim(rngWord.Text)
' Reset Word range
Set rngWord = docWord.Content
' Find second term
rngWord.Find.Execute _
FindText:="Table Name:", MatchWildcards:=False, _
Wrap:=wdFindStop, Forward:=True
' Reset range to get number
rngWord.Collapse wdCollapseEnd
Do
rngWord.MoveStart Unit:=wdCharacter, Count:=1
numTable = Trim(rngWord.Text)
Loop Until Right(numTable, 1) < " "
rngWord.MoveEnd Unit:=wdWord, Count:=1
' Put number into variable
numTable = Trim(rngWord.Text)

MsgBox "The Primary Key is: " & numKey & "."
MsgBox "The Table Name is: " & numTable & "."

docWord.Close wdDoNotSaveChanges
Set docWord = Nothing
appWord.Quit
Set appWord = Nothing

End Sub



"pikapika13" wrote
in message ...

Darn, did everyone leave for the weekend?
I need a macro that will open up a word document, look for two key
words: "Primary Key:" and return the number that follows this specific
word, go down the document and look for another key word: "Table Name:"
and return it's number as well.
I hope this is a simple request, and thanks much in advance.:)


--
pikapika13
------------------------------------------------------------------------
pikapika13's Profile:
http://www.excelforum.com/member.php...o&userid=10892
View this thread: http://www.excelforum.com/showthread...hreadid=566182