Need tweak in program: finding next data

J

joseph.choi13

Let me post this again because I don't think I explained this well the
first go around:
The code below opens up a word doc, looks for the word "Primary Key:"
then gives me the corresponding answer. However, I require a tweak
since there are times when the word "Primary Key:" is followed by
unwanted spaces, no spaces, or tabs. It works great if there is one
space, but it doesn't work if there are no spaces after the colon, or
there are more than one space.
I have marked the problem area with UPPER CASE comments.

I have tried tweaking the code myself, but I'm new to this VBA.

' 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 _

'******PROBLEM STARTS HERE***********
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)
Edit/Delete Message
 
D

Daiya Mitchell

Hi Joseph,

This group is actually for Word on the Macintosh‹and I think you are on
Windows?

Requests for help writing code for macros are better directed to a group
that specializes in Word VBA, aka Word Programming, for instance:

[you may need to re-wrap these URLs if they are not clickable]

http://www.microsoft.com/office/community/en-us/default.mspx?dg=microsoft.pu
blic.word.vba.general&lang=en&cr=US

If just beginning with macros, this group is for beginners:

http://www.microsoft.com/communities/newsgroups/en-us/default.aspx?dg=micros
oft.public.word.vba.beginners&lang=en&cr=US

There are many more people in those groups who have the ability to answer
your question, so you will probably get a better and faster answer there. Be
sure to state the exact version of Word and OS that you are using.
 

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