Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

macro help 1

Status
Not open for further replies.

valdosta

MIS
Dec 18, 2002
50
US
I am working on MS Word mail merge and trying to get
a field (which has multiple values) from the data
source file and then check if the substring of this field
contains a text, if true then insert some messages
into word document.

The macro code below does not work. I always have
problem with wdFirstRecord. The error message is
"requested object is not available". If I put dot in
front of it, error message is "Object required".

I am not familiar with VB. Please help!
Appreciate your help! Thank you in advance!

With DataSource
nub_conta =
ActiveDocument.MailMerge.DataSource.ActiveRecord
ActiveDocument.MailMerge.DataSource.ActiveRecord =
wdFirstRecord
For I = 1 To nub_conta
str_contact =
ActiveDocument.MailMerge.DataSource.DataFields("CONTACT_CODE")
If InStr(1, str_contact, "OL", vbTextCompare) >= 1
Then
With ActiveDocument.Paragraphs(1).Range
.InsertAfter "postcard from" & "College Outlook"
.InsertParagraphAfter
End With
End If
ActiveDocument.MailMerge.DataSource.ActiveRecord =
wdNextRecord
Next I
End With

 
My first thought is that you are getting this error because you do not have a currently acive record when running the macro. So to test if no record is active prior to this line: ActiveDocument.MailMerge.DataSource.ActiveRecord = wdFirstRecord
test if this line:
nub_conta =
ActiveDocument.MailMerge.DataSource.ActiveRecord
is returning -1, like:
If nub_conta <> -1 Then ActiveDocument.MailMerge.DataSource.ActiveRecord = wdFirstRecord
End If
 
Dr JavaJoe,
Thank you so much for quick response. I tried your suggestion and worked. Thank a million! But I still have another problem: When it find the substring, it suppose insert text &quot;College Outlook&quot; to main document but nothing happened. Would you please check the code again? Really appreciate your help!


If InStr(1, str_contact, &quot;OL&quot;, vbTextCompare) >= 1
Then
With ActiveDocument.Paragraphs(1).Range
.InsertAfter &quot;postcard from&quot; & &quot;College Outlook&quot;
.InsertParagraphAfter
End With
End If
 
first is
If InStr(1, str_contact, &quot;OL&quot;, vbTextCompare) >= 1 Then
returning true, or if you put a break point at this line
With ActiveDocument.Paragraphs(1).Range
does the code break there?
 
Dr JavaJoe,

Thank you very much for your help. Actually I never used VB before that's why it's so hard for me.

According to your second suggestion, I tried to test if I get the total recode number using:
nub_conta = ActiveDocument.MailMerge.DataSource.ActiveRecord

The message box didn't display the value -1 for nub_conta, so I guess that fuction didn't get the total number of record.

Thank you very much for your help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top