Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.word.mailmerge.fields
Graywolf Graywolf is offline
external usenet poster
 
Posts: 22
Default Merge Fields won't populate after Datasource change

If this post should be in a different forum, please let me know. I'm trying
to run a Word 2003 Mailmerge to a mailing label template. I've set up the
Mailing label mailmerge and my datasource is a stored query in an Access
database. Everything works fine with the mailmerge from Word. Now I want to
run that mailmerge from a Visual Basic 6 program, but change the datasource.
When I change the datasource I don't get anything coming out on the labels.
I'm trying to set the datasource to a query statement that I build in the
program. For my initial tests the Access database is the same and the query
statement I build is identical to the stored query statement in the database.
The fields that are returned in the queries will be identical, ultimately the
where clause will change so it will select different records. But first I
have to get the labels populated. When I run the code in VB and the new Word
doc is created with the labels the labels are blank. Here's the code and
the query statement. I recorded a Word macro to get the proper syntax for
the Opendatasource command. gsDBLocation is the fully qualified path to the
database. The msSQL variable holds the query statement. After opening the
datasource, firstrecord returns a 1 and lastrecord returns a -16 (there
should be 3 records) so it appears that the query is failing, but it's the
same as the stored query (which works) so I'm stumped. Any help would be
appreciated. Thanks in advance.

Query:
SELECT distinct REG.*, REGADDR.*, Address.* FROM REG INNER
JOIN (Address INNER JOIN REGADDR ON Address.A_ADDRCODE =
REGADDR.RA_ADDRCODE) ON REG.CODE = REGADDR.RA_REGCODE WHERE
(((REGADDR.WIRE_NOTICE)=True)) or (((REGADDR.WIRE_NOTICE_DUP)=True));

Code:
Set objWordApp = CreateObject("Word.application") 'New word.Application

If chkLabels.Value = vbChecked Then
Set objWordDoc = objWordApp.Documents.Open(C:\Labels5663.doc")
End If


objWordApp.Visible = vbTrue


With objWordDoc.MailMerge
.MainDocumentType = wdMailingLabels

.SuppressBlankLines = True

.OpenDataSource Name:=gsDBLocation, ConfirmConversions:=False,
ReadOnly:=False, LinkToSource:=True, _
AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:="", _
WritePasswordDocument:="", WritePasswordTemplate:="", Revert:=False, _
Format:=wdOpenFormatAuto, Connection:= _
"Provider=Microsoft.Jet.OLEDB.4.0;Password="""";Us er ID=Admin;Data
Source=gsDBLocation;Mode=Read;Extended Properties="""";Jet OLEDB:System
database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:" _
, SQLStatement:=msSQL, SQLStatement1:="", _
SubType:=wdMergeSubTypeAccess

objWordDoc.MailMerge.ViewMailMergeFieldCodes = vbTrue

With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute Pause:=False
End With
 
Thread Tools
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Fields to populate a document Yacbo Microsoft Word Help 2 November 14th 06 02:34 PM
CHange query/datasource mail merge is bound to Alex Mailmerge 1 September 13th 06 06:31 PM
Populate Merge Fields via VB.NET ben Mailmerge 5 June 21st 05 05:43 PM
How do I use unique fields to populate mail merge data like Word 2 PMPoohbah Mailmerge 1 March 11th 05 07:16 PM
Merge fields in word tables populate by a VBA query Saeed Tables 3 January 12th 05 09:36 PM


All times are GMT +1. The time now is 05:42 AM.

Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 Microsoft Office Word Forum - WordBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Word"