Need tweak in program: finding next data

P

pikapika13

An "Ed" helped me on this before. It would be nice for him to reply, but
I'll take any help. The code below fetches specific data for me.
However, some of the documents have extra spaces (sometimes 2,3,4 + or
even tabs)after "Primary Key:" & "Table Name:" In another situation,
there isn't a space after these terms and the code below seems to
delete the first character right after the colon.

Can someone add this modification?



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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top