Nah didnt work, any other suggestions?
why did you take out the other part of the code? it needs to work with that.
Main Topics
Browse All TopicsHey there I really need help with this, I have basically made a Table which list query names as records, I haev then made the querys which are all different, I then have a form which has a combo box, the combo box looks up the values of the table which lists the queries. I have made a button on the form which I would like it to create a mail merge based on the query.
this is what I have so far on the command button:
Private Sub cmd_PrevMail_Click()
Dim stDocName As String
stDocName = cboSelectMailOut
MergeIt
End Sub
The MergeIt part is a function which has this code:
Function MergeIt()
Dim objWord As Word.Document
Set objWord = GetObject("\\Stealth\DB_BA
' Make Word visible.
objWord.Application.Visibl
objWord.MailMerge.OpenData
name:=CurrentDb.name, _
LinkToSource:=True, _
Connection:="QUERY Qry_MailOuts", _
SQLStatement:="SELECT * FROM [qry_MailOuts]"
' Execute the mail merge.
objWord.MailMerge.Execute
objWord.Close
End Function
Basically the function at the moment only selects the one query (qry_MailOuts) however I would like to make it to select the query from the combo box - the function needs to be modded so it looks up the query from the combobox.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
I recorded a macro that did a mail merge on a query and here is what I come up with..it put the name of the query in quotes.
Function MergeIt()
Dim objWord As Word.Document
Set objWord = GetObject("\\Stealth\DB_BA
' Make Word visible.
objWord.Application.Visibl
objWord.MailMerge.OpenData
name:=CurrentDb.name, _
LinkToSource:=True, _
Connection:="QUERY " & me.cboQueryList , _
SQLStatement:="SELECT * FROM '" & me.cboQueryList & "'"
' Execute the mail merge.
objWord.MailMerge.Execute
objWord.Close
End Function
Mike
Okay I found out why it did that its a security feature in word 2003, anyways I still ahve the problem of when I click on the command button it runs the code perfect the word mail merge document opens and it pops up with a box saying "confirm data source" which give you options of which data it should use. Its relly annoying and if you click cancel it goes back to access and says "Runtime error "5922" - Word was unable to open the data source" - to me this seems like it doesnt recognise that it should get its data source from the query by the name of the combobox.....? if however I select the datasource from the pop up box I can select which query I can run and it works...I have ppl working for me who would not know how to do this though so I really need the mail merge to work as I jsut click on the command and then it opens the mail merge with the query selected from the combo box? any ideas?
I changed it to normal word document but when I run the command and if I select a data source it changes from normal word document to letter?
Hey DataMan, I think that your not understanding what Im trying to do - I have a form which has a combo box, the combo box gets its values from a table (the table lists all the queries I would like to have a choice of running) I select the query I want which is just text, I then click on the command button which runs the code to mail merge using the combobox text displayed... But like i said when it opens the mail merge it doesnt see that I want that query to be selected....what am I doing wrong?
I understand that...that is why I wrote the code like this
SQLStatement:="SELECT * FROM '" & me.cboQueryList & "'"
the combo box contains the name of the query. That is the same structure that was present when I recorded a mail merge macro based on a query.
You said your problem is that the word document is remembering the old datasource. Is that still the problem?
Here is what I do... take your query and using transfertext export it as a word merge file.
Then in your access code open the document and then do this
objWordApp.ActiveDocument.
ConfirmConversions:=False,
AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:="", _
WritePasswordDocument:="",
Format:=wdOpenFormatAuto, Connection:="", SQLStatement:="", SQLStatement1:=""
'Used to refresh the data in the document
'We have seen that word would remember the previous information
'and if we didn't toggle you could get the data from the
'previous letter
objWordApp.ActiveDocument.
objWordApp.ActiveDocument.
'Send the letters to the printers merged with the records
objWordApp.ActiveDocument.
objWordApp.ActiveDocument.
Hope this helps...Mike
well the combobox lists all the queries I have a selection of.....its pointless to export 1 query and run the command as the whole point of this question is to be able to have a choice of which query you would like to run on the mail merge - the mail merge is basically 9 labels of address - there are about 8 queries which all display diffferant addresses from different states in australia - like if i want to run the first query - it will display all those addresses in that state, the mail merge part is for labels to create labels based on the selection of which state do you want to print labels for...
I hope you understand....let me know if i can be mroe descriptive?
thanks
Run the transfertext code and then this code
objWordApp.ActiveDocument.
ConfirmConversions:=False,
AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:="", _
WritePasswordDocument:="",
Format:=wdOpenFormatAuto, Connection:="", SQLStatement:="", SQLStatement1:=""
'Used to refresh the data in the document
'We have seen that word would remember the previous information
'and if we didn't toggle you could get the data from the
'previous letter
objWordApp.ActiveDocument.
objWordApp.ActiveDocument.
'Send the letters to the printers merged with the records
objWordApp.ActiveDocument.
objWordApp.ActiveDocument.
Mike
Code behind Button:
Function MergeIt()
DoCmd.TransferText acExportMerge, , Me.cboSelectMailOut, "\\Stealth\DB_BACKEND\Mail
objWordApp.ActiveDocument.
ConfirmConversions:=False,
AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:="", _
WritePasswordDocument:="",
Format:=wdOpenFormatAuto, Connection:="", SQLStatement:="", SQLStatement1:=""
'Used to refresh the data in the document
'We have seen that word would remember the previous information
'and if we didn't toggle you could get the data from the
'previous letter
objWordApp.ActiveDocument.
objWordApp.ActiveDocument.
'Send the letters to the printers merged with the records
objWordApp.ActiveDocument.
objWordApp.ActiveDocument.
End Function
Private Sub cmd_PrevMail_Click()
MergeIt
End Sub
Here is the SQL for the queries:
SELECT DISTINCT STORES.STORE_INDEX, STORES_CONT.FirstName, STORES_CONT.LastName, STORES.StoreName, STORES_ADDR.StoreAddr, STORES_ADDR.StoreSub, STORES_ADDR.StoreAbbrev, STORES_ADDR.StoreZip, STORES_ADDR.StoreCoun
FROM (STORES INNER JOIN STORES_ADDR ON STORES.STORE_INDEX = STORES_ADDR.STORE_INDEX) INNER JOIN STORES_CONT ON STORES.STORE_INDEX = STORES_CONT.STORE_INDEX
WHERE (((STORES_ADDR.StoreAbbrev
ORDER BY STORES_ADDR.StoreAbbrev;
SELECT DISTINCT STORES.STORE_INDEX, STORES_CONT.FirstName, STORES_CONT.LastName, STORES.StoreName, STORES_ADDR.StoreAddr, STORES_ADDR.StoreSub, STORES_ADDR.StoreAbbrev, STORES_ADDR.StoreZip, STORES_ADDR.StoreCoun
FROM (STORES INNER JOIN STORES_ADDR ON STORES.STORE_INDEX = STORES_ADDR.STORE_INDEX) INNER JOIN STORES_CONT ON STORES.STORE_INDEX = STORES_CONT.STORE_INDEX
WHERE (((STORES_ADDR.StoreCoun)=
ORDER BY STORES_ADDR.StoreAbbrev;
SELECT DISTINCT STORES.STORE_INDEX, STORES_CONT.FirstName, STORES_CONT.LastName, STORES.StoreName, STORES_ADDR.StoreAddr, STORES_ADDR.StoreSub, STORES_ADDR.StoreAbbrev, STORES_ADDR.StoreZip, STORES_ADDR.StoreCoun
FROM (STORES INNER JOIN STORES_ADDR ON STORES.STORE_INDEX = STORES_ADDR.STORE_INDEX) INNER JOIN STORES_CONT ON STORES.STORE_INDEX = STORES_CONT.STORE_INDEX
WHERE (((STORES_ADDR.StoreAbbrev
ORDER BY STORES_ADDR.StoreAbbrev;
SELECT DISTINCT STORES.STORE_INDEX, STORES_CONT.FirstName, STORES_CONT.LastName, STORES.StoreName, STORES_ADDR.StoreAddr, STORES_ADDR.StoreSub, STORES_ADDR.StoreAbbrev, STORES_ADDR.StoreZip, STORES_ADDR.StoreCoun
FROM (STORES INNER JOIN STORES_ADDR ON STORES.STORE_INDEX = STORES_ADDR.STORE_INDEX) INNER JOIN STORES_CONT ON STORES.STORE_INDEX = STORES_CONT.STORE_INDEX
WHERE (((STORES_ADDR.StoreAbbrev
ORDER BY STORES_ADDR.StoreAbbrev;
SELECT DISTINCT STORES.STORE_INDEX, STORES_CONT.FirstName, STORES_CONT.LastName, STORES.StoreName, STORES_ADDR.StoreAddr, STORES_ADDR.StoreSub, STORES_ADDR.StoreAbbrev, STORES_ADDR.StoreZip, STORES_ADDR.StoreCoun
FROM (STORES INNER JOIN STORES_ADDR ON STORES.STORE_INDEX = STORES_ADDR.STORE_INDEX) INNER JOIN STORES_CONT ON STORES.STORE_INDEX = STORES_CONT.STORE_INDEX
WHERE (((STORES_ADDR.StoreCoun)=
ORDER BY STORES_ADDR.StoreAbbrev;
SELECT DISTINCT STORES.STORE_INDEX, STORES_CONT.FirstName, STORES_CONT.LastName, STORES.StoreName, STORES_ADDR.StoreAddr, STORES_ADDR.StoreSub, STORES_ADDR.StoreAbbrev, STORES_ADDR.StoreZip, STORES_ADDR.StoreCoun
FROM (STORES INNER JOIN STORES_ADDR ON STORES.STORE_INDEX = STORES_ADDR.STORE_INDEX) INNER JOIN STORES_CONT ON STORES.STORE_INDEX = STORES_CONT.STORE_INDEX
WHERE (((STORES_ADDR.StoreAbbrev
ORDER BY STORES_ADDR.StoreAbbrev;
SELECT DISTINCT STORES.STORE_INDEX, STORES_CONT.FirstName, STORES_CONT.LastName, STORES.StoreName, STORES_ADDR.StoreAddr, STORES_ADDR.StoreSub, STORES_ADDR.StoreAbbrev, STORES_ADDR.StoreZip, STORES_ADDR.StoreCoun
FROM (STORES INNER JOIN STORES_ADDR ON STORES.STORE_INDEX = STORES_ADDR.STORE_INDEX) INNER JOIN STORES_CONT ON STORES.STORE_INDEX = STORES_CONT.STORE_INDEX
WHERE (((STORES_ADDR.StoreAbbrev
ORDER BY STORES_ADDR.StoreAbbrev;
SELECT DISTINCT STORES.STORE_INDEX, STORES_CONT.FirstName, STORES_CONT.LastName, STORES.StoreName, STORES_ADDR.StoreAddr, STORES_ADDR.StoreSub, STORES_ADDR.StoreAbbrev, STORES_ADDR.StoreZip, STORES_ADDR.StoreCoun
FROM (STORES INNER JOIN STORES_ADDR ON STORES.STORE_INDEX = STORES_ADDR.STORE_INDEX) INNER JOIN STORES_CONT ON STORES.STORE_INDEX = STORES_CONT.STORE_INDEX
WHERE (((STORES_ADDR.StoreAbbrev
ORDER BY STORES_ADDR.StoreAbbrev;
SELECT DISTINCT STORES.STORE_INDEX, STORES_CONT.FirstName, STORES_CONT.LastName, STORES.StoreName, STORES_ADDR.StoreAddr, STORES_ADDR.StoreSub, STORES_ADDR.StoreAbbrev, STORES_ADDR.StoreZip, STORES_ADDR.StoreCoun
FROM (STORES INNER JOIN STORES_ADDR ON STORES.STORE_INDEX = STORES_ADDR.STORE_INDEX) INNER JOIN STORES_CONT ON STORES.STORE_INDEX = STORES_CONT.STORE_INDEX
WHERE (((STORES_ADDR.StoreAbbrev
ORDER BY STORES_ADDR.StoreAbbrev;
SELECT DISTINCT STORES.STORE_INDEX, STORES_CONT.FirstName, STORES_CONT.LastName, STORES.StoreName, STORES_ADDR.StoreAddr, STORES_ADDR.StoreSub, STORES_ADDR.StoreAbbrev, STORES_ADDR.StoreZip, STORES_ADDR.StoreCoun
FROM (STORES INNER JOIN STORES_ADDR ON STORES.STORE_INDEX = STORES_ADDR.STORE_INDEX) INNER JOIN STORES_CONT ON STORES.STORE_INDEX = STORES_CONT.STORE_INDEX
WHERE (((STORES_ADDR.StoreAbbrev
ORDER BY STORES_ADDR.StoreAbbrev;
And thats all HEHE.....anyways thanks for helping.
The code I showed you, is what I use.
I export the records to a word merge file
I open word in VBA (from Access)
Open the word file (the one with the merge field) - no datasource
Tell the document it is a mail merge and the data source
Merge to printer
I've been doing it for years....no problems.
Are you sure that your word document doesn't have a data source? It should only contain merge fields.
Mike
Here is all the code behind the form. You were exporting the data to the same file name as the word document that contained your merge fields. I changed the export file name.
Option Compare Database
Option Explicit
Public Sub sbMergeIt()
Dim strPath As String
'Create the object variables need
'Make sure there is a reference to MS Word
Dim objWordDoc As Word.Document
Dim objWordApp As Word.Application
'Set the variable that holds the path to the files used in the procedure
'Change it to match your path \\Stealth\DB_BACKEND\
'strPath = "C:\Documents and Settings\Administrator\My Documents\Access Databases\"
strPath = "\\Stealth\DB_BACKEND\"
'Export the data from the selected query. Notice the file name is not the same as the word document we are trying to merge it with
DoCmd.TransferText acExportMerge, , Me.cboSelectMailOut, strPath & "MailOutMergeData.txt", True
'Create our object variable
Set objWordApp = CreateObject("Word.Applica
'Open the word document
Set objWordDoc = objWordApp.Documents.Open(
objWordApp.ActiveDocument.
ConfirmConversions:=False,
AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:="", _
WritePasswordDocument:="",
Format:=wdOpenFormatAuto, Connection:="", SQLStatement:="", SQLStatement1:=""
'Used to refresh the data in the document
'We have seen that word would remember the previous information
'and if we didn't toggle you could get the data from the
'previous letter
objWordApp.ActiveDocument.
objWordApp.ActiveDocument.
'Send the letters to the printers merged with the records
objWordApp.ActiveDocument.
'If you want it to be automatic, then uncomment the next 3 lines and comment the visible line
objWordApp.Visible = True
'objWordApp.ActiveDocument
'objWordApp.Quit wdDoNotSaveChanges
'Set objWordApp = Nothing
End Sub
Private Sub cmd_PrevMail_Click()
Call sbMergeIt
MsgBox "List exported to printer"
End Sub
Private Sub cmd_Close_Click()
Quit
End Sub
Business Accounts
Answer for Membership
by: Data-ManPosted on 2004-07-27 at 20:34:08ID: 11653115
I think you can just replace the select statement with the value of the combo box
CKEND\Mail OutList.do c", "Word.Document") e = True Source _
Function MergeIt()
Dim objWord As Word.Document
Set objWord = GetObject("\\Stealth\DB_BA
' Make Word visible.
objWord.Application.Visibl
objWord.MailMerge.OpenData
name:=CurrentDb.name, _
LinkToSource:=True, _
Connection:="QUERY " & Me.cboQueryList
' Execute the mail merge.
objWord.MailMerge.Execute
objWord.Close
End Function
mike