borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (Help this beginer)Next Topic (Referential Integrity) New Topic New Poll Post Reply
AndreaVB Forum : Database : Access to Word Mail Merge Automation
Poster Message
kareltje
Level: Trainee

Registered: 08-06-2005
Posts: 1

icon Access to Word Mail Merge Automation

hello,

I have a db whit a query, now when a user is clicking on a button in a form there most be print a number of letters whit some data of the query.  

I have now the following code:


Public Sub MergeToWord(strDocName As String, MyQuery As String)
Dim objApp As Object

'DoCmd.Close

'Change cursor to hourglass
DoCmd.Hourglass True

'Open Mailmerge Document
'Start Word


Set objApp = CreateObject("Word.Application")
With objApp
    .Visible = True 'Make it visible
    .Documents.Open strDocName 'Open the Mailmerge Document

    'Use the Query defined in the arguments as the datasource
     .ActiveDocument.MailMerge.OpenDataSource Name:=CurrentDb.Name, Connection:="QUERY " & MyQuery

End With

'print and close Document
With objApp
   .ActiveDocument.MailMerge.Execute Pause:=True
   .ActiveDocument.PrintOut Background:=False
   .ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges 'Avoid Saving over your template
   .Quit SaveChanges:=wdDoNotSaveChanges 'close all documents
End With


Set objApp = Nothing

ErrorHandler:
Select Case Err.Number
Case 4157
End Select

DoCmd.Hourglass False 'Cursor back to normal


the word file has all the correct merg reference and no connection.

only when i am running the code it will stop in word and ask me to select the correct table/query. How ever i give the query in the line: Connection:="QUERY " & MyQuery

I have try moste options under  ActiveDocument.MailMerge.OpenDataSource but don't get it to work. Does someone now how to get round this message form word?

Richard Karelse

08-06-2005 at 08:51 AM
View Profile Send Email to User Show All Posts | Quote Reply
iain_clark99
Level: Trainee

Registered: 17-06-2005
Posts: 1
icon Re: Access to Word Mail Merge Automation

Had the same bug and found this solution. Thanks to meloncolly http://forums.aspfree.com/archive/t-30985/mail-merge-access-am-i-on-the-right-road-

You need to use the following code...

.ActiveDocument.MailMerge.OpenDataSource Name:=CurrentDb.Name, SQLStatement:="SELECT & FROM" & MyQuery, SubType:=wdMergeSubTypeWord2000

17-06-2005 at 10:26 AM
View Profile Send Email to User Show All Posts | Quote Reply
AndreaVB Forum : Database : Access to Word Mail Merge Automation
Previous Topic (Help this beginer)Next Topic (Referential Integrity) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder