+ Reply to Thread
Results 1 to 5 of 5

Open Word and fetch specific data

  1. #1
    Forum Contributor
    Join Date
    06-21-2004
    Posts
    106

    Question Open Word and fetch specific data

    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.

  2. #2
    Ed
    Guest

    Re: 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" <[email protected]> wrote
    in message news:[email protected]...
    >
    > 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
    >




  3. #3
    Forum Contributor
    Join Date
    06-21-2004
    Posts
    106
    ha! my weekend just started too. I'll check it out after a little R and R. I appreciate the reply. Have a good one.

  4. #4
    Forum Contributor
    Join Date
    06-21-2004
    Posts
    106
    Ed,

    I didn't want this post to go by without a thanks! It works beautifully.

  5. #5
    Ed
    Guest

    Re: Open Word and fetch specific data

    Glad to help. It feels good to be able to help someone else for a change.
    I know I've received more than my fair share from the good people here.

    Ed

    "pikapika13" <[email protected]> wrote
    in message news:[email protected]...
    >
    > Ed,
    >
    > I didn't want this post to go by without a thanks! It works
    > beautifully.
    >
    >
    > --
    > pikapika13
    > ------------------------------------------------------------------------
    > pikapika13's Profile:
    > http://www.excelforum.com/member.php...o&userid=10892
    > View this thread: http://www.excelforum.com/showthread...hreadid=566182
    >




+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1