Mailmerge hangs at OpenDataSource

J

John G

I have the follow app that always hangs when i get to the
OpenDataSource method. Any Ideas? Thanks in Advance

Word.Application wrdApp;
Word.Document oDataDoc;
Word.MailMerge wrdMailMerge;
Object oMissing = System.Type.Missing;
Object oFalse = false;
Object oTrue = true;
Object oName = "C:\\Documents and Settings\\John\\Desktop\
\EDP\\cardprocess\\idcardsmmeastern-x4.doc";
Object oFileName = @"c:\saved.rtf";
Object oFileFormat = Word.WdSaveFormat.wdFormatRTF;
// Starting the Word Application

wrdApp = new Word.ApplicationClass();
wrdApp.Visible = true;
wrdApp.WindowState =
Word.WdWindowState.wdWindowStateMaximize;

// Open the template
oDataDoc = wrdApp.Documents.Add(ref oName, ref oFalse, ref
oMissing, ref oMissing);
oDataDoc.Activate();

// Document 2 mailmerge format
wrdMailMerge = oDataDoc.MailMerge;
wrdMailMerge.HighlightMergeFields = false;
//wrdMailMerge.ViewMailMergeFieldCodes = 0; //this is for
showing the data instead of fieldnames
// The data part

Object oConnection = "DSN=Edentalnew"; //The DSN
connection name
Object oQuery = "SELECT top 10 mblname LastName, mbfname
FirstName FROM [lumemb]"; // The query to get data from the DSN
Object oSubType = Word.WdMergeSubType.wdMergeSubTypeWord;

//Open the data source and merge the fields
wrdMailMerge.OpenDataSource("", ref oFileFormat, ref
oMissing, ref oMissing, ref oTrue, ref oMissing, ref oMissing, ref
oMissing, ref oFalse, ref oMissing, ref oMissing, ref oConnection, ref
oQuery, ref oMissing, ref oMissing, ref oSubType);
wrdMailMerge.SuppressBlankLines = true;

// Save document
oDataDoc.SaveAs(ref oFileName, ref oFileFormat, ref
oMissing, ref oMissing, ref oTrue, ref oMissing, ref oMissing, ref
oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref
oMissing, ref oMissing, ref oMissing, ref oMissing);
oDataDoc.Saved = true;

// Unload objects from the memory
wrdMailMerge = null;
oDataDoc = null;
wrdApp = null;
 
P

Peter Jamieson

First, I would try

Object oSubType = Word.WdMergeSubType.wdMergeSubTypeWord2000;

Next, I would probably try to use a much simpler piece of SQL (SELECT * FROm
[mytable] until everything else is sorted out.

Next, I would check that the DSN contained everything needed for connection
(typically, username and password are not saved in the DSN even though it
sometimes appears from the DSN setup that they are. With SQL Server, you
should however be OK if you are using Windows Integrated Security.

Next, I would probably add an alias name for the /table/ and use it to
qualify each field name.
 
J

John G

Thanks Peter. I tried countless examples. I took the same example
and did it on another machine and it worked fine. Not sure what it
could be.:( Thanks for your help

First, I would try

Object oSubType = Word.WdMergeSubType.wdMergeSubTypeWord2000;

Next, I would probably try to use a much simpler piece of SQL (SELECT * FROm
[mytable] until everything else is sorted out.

Next, I would check that the DSN contained everything needed for connection
(typically, username and password are not saved in the DSN even though it
sometimes appears from the DSN setup that they are. With SQL Server, you
should however be OK if you are using Windows Integrated Security.

Next, I would probably add an alias name for the /table/ and use it to
qualify each field name.
--
Peter Jamiesonhttp://tips.pjmsn.me.uk




I have the follow app that always hangs when i get to the
OpenDataSource method. Any Ideas? Thanks in Advance
Word.Application wrdApp;
Word.Document oDataDoc;
Word.MailMerge wrdMailMerge;
Object oMissing = System.Type.Missing;
Object oFalse = false;
Object oTrue = true;
Object oName = "C:\\Documents and Settings\\John\\Desktop\
\EDP\\cardprocess\\idcardsmmeastern-x4.doc";
Object oFileName = @"c:\saved.rtf";
Object oFileFormat = Word.WdSaveFormat.wdFormatRTF;
// Starting the Word Application
wrdApp = new Word.ApplicationClass();
wrdApp.Visible = true;
wrdApp.WindowState =
Word.WdWindowState.wdWindowStateMaximize;
// Open the template
oDataDoc = wrdApp.Documents.Add(ref oName, ref oFalse, ref
oMissing, ref oMissing);
oDataDoc.Activate();
// Document 2 mailmerge format
wrdMailMerge = oDataDoc.MailMerge;
wrdMailMerge.HighlightMergeFields = false;
//wrdMailMerge.ViewMailMergeFieldCodes = 0; //this is for
showing the data instead of fieldnames
// The data part
Object oConnection = "DSN=Edentalnew"; //The DSN
connection name
Object oQuery = "SELECT top 10 mblname LastName, mbfname
FirstName FROM [lumemb]"; // The query to get data from the DSN
Object oSubType = Word.WdMergeSubType.wdMergeSubTypeWord;
//Open the data source and merge the fields
wrdMailMerge.OpenDataSource("", ref oFileFormat, ref
oMissing, ref oMissing, ref oTrue, ref oMissing, ref oMissing, ref
oMissing, ref oFalse, ref oMissing, ref oMissing, ref oConnection, ref
oQuery, ref oMissing, ref oMissing, ref oSubType);
wrdMailMerge.SuppressBlankLines = true;
// Save document
oDataDoc.SaveAs(ref oFileName, ref oFileFormat, ref
oMissing, ref oMissing, ref oTrue, ref oMissing, ref oMissing, ref
oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref
oMissing, ref oMissing, ref oMissing, ref oMissing);
oDataDoc.Saved = true;
// Unload objects from the memory
wrdMailMerge = null;
oDataDoc = null;
wrdApp = null;- Hide quoted text -

- Show quoted text -
 
P

Peter Jamieson

Does the same DSN work correctly with other software (e.g. Excel, MS Query)
on the original machine?

--
Peter Jamieson
http://tips.pjmsn.me.uk

John G said:
Thanks Peter. I tried countless examples. I took the same example
and did it on another machine and it worked fine. Not sure what it
could be.:( Thanks for your help

First, I would try

Object oSubType = Word.WdMergeSubType.wdMergeSubTypeWord2000;

Next, I would probably try to use a much simpler piece of SQL (SELECT *
FROm
[mytable] until everything else is sorted out.

Next, I would check that the DSN contained everything needed for
connection
(typically, username and password are not saved in the DSN even though it
sometimes appears from the DSN setup that they are. With SQL Server, you
should however be OK if you are using Windows Integrated Security.

Next, I would probably add an alias name for the /table/ and use it to
qualify each field name.
--
Peter Jamiesonhttp://tips.pjmsn.me.uk




I have the follow app that always hangs when i get to the
OpenDataSource method. Any Ideas? Thanks in Advance
Word.Application wrdApp;
Word.Document oDataDoc;
Word.MailMerge wrdMailMerge;
Object oMissing = System.Type.Missing;
Object oFalse = false;
Object oTrue = true;
Object oName = "C:\\Documents and Settings\\John\\Desktop\
\EDP\\cardprocess\\idcardsmmeastern-x4.doc";
Object oFileName = @"c:\saved.rtf";
Object oFileFormat = Word.WdSaveFormat.wdFormatRTF;
// Starting the Word Application
wrdApp = new Word.ApplicationClass();
wrdApp.Visible = true;
wrdApp.WindowState =
Word.WdWindowState.wdWindowStateMaximize;
// Open the template
oDataDoc = wrdApp.Documents.Add(ref oName, ref oFalse, ref
oMissing, ref oMissing);
oDataDoc.Activate();
// Document 2 mailmerge format
wrdMailMerge = oDataDoc.MailMerge;
wrdMailMerge.HighlightMergeFields = false;
//wrdMailMerge.ViewMailMergeFieldCodes = 0; //this is for
showing the data instead of fieldnames
// The data part
Object oConnection = "DSN=Edentalnew"; //The DSN
connection name
Object oQuery = "SELECT top 10 mblname LastName, mbfname
FirstName FROM [lumemb]"; // The query to get data from the DSN
Object oSubType = Word.WdMergeSubType.wdMergeSubTypeWord;
//Open the data source and merge the fields
wrdMailMerge.OpenDataSource("", ref oFileFormat, ref
oMissing, ref oMissing, ref oTrue, ref oMissing, ref oMissing, ref
oMissing, ref oFalse, ref oMissing, ref oMissing, ref oConnection, ref
oQuery, ref oMissing, ref oMissing, ref oSubType);
wrdMailMerge.SuppressBlankLines = true;
// Save document
oDataDoc.SaveAs(ref oFileName, ref oFileFormat, ref
oMissing, ref oMissing, ref oTrue, ref oMissing, ref oMissing, ref
oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref
oMissing, ref oMissing, ref oMissing, ref oMissing);
oDataDoc.Saved = true;
// Unload objects from the memory
wrdMailMerge = null;
oDataDoc = null;
wrdApp = null;- Hide quoted text -

- Show quoted text -
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top