JBNV123


Hi,

for some reason if I use the code following (changed for my spefic data of course) I do not get the desired record to display, in fact I get only the record I am currently viewing to display, ie it is as though nothing is happening. This code is supposed to search for the record with "Joe" in the "FirstName" field and display it and also store the record number of interest as numRecord. When I run it, numRecord ends up just being the record I was viewing to begin with (the number that refers to the record that I am viewing) I am using Office 2003. Thanks so much. See explanation of code below at this link: http://msdn2.microsoft.com/en-us/library/aa201484(office.10).aspx

Dim dsMain As MailMergeDataSource
Dim numRecord As Integer

ActiveDocument.MailMerge.ViewMailMergeFieldCodes = False
Set dsMain = ActiveDocument.MailMerge.DataSource
If dsMain.FindRecord(FindText:="Joe", _
Field:="FirstName") = True Then
numRecord = dsMain.ActiveRecord
End If










Re: Difficulty using the Findrecord Method

Cringing Dragon


Probably a silly question... but is there actually a record with a first name of "Joe"

btw When I responded to your previous question, I posted the link to Office 2002 help instead of 2003. I changed it afterwards. The better link is below. The example code is the same for both, so it doesn't matter.

http://msdn2.microsoft.com/en-us/library/aa172022(office.11).aspx







Re: Difficulty using the Findrecord Method

JBNV123

Hi,

Yeah, I dont undeerstand why this is nort working. To explain my siutation further I am using an Excel spreadsheet as a source file for the merge document. It has the follwoing data for my test case:
FirstName LastName
Joe Smith
Bill Jones
Bob Brown
Frank Martinez


So the field names are FirstName and LastName. Then I used the code from the link you provided me:

Dim dsMain As MailMergeDataSource
Dim numRecord As Integer

ActiveDocument.MailMerge.ViewMailMergeFieldCodes = False
Set dsMain = ActiveDocument.MailMerge.DataSource
If dsMain.FindRecord(FindText:="Joe", _
Field:="FirstName") = True Then
numRecord = dsMain.ActiveRecord
End If


I set a breakpoint in the code and stepped through. My macro never entered the code in the If statement.
I guess it
isn't recongizing the record with "Joe" as the first name. Any suggestions
By the way, thanks for your help.







Re: Difficulty using the Findrecord Method

JBNV123

And to clarify by "never entered the if statement" I mean that
If dsMain.FindRecord(FindText:="Joe", _
Field:="FirstName") = True

was never true. thanks!





Re: Difficulty using the Findrecord Method

Cringing Dragon

OK. Trying to troubleshoot. Maybe this will give us some clues.

Does it behave the same way for any search string Eg "Bob" instead of "Joe" What about a single character, such as "o"

What happens if you change the FindRecord to Field:=1 instead of using the field name

Try adding a line after you set dsMain:

MsgBox dsMain.Name

Does that display the correct data source

Change that line to

MsgBox dsMain.DataFields("FirstName").Value

Does that Display "Joe" (or whatever the first name of the active record should be)

If that fails, it could be that the field name isn't "FirstName" at all, so to try to find out what it is, try:

MsgBox dsMain.DataFields(1).Name

To try to figure out if all the records are avaibale, try adding a line after you set dsMain:

dsMain.ActiveRecord = wdNextRecord

When you step through this line, does it activate the next record, or is it still on the original record

Assuming it does activate the next record, if you continue stepping through your IF statement does it end up still on the "next" record, or does it revert to the original record If the latter, I think it means it's finding a match in the original record (although because your IF was never true in the first attempts, that would mean the active record must get excluded from the FindRecord).






Re: Difficulty using the Findrecord Method

JBNV123

Awesome, everything solved! I was not naming the field right as you expected,. Thanks for help familiarizing myself with the FidRecord method. You were a tremendous help.