Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Mailmerge and Fax / February 2007

Tip: Looking for answers? Try searching our database.

Mail Merge to SQL Server without using an external ODC

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Graham - 21 Nov 2006 19:58 GMT
Is there any way to embed the SQL Server connection information right
into a Word document? I'd like to be able to e-mail the document to
others within the organization without having to navigate them through
the process of creating a data source. (They all have access to the SQL
server, of course.)

Thanks,

g.
Cindy M. - 24 Nov 2006 15:38 GMT
Hi Graham,

> Is there any way to embed the SQL Server connection information right
> into a Word document? I'd like to be able to e-mail the document to
> others within the organization without having to navigate them through
> the process of creating a data source. (They all have access to the SQL
> server, of course.)

Which version of Word are we discussing? And do your systems have an ODBC
driver for SQL Server installed?

I think it can't be done for OLE DB, as this also requires an external
*.odc file. ODBC can probably do it. And a macro that links the data
source when the file opens could probably work.

Another possibility (and it's what MSFT had in mind when they designed
the interface) would be to distribute the *.odc file for the connection
to everyone's "My Data Source" folder.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :-)
Graham - 24 Nov 2006 18:35 GMT
Cindy M.
wrote:
> Hi Graham,
>
> > Is there any way to embed the SQL Server connection information right
> > into a Word document?

> Which version of Word are we discussing? And do your systems have an ODBC
> driver for SQL Server installed?
[quoted text clipped - 11 lines]
> http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
> http://www.word.mvps.org

Word 2003, and all Microsoft systems have the SQL ODBC driver
installed.

The distribution path you describe would require an installer, which is
a heck of a lot of trouble to get a 25-character connection string down
to the users' machines. Honestly, I think this is another of those
situations where Microsoft didn't have *anything* in mind.

g.
Peter Jamieson - 30 Nov 2006 09:37 GMT
Did you work out how to do the ODBC connection?

> Honestly, I think this is another of those
> situations where Microsoft didn't have *anything* in mind.

I certainly don't understand why they insist that you have an external
object of some kind and don't allow DSN-less and ODC-less connections,
unless of course they were
a. worried that the end result would be loads of .doc files with embedded
plain text login/password info.
(but if you're using SQL Server integrated security that's wouldn't be an
issue anyway)
or
b. worried that then people would be able to distibute their merge
qapplications reasonably easily :-)

Peter Jamieson

> Cindy M.
> wrote:
[quoted text clipped - 28 lines]
>
> g.
Todd K. - 06 Feb 2007 16:38 GMT
Peter -

I have a similar situation.  I have code that works when the source is an
Access table (see below), but when the source changed to SQL Server it quit
working.  Is there a different protocol for SQL Server or is it probably in
the routing?:

Private Sub Document_Open()

Me.MailMerge.OpenDataSource _
Name:="I:\Applications\Databases\ProjectOne.adp", _
LinkToSource:=True, _
Connection:="TABLE TblAmendment_Ren", _
SQLStatement:="SELECT * FROM [TblAmendment_Ren]"
Me.MailMerge.Destination = wdSendToNewDocument
Me.MailMerge.Execute
Windows(Me).Close wdDoNotSaveChanges

End Sub

> Did you work out how to do the ODBC connection?
>
[quoted text clipped - 46 lines]
> >
> > g.
Peter Jamieson - 06 Feb 2007 17:46 GMT
Which version of Word?

At the moment I'd guess your code is using DDE to get the data (i.e. Access
will open and get the data).

When you say that the source changed to SQl Server do you mean that the
underlying data moved to SQL Server (and that you still have linked tables
in your .adp) or that the whole thing moved to SQL Server?

For starters, to connect directly to SQL Server you need either
a. a suitable ODBC DSN (either "machine" DSN or .dsn file) or
b. a suitable OLE DB  .udl file or
c. a suitable OLE DB .odc file

I can go into the details if necessary but maybe that will take you a step
further. As a rule, an OLE DB connection is advisable, and essential if your
SQl Server database has Unicode fields in it (which it almost certainly will
have if it was created from an Access project using the Upsizing wizard.

Connecting to SQL Server tables and views should be straightforward,
security issues permitting, but connecting to stored procedures/stored
functions is another matter.

Peter Jamieson

> Peter -
>
[quoted text clipped - 72 lines]
>> >
>> > g.
Todd K. - 06 Feb 2007 18:21 GMT
1) Word 2003
2) The whole thing will be moved to SQL Server.  So much needed to be
upgraded and changed from the current Access DB that we decided to build a
SQL Server project from scratch.  Right now it is on my CPU as a MySQL
project, but upon completion we will move it to a separate server.  The Word
documents were rebuilt as Merge documents "linked" to the SQL tables, and
that worked great from my computer but not from anyone else's.  So I figured
how to use the code below on the current Access database allowing everyone to
utilize them from their computers.  When I essentially just changed the code
in the OpenDataSource, it can't seem to find the SQL table
3) I just want to connect to a SQL Server table, no stored procedures.

> Which version of Word?
>
[quoted text clipped - 97 lines]
> >> >
> >> > g.
Peter Jamieson - 06 Feb 2007 18:55 GMT
> The Word
> documents were rebuilt as Merge documents "linked" to the SQL tables, and
> that worked great from my computer but not from anyone else's.

If you did this using a .odc, then you would have to distribute the .odc to
the other systems, and the other users would obviously have to have the
necessary permissions for the tables/views they need to access. You would
probably also have to re-issue the OpenDataSource call, for exampel in an
AutoOpen macro.

If you're using SQl Server with Windows Integrated Security, you shouldn't
have too many problems in this area. If you're not, then
a. everything gets a lot more difficult, straight away
b. I'm not even sure you will be able to make it work at all if you are
using SQL Server 2005 and the new drivers/providers that come with it. I
haven't been able to do it with SQl Server security so far.

Also, you may need to consider

http://support.microsoft.com/kb/825765

However, when you use a .odc you currently have two choices:
a. have a .odc for each table or view you want to connect to, and specify
any query information in the SQLStatement and SQLStatement1 parameters in
Word's OpenDataSource method
b. use one completely empty .odc (i.e. an empty text file) and specify
everything you need in the OpenDataSource call.

It seems likely that distributing a set of solutions based on (b) is likely
to be easier than basing them on (a).

Let's suppose you head for (b). You still have to write that OpenDataSource
call and specify where that empty .odc is. I suppose there are two possible
solutions:
a. put an empty .odc in every user's My Data Sources folder, build the
complete pathname for that file for each user and use that
b. use a completely standard pathname, e.g. c:\myodcs\empty.odc , for
everyone. can't do much harm - it's just an empty file and all the necessary
security is handled elsewhere. I hope.

As for the OpenDataSource code, with an empty.odc you will probably need
something like

ActiveDocument.MailMerge.OpenDataSource _
 Name:="c:\myodcs\empty.odc", _
 Connection:="Provider=SQLOLEDB.1;Integrated Security=SSPI;" & _
   "Persist Security Info=False;Initial Catalog=mydb;Data Source=myserver;"
_
 SQLSatatement:="SELECT FROM ""mytable"", _
 SubType:=wdMergeSubTypeOther

where "myserver" is the name of the server running your SQl Server database,
"mydb" is the name of the database you want to access and "mytable" is the
table or view from which you want to retrieve data.

You'll need a different Provider name for SQl Server 2005's native
provider...

See if that gets you any further.

Peter Jamieson

> 1) Word 2003
> 2) The whole thing will be moved to SQL Server.  So much needed to be
[quoted text clipped - 128 lines]
>> >> >
>> >> > g.
Todd K. - 06 Feb 2007 20:36 GMT
I set up a datasource just to the SQL Database and tried adjusting the code
as follows and it still says it can't find the source:

Private Sub Document_Open()

Me.MailMerge.OpenDataSource _
Name:="GOLD-10455 ProjectONESQL.odc", _
Connection:="Provider = SQLOLEDB.1;Integrated Security=SSPI;" & _
"Persist Security Info=False;Initial Catalog=ProjectOne.adp;Data
Source=QryVW_Amendment;", _
SQLStatement:="SELECT * FROM [QryVW_Amendment]", _
SubType:=wdMergeSubTypeOther

Me.MailMerge.Destination = wdSendToNewDocument
Me.MailMerge.Execute
Windows(Me).Close wdDoNotSaveChanges

End Sub

> > The Word
> > documents were rebuilt as Merge documents "linked" to the SQL tables, and
[quoted text clipped - 190 lines]
> >> >> >
> >> >> > g.
Peter Jamieson - 06 Feb 2007 21:26 GMT
When you're dealing with SQL Server,
a. there's a machine where the SQL Server instance is located. That machine
has a name, e.g. "myserver"
b. that server can have a number of databases or "catalogs", each of which
has a name, e.g. "ProjectOne". They aren't /file names/ like
"ProjectOne.adp". However, just because your Access project is called
ProjectOne.adp does not mean that whoever has set up the back-end SQL Server
database has called the database "ProjectOne" either.

What is the server name? What is the database name?

Peter Jamieson

>I set up a datasource just to the SQL Database and tried adjusting the code
> as follows and it still says it can't find the source:
[quoted text clipped - 237 lines]
>> >> >> >
>> >> >> > g.
Todd K. - 06 Feb 2007 21:28 GMT
Peter -

How do I set up the data source under c:mysource.odc instead of under "c:\my
documents\todd kirby..."?  I can't seem to do it.

- Todd

> > The Word
> > documents were rebuilt as Merge documents "linked" to the SQL tables, and
[quoted text clipped - 190 lines]
> >> >> >
> >> >> > g.
Peter Jamieson - 06 Feb 2007 21:49 GMT
Set it up wherever you can, then copy it using Windows Exploere (or
whatever). The .odc file is just a file.

If you're getting into areas of Windows etc. that are unfamiliar, better
just to keep asking the questions and bear in mind that we're all working
within a particular time zone...

Peter Jamieson

> Peter -
>
[quoted text clipped - 226 lines]
>> >> >> >
>> >> >> > g.
Todd K. - 07 Feb 2007 16:59 GMT
Success!  I set up a datasource just to the SQL Server and copied the .odc
file to a folder on my c: drive called "c:\DataSources)" and then placed the
following code in the Word Merge document:

Private Sub Document_Open()

Me.MailMerge.OpenDataSource _
Name:="c:\DataSources\GOLD-10455 ProjectONESQL.odc", _
Connection:="Provider=SQLOLEDB.1;Integrated Security=SSPI;" & _
"Persist Security Info=False;Initial Catalog=ProjectOneSQL;data
source=GOLD-10455;", _
SQLStatement:="SELECT * FROM [TblRptCompletion]", _
subType:=wdMergeSubTypeOther

Me.MailMerge.Destination = wdSendToNewDocument
Me.MailMerge.Execute
Windows(Me).Close wdDoNotSaveChanges

End Sub

If I set up everybody's computer with this folder and datasource once, I
should be able to use code in the Word documents to draw from any table/view
in the database from anybody's computer (with proper permissions of course).

THANKS!

> Set it up wherever you can, then copy it using Windows Exploere (or
> whatever). The .odc file is just a file.
[quoted text clipped - 235 lines]
> >> >> >> >
> >> >> >> > g.
Peter Jamieson - 07 Feb 2007 17:19 GMT
Good! Thanks for the feedback.

Peter Jamieson

> Success!  I set up a datasource just to the SQL Server and copied the .odc
> file to a folder on my c: drive called "c:\DataSources)" and then placed
[quoted text clipped - 293 lines]
>> >> >> >> >
>> >> >> >> > g.
Todd K. - 12 Feb 2007 21:32 GMT
I also noticed that if I put "Option Explicit" at the top of the code, I
don't get that warning "Do you want SQL to update..."  I know that has been a
problem for a lot of people using Merge documents...

> Good! Thanks for the feedback.
>
[quoted text clipped - 277 lines]
> >> >> >> >> >>
> >> >> >> >> >> Cindy Meister
Peter Jamieson - 12 Feb 2007 21:43 GMT
That's also interesting - will take a look...

Peter Jamieson
>I also noticed that if I put "Option Explicit" at the top of the code, I
> don't get that warning "Do you want SQL to update..."  I know that has
[quoted text clipped - 309 lines]
>> >> >> >> >> >>
>> >> >> >> >> >> Cindy Meister
Todd K. - 15 Feb 2007 16:58 GMT
This is kind of bizarre, but I started having some of my documents give me
the "Do you want SQL to update..." message again.  I discovered that if I set
up the mail merge first, then go back and paste the code in "on open", I
don't get the message.  However, if I paste the code in first, then go back
and set up the merge, I get the message.

> That's also interesting - will take a look...
>
[quoted text clipped - 275 lines]
> >> >> >> >> >> merge
> >> >> >> >> >> qapplications reasonably easily :-)
Peter Jamieson - 15 Feb 2007 18:55 GMT
More weird Word MailMerge-related behaviour has been reported in the last
week or so in this group than I can remember for quite a while:-) For my
part, if a problem can be worked around, I try not to dwell on it, because
investigating problems (a) isn't my job and (b) usually involves intensive
effort. But I will try to have a look at this one as well...

When you say "on open", do you mean a Document_Open() sub in either the
document or its template, an AutoOpen, or something else?

Peter Jamieson
> This is kind of bizarre, but I started having some of my documents give me
> the "Do you want SQL to update..." message again.  I discovered that if I
[quoted text clipped - 318 lines]
>> >> >> >> >> >> merge
>> >> >> >> >> >> qapplications reasonably easily :-)
Todd K. - 15 Feb 2007 19:11 GMT
Sorry, I meant in the Document_Open() sub of the Word document.  I didn't
mean for you to do any heavy research or anything, I just know you answer a
lot of the issues in this group and I thought you might find it useful.

> More weird Word MailMerge-related behaviour has been reported in the last
> week or so in this group than I can remember for quite a while:-) For my
[quoted text clipped - 273 lines]
> >> >> >> >> >> > I have a similar situation.  I have code that works when
> >> >> >> >> >> > the
Peter Jamieson - 15 Feb 2007 19:33 GMT
> Sorry, I meant in the Document_Open() sub of the Word document.

OK.

> I thought you might find it useful.

I certainly do. In some respects, the weirder, the better, precisely because
this is stuff I've never encountered myself. But there's a lot of ground to
cover...:-)

Peter Jamieson

> Sorry, I meant in the Document_Open() sub of the Word document.  I didn't
> mean for you to do any heavy research or anything, I just know you answer
[quoted text clipped - 309 lines]
>> >> >> >> >> >> > I have a similar situation.  I have code that works when
>> >> >> >> >> >> > the
Graham Mayor - 16 Feb 2007 07:10 GMT
> More weird Word MailMerge-related behaviour has been reported in the
> last week or so in this group than I can remember for quite a
> while:-)

I wonder if this is related to recent security updates? I found I had to
roll back to before the last batch to get an essential macro to run that
grabbed data from Outlook.

Signature

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor -  Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

Peter Jamieson - 16 Feb 2007 09:04 GMT
Hi Graham,

Could well be. I wish it was easier to keep track of the impact of these
changes!

Peter Jamieson
>> More weird Word MailMerge-related behaviour has been reported in the
>> last week or so in this group than I can remember for quite a
[quoted text clipped - 3 lines]
> roll back to before the last batch to get an essential macro to run that
> grabbed data from Outlook.
Graham Mayor - 16 Feb 2007 14:32 GMT
Agreed - they tend to come thick and fast :(

Signature

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor -  Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

> Hi Graham,
>
[quoted text clipped - 17 lines]
>> Word MVP web site http://word.mvps.org
>> <>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.