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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Mail merge (labels) to word document directly from Access

Status
Not open for further replies.

kevink

Programmer
Nov 23, 1999
53
IE
I'm trying to create a mail merge document in Word directly from Access that prints out labels for each county.&nbsp;&nbsp;The user selects the county from a combo box and then hits a command button which executes the following code:<br><br>strSQL = &quot;SELECT * FROM NAMES WHERE COUNTY = & &quot; '&quot; & strCounty &&quot;'&quot;<br><br>Set objWord = CreateObject(&quot;Word.Application&quot;)<br>Set objWord = GetObject(&quot;d:\test\Labels for Printing.doc&quot;)<br>objWord.Application.Visible = True<br>objWord.MailMerge.DataSource.QueryString = strSQL<br>&nbsp;&nbsp;&nbsp;&nbsp;With objWord.MailMerge<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Destination = wdSendToNewDocument<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.MailAsAttachment = False<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.MailAddressFieldName = &quot;&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.MailSubject = &quot;&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.SuppressBlankLines = True<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;With .DataSource<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.FirstRecord = wdDefaultFirstRecord<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.LastRecord = wdDefaultLastRecord<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;End With<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Execute Pause:=True<br>&nbsp;&nbsp;&nbsp;&nbsp;End With<br><br>Needless to say, it doesn;t work!!&nbsp;&nbsp;It opens 7 (yip, 7!) instances of Access and the word document, but then does nothing else.&nbsp;&nbsp;All the vba books I have say how to insert text into a word document, but not how to create labels based on user choice!<br><br>Any ideas?<br><br>Thanks<br><br>Kevin
 
kevin,<br>I'm not sure on this, but don't you want to use:<br>objWord.open(&quot;d:\test\Labels for Printing.doc&quot;)<br><br>...instead of Getobject?...then set a Document object to this doc.<br>I'm not real up on Word OLE, but let me know if this does it...<br>--Jim
 
I use a query for the data source in Word instead.<br>1st Create query in Access that returns the results.<br>Sort them by Zip or whatever<br>I have had Excellent results with this Method.<br>Forget all of the CODE above.<br>We had a form which allowed us to pick a specific record or groups of records.<br>A Query was created which looked at a Unique field on the form ([Forms]![Launch MSWord]![Combo1]) so when you narrow down the results on the form the query had the exact same results.<br>Then a button was clicked which launched Word opened a specific document whose Datasource was the same query above.<br>Ran a macro in Word that merged and printed and quit.<br>We had LOTS of these and they ran some very complicated Word Docs (7-10 Page Proposals) with upto 75 fields of data from Access.<br><br>In Word create a new Blank doc for testing<br><br>Click Mail merge, Create Active Window, GetData Source, OPen Datasource, At the bottom in &quot;Files of Type&quot; click Microsoft Access Database.<br>In the Browse Window Find your database.<br>click the &quot;Queries&quot; TAB and find your query.<br>Click the &quot;Insert Merge Field&quot; button and add your buttons.<br>Click the &lt;&lt;ABC&gt;&gt; to see your results<br>Save your doc <br>Copy down the the whole drive and path where you saved it (you'll need in in a minute)<br>and Close Word<br>In Access make a form<br>Add a command button<br>and put this code in it<br>Do NOT use any Wizard to connect to Word.<br><br>--------------------------<br>Private Sub Command0_Click()<br>On Error GoTo Err_Command0_Click<br><br>&nbsp;&nbsp;&nbsp;&nbsp;Dim retval As Variant<br>&nbsp;&nbsp;&nbsp;&nbsp;Dim DocName, MacroName, BuildWordInfo As String<br>&nbsp;&nbsp;&nbsp;&nbsp;' The document name can be gotten from anywhere including Combobox, ListBox, Subform<br>&nbsp;&nbsp;&nbsp;&nbsp;DocName = &quot;F:\AccessMailMerge.doc&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;'Optional Macro Name<br>&nbsp;&nbsp;&nbsp;&nbsp;'MacroName = &quot;/M&quot; & &quot;YourMacro&quot;&nbsp;&nbsp;&nbsp;' &lt;&lt;&lt; Macro name cannot have any spaces in it.<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;' Launch Word<br>&nbsp;&nbsp;&nbsp;&nbsp;BuildWordInfo = &quot;C:\Microsoft Office97\Office\WinWord.exe &quot; & DocName & MacroName<br>&nbsp;&nbsp;&nbsp;&nbsp;retval = Shell(BuildWordInfo, 1)<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>Exit_Command0_Click:<br>&nbsp;&nbsp;&nbsp;&nbsp;Exit Sub<br><br>Err_Command0_Click:<br>&nbsp;&nbsp;&nbsp;&nbsp;MsgBox Err.Description<br>&nbsp;&nbsp;&nbsp;&nbsp;Resume Exit_Command0_Click<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>End Sub<br>-----------------------------<br>Ok I just tested it<br><br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top