Word Template automation

J

Johnny E Jensen

Hello

In the VB6 days i've created a word automation, to create Templates. I've
first created a tab seperated datafile, saved that file, and then using the
following method.

Set NewDoc = App.Documents.Open(Filename:="""" & Templatename & """")
NewDoc.MailMerge.OpenDatasource Name:="""" & MergeFilename & """"
NewDoc.MailMerge.EditMainDocument

And from here the user is now creating the template using the fields from
the datasource, it works just fine.

Now i moved to C# 2005 and have some problems.
_objApplication.Visible = true;

_objApplication.ScreenUpdating = false;

Word.Document objDocument = (Word.Document)_objApplication.Documents.Add(ref
_objMissing, ref _objTrue, ref objType, ref _objTrue);

objDocument.MailMerge.OpenDataSource(mergedatafilename, ref objFormat, ref
_objFalse, ref _objFalse, ref _objMissing, ref _objMissing, ref _objMissing,
ref _objMissing, ref _objMissing, ref _objMissing, ref _objMissing, ref
_objMissing, ref _objMissing, ref _objMissing, ref _objMissing, ref
_objMissing);

Word is displaying a dialog where i'll have to select the field delimiter
used within the datafile - how to awoid this dialog?

And after i click OK i'll get an COM exception error, Word was unable to
open the datasource.

objDocument.SaveAs(ref objName, ref objFile, ref _objMissing, ref
_objMissing, ref _objMissing, ref _objMissing, ref _objMissing, ref
_objMissing, ref _objMissing, ref _objMissing, ref _objMissing, ref
_objMissing, ref _objMissing, ref _objMissing, ref _objMissing, ref
_objMissing);

objDocument.MailMerge.EditMainDocument();

_objApplication.ScreenUpdating = true;

Could anyone please help me.
Kind regards
Johnny E Jensen
 
C

Cindy M.

Hi Johnny,

Which version of Word is involved, here, and did you use the same one with the
code from the "VB6 days" as you're working with now?

It would also help to see what you're passing in the parameter
mergedatafilename. This ought to be more than just a file name; it should
contain connection information...
In the VB6 days i've created a word automation, to create Templates. I've
first created a tab seperated datafile, saved that file, and then using the
following method.

Set NewDoc = App.Documents.Open(Filename:="""" & Templatename & """")
NewDoc.MailMerge.OpenDatasource Name:="""" & MergeFilename & """"
NewDoc.MailMerge.EditMainDocument

And from here the user is now creating the template using the fields from
the datasource, it works just fine.

Now i moved to C# 2005 and have some problems.
_objApplication.Visible = true;

_objApplication.ScreenUpdating = false;

Word.Document objDocument = (Word.Document)_objApplication.Documents.Add(ref
_objMissing, ref _objTrue, ref objType, ref _objTrue);

objDocument.MailMerge.OpenDataSource(mergedatafilename, ref objFormat, ref
_objFalse, ref _objFalse, ref _objMissing, ref _objMissing, ref _objMissing,
ref _objMissing, ref _objMissing, ref _objMissing, ref _objMissing, ref
_objMissing, ref _objMissing, ref _objMissing, ref _objMissing, ref
_objMissing);

Word is displaying a dialog where i'll have to select the field delimiter
used within the datafile - how to awoid this dialog?

And after i click OK i'll get an COM exception error, Word was unable to
open the datasource.

objDocument.SaveAs(ref objName, ref objFile, ref _objMissing, ref
_objMissing, ref _objMissing, ref _objMissing, ref _objMissing, ref
_objMissing, ref _objMissing, ref _objMissing, ref _objMissing, ref
_objMissing, ref _objMissing, ref _objMissing, ref _objMissing, ref
_objMissing);

objDocument.MailMerge.EditMainDocument();

_objApplication.ScreenUpdating = true;

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 :)
 
J

Johnny E Jensen

Hey Cindy

I'll found this article:
http://support.microsoft.com/?kbid=301659

And i got the picture now. When i first run the example i'll got an error
because the table creator only created 1 column named
'FirstName_LastName_Address_Cit' because the header was delimited with an
','. when i changed the delimiter to ';' it worked fine.

I'm using Word 2003.

Is there a property i can read for the correct delimiter to set the header?



Kind regards

Johnny E. Jensen
 
C

Cindy M.

Hi Johnny,
I'll found this article:
http://support.microsoft.com/?kbid=301659

And i got the picture now. When i first run the example i'll got an error
because the table creator only created 1 column named
'FirstName_LastName_Address_Cit' because the header was delimited with an
','. when i changed the delimiter to ';' it worked fine.

I'm using Word 2003.

Is there a property i can read for the correct delimiter to set the header?
Hmmm. First thing I'd like you to check is what's set as the argument
delimiter in Windows. For US installations it's usually a comma, for other
regions usually a semicolon.

This sounds like a problem with the ODBC driver configuration, and I can't
recall it ever being standardized as a semicolon. Tab, yes; comma, yes. And
there were problems similar to what you describe when MDAC moved from Tab to
Comma.

In any case, you can create a DSN for the ODBC driver that specifies
semicolon as the field delimiter, then specify this driver in the
OpenDataSource method for your code, rather than using the default. The
biggest "obstacle" is making sure all the machines have the DSN, but ODBC DSN
can be created programmatically (it's just not "my thing").

The other option would be to use an OLE DB connection instead of the old DSN.
You can try that in Word's user interface (OLE DB is the default in 2003) and
see if it works "right off the bat".

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 :)
 

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