Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Removing Select Table Dialog from MailMerge Macro

192 views
Skip to first unread message

Nirmal Singh

unread,
Dec 8, 2006, 8:05:41 AM12/8/06
to
I am running a macro to do a mail merge from an excel spreadsheet.

The code for the mail merge is as follows:

ActiveDocument.MailMerge.OpenDataSource Name:= _
excelsheet, ConfirmConversions:=False, ReadOnly:= _
False, LinkToSource:=True, AddToRecentFiles:=False, _
Revert:=False, Format:=wdOpenFormatAuto, Connection:= _
"Sheet1", SQLStatement:="", SQLStatement1:=""

This pops up a dialog box to select the table for the merge. I want to use
the data in Sheet1.

How can I get rid of this dialog within my code?


Nirmal Singh


Peter Jamieson

unread,
Dec 9, 2006, 5:05:40 AM12/9/06
to
Assuming you're using Word 2002/2003 and you want to connect using the
default method (OLEDB), the following should do the trick:

ActiveDocument.MailMerge.OpenDataSource _
Name:=excelsheet, _
Connection:="", _
SQLStatement:="SELECT * FROM `Sheet1$`"

(If you macro record the data source setup, you will see that Word actually
inserts a truncated Connection string, but you shouldn't need it)

Peter Jamieson

"Nirmal Singh" <nirmal...@notthisbittowerhamlets.gov.uk> wrote in message
news:%23vIpRms...@TK2MSFTNGP04.phx.gbl...

Nirmal Singh

unread,
Dec 13, 2006, 5:11:54 AM12/13/06
to
Peter,

Sorry for the late response, I've been on leave.

I have tried your suggestion, but it now comes up with a confirm datasource
dialog. This has, amongst other options, OLEDB.

Can I get rid of this too?

"Peter Jamieson" <p...@KillmapSpjjnet.demon.co.uk> wrote in message
news:OXyarm3...@TK2MSFTNGP04.phx.gbl...

Peter Jamieson

unread,
Dec 13, 2006, 6:00:07 AM12/13/06
to
Some possibilities:
a. ensure the sheet is not already open in Excel. If it needs to be, you'll
have to connect using the old DDE method instead (see below)
b. Did you use backquotes around the sheet name (`Sheet1$`, not 'Sheet1$')
?
c. You may need the connection string, e.g. something like

ActiveDocument.MailMerge.OpenDataSource _
Name:=excelsheet, _

Connection:="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" _
& excelsheet _
&";Mode=Read;Extended Properties=""HDR=YES;IMEX=1;"";Jet
OLEDB:Engine Type=35;", _


SQLStatement:="SELECT * FROM `Sheet1$`"

You may also need a "subtype" (these are not very well documented), e.g.

ActiveDocument.MailMerge.OpenDataSource _
Name:=excelsheet, _

Connection:="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" _
& excelsheet _
&";Mode=Read;Extended Properties=""HDR=YES;IMEX=1;"";Jet
OLEDB:Engine Type=35;", _
SQLStatement:="SELECT * FROM `Sheet1$`", _
Subtype:=wdMergeSubTypeOther

(or maybe wdMergeSubTypeAccess)

If you need to connect using DDE, you can try

ActiveDocument.MailMerge.OpenDataSource _
Name:=excelsheet, _

Connection:="Sheet1$", _
SQLStatement:="", _
Subtype:=wdMergeSubTypeWord2000

but you will probably only be able to access the first sheet in the
workbook, or the sheet that was open when the workbook was last saved

If none of the above achieve what you want, can you
a. macro record you you're doing and post the OpenDataSource code here.
Beware - Word doesn't always record an OpenDataSource that will work how you
expect when you execute the recorded macro.
b. ensure that you can open the data source manually using OLEDB (or
whatever it is you want to use).

(NB, you may not be able to do (a) in Word 2002)

Peter Jamieson

"Nirmal Singh" <nirmal...@notthisbittowerhamlets.gov.uk> wrote in message

news:eSjIf8pH...@TK2MSFTNGP06.phx.gbl...

Nirmal Singh

unread,
Dec 15, 2006, 9:43:29 AM12/15/06
to
Peter,

Thanks for your help. Suggestion b worked!

Nirmal


"Peter Jamieson" <p...@KillmapSpjjnet.demon.co.uk> wrote in message

news:eSW79WqH...@TK2MSFTNGP03.phx.gbl...

Peter Jamieson

unread,
Dec 16, 2006, 11:13:08 AM12/16/06
to
Good - thanks for the feedback.

Peter Jamieson
"Nirmal Singh" <nirmal...@notthisbittowerhamlets.gov.uk> wrote in message

news:eQZUldFI...@TK2MSFTNGP02.phx.gbl...

0 new messages