[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now


Error while calling 'WordDocument1.MailMerge.OpenDataSource()' .method (Hello Russell, rllibby--> back to you)

Posted on 2003-12-11
Medium Priority
Last Modified: 2012-05-04

Please refer to my previous question.


in that time i was using 'Access Database', according to your suggestions it is working fine.

but now i had to use the Text Database(.txt), now the source code is like

         SrcName := GetCurrentDir + '\param.txt';
    EP := EmptyParam;
    ConfConv := False;
    ReadOnly := False;
    LinkToSrc := True;
    AddToRecent := False;
    PWordDoc := '';
    PWordTemplate := '';
    WritePWordDoc := '';
    WritePWordTemplate := '';
    Conn := '';
    SQL := '';
    SQL1 := '';

    pvDoc := WordDocument1.DefaultInterface as IDispatch;
    pvDoc.MailMerge.OpenDataSource(SrcName, EP, ConfConv, ReadOnly, LinkToSrc, AddToRecent, PWordDoc, PWordTemplate, EP,
      WritePWordDoc, WritePWordTemplate, Conn, SQL, SQL1);

--> the only the change is the parameter Conn := '' as it is text data source.

now the error is:
     'Word was unable to open the datasource'

 Could you suggest me how can i solve this problem.

Question by:pvp1
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 7
  • 2
LVL 26

Expert Comment

by:Russell Libby
ID: 9921041

From the MSDN:

Text Files
Delimited text and comma-separated value files are standard data formats. Almost any program that imports and exports data supports one or both, be it a Windows, DOS, or mainframe application. Up through Word 95, using text files as a data source was a fairly straightforward proposition.

But if you're merging in Office 97 (and on Office 2000 machines upgraded from an earlier version), you're likely to run into a number of problems: The individual fields aren't recognized, the names all run together as a single field, and error messages like "Word was unable to open the data source" appear. The culprit is the text ODBC driver with which Word does not communicate correctly. To link up to your data source, you have three options:

1)        uninstall the ODBC driver;

2)        activate the Select method checkbox so you can specify Word's internal text file converter as the link method (see FIGURE 1); Note: this is done when manually linking in the text file, and does not apply to this situation

3)        rename the file with an extension the ODBC driver doesn't recognize (such as .dat), so that Word automatically uses its internal text file converter.

"Virgin" Office 2000 installations shouldn't have this problem, as Microsoft didn't include the Text ODBC converter as one of the recognized link methods.

If you use a Header file to define the field names for the data source (as is often the case with data coming from a mainframe), an annoying automation obstacle you'll encounter in all versions of Word is a dialog box requesting you to confirm the field and record delimiters. For this, too, there are a couple of work-arounds:

1)        Create the Header file as a Word table, or

2)        duplicate the first line of the Header file, so that it contains two lines with the same number of fields using the same field delimiter (Note: This only works for field delimiters, not for record delimiters.):


My suggestion would be to try and rename the file to something like .dat (as the MSDN suggests) and give that a try first.



Author Comment

ID: 9922212
Hello Russell,

           thanks for response. Now i am able to open the datasource in the .dat format. But while i am trying to write the data from the delphi to the file(.dat) i am getting the error 'I/O error 32'.

the code i am using:

  str1 := '"param1"'+char(9)+'"param2"'+char(9)+'"param3"'+char(9)+'"param4"';
  str2 := p1+char(9)+p2+char(9)+p3+char(9)+p4;
  Rewrite(tf); // at this statement i am getting error

could you suggest me how can i solve this error.

LVL 26

Expert Comment

by:Russell Libby
ID: 9922274

I/O error 32 is really ---> WIN32 error code 32, which is


In other words, your mail merge (Word) has the file open and locked. You should be generating this file BEFORE you perform the automation that does the mail mege. Also, when the mail merge is done and complete, make sure you release the automation objects (variants = unassigned, interfaces = nil, etc) so that Word does not remain out there, keeping the file locked.

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Author Comment

ID: 9927189
Hello Russell!

           I have 2 applications, first is for opening the data source for some templates(.doc--> will be reports in the second app.) and the second application is to write the data into this data source, and opening the template with appropriate datasource using olecontiner for reporting purpose.

           so as you metioned i unassigned all the variants, in the first application:

              pvDoc.MailMerge.OpenDataSource(SrcName, EP, ConfConv, ReadOnly, LinkToSrc, AddToRecent, PWordDoc, PWordTemplate, EP,
      WritePWordDoc, WritePWordTemplate, Conn, SQL, SQL1);

    pvDoc.MailMerge.ViewMailMergeFieldCodes := wdToggle;


  //unassigning the pointers

  SrcName  := Unassigned;
  EP       := Unassigned;
  ConfConv := Unassigned;
  ReadOnly := Unassigned;
  LinkToSrc:= Unassigned;
  FileName := Unassigned;
  AddToRecent := Unassigned;
  PWordDoc := Unassigned;
  PWordTemplate := Unassigned;
  WritePWordDoc := Unassigned;
  WritePWordTemplate := Unassigned;
  Conn    := Unassigned;
  SQL     := Unassigned;
  SQL1    := Unassigned;
  Pause   := Unassigned;
  pvDoc   := Unassigned;


in the second application

            senddata(data will write to the data source i.e .dat )
           OleContainer1.CreateObjectFromFile(Report1.doc',false); //Report1.doc has been used in the first app, for opening the datasource.
           OleContainer1.DoVerb(ovShow); //showing the report.

In my case i had to execute the first application, which manages in opening the datasouce.... so i am running it first.

Now the problem is:
        now while running the second applicaton the error( I/O error 32 ) remains.
as you told i unassigned the variants in the first application and i tried also in the second application, olecontiner1.close;--> to release its object. But it is not functioning.

i hope you got my porplm, could you provide your suggestion.



Author Comment

ID: 9927871

        do you have any problem with question?

LVL 26

Expert Comment

by:Russell Libby
ID: 9928568

No, I don't have any problem with the question....

But if you were expecting a faster response, you should remember that we are most likely in different time zones. (I see that your response was posted at 2am my time)

In regards to the question at hand, all I can suggest is this....

Create a stand alone project/executable that does nothing more than the following:

var  tf:         TextFile;
     p4:         String;


  AssignFile(tf, GetCurrentDir+'\param.dat');
  WriteLn(tf, str1);
  WriteLn(tf, str2);


Run this program (making sure that your other 2 applications are NOT running) and make sure the code is functioning correctly. Once you have verified that the above code works correctly, step through your first program in the debugger, and run this test program at intervals to try and figure out where the file locking is taking place.
If you are still able to run the test code above without errors, after your first program is complete, then move on to your second program. The whole point of this it to try and determine where/when/and at what point the file is being opened by Word, and also to determine when Word relinquishes control over the file.



Expert Comment

ID: 9928783
I am sorry, i did not consider the time system.....

ok i will do the things what you have explained. soon i will respond you with the results.

LVL 26

Expert Comment

by:Russell Libby
ID: 9928809

No problems,

Wish I could help more with this, but without the source for both your projects its kind of like "shooting in the dark".


Expert Comment

ID: 9929619
       I can tell the proper results may be tomorrow or on monday, now it is already night here. But please fallow this question.


Author Comment

ID: 9942127
Hello Russell, back to you!!!!

             I have done in the same way, now i dont have I/O 32 error.

But now i am with new problem.

               My datasource for mailmerge is working nicely if the datasource is having only 2 lines.

for example:
            DataSource: param.dat

"P1"  "p2"  "p3"     ---> field delimiters, separted by tab (char(9))
45   12     rk        ---->Record delimiters, separted by tab

as long as names in one line and values in the other line, data source is sucessfully opening, if the values(Record elements), crosses the line and going to the next line i.e third line, i am facing the problem..
it is:
-->         Header Record Delimiters Dialogue

        i selected tab and tab for both delimiters, it is telling, 'Field delimiter can not be the same as record delimiter'. (in fact it is accepting when the data is less, thogh both delimiters are tabs)

--> if i select some thing wrong delimiter then, i am getting 'Invalid Merge Field' Dialogue
      though all the mergefields are existed.

could you suggest me how can i solve this problem.

waiting for your response.


LVL 26

Accepted Solution

Russell Libby earned 150 total points
ID: 9942324

If you are using WriteLn to write the data (each logical record) out, then you should be choosing Tab as the field delimiter (Tab char seperates each element of data in the "record"), and "(Enter)" as the record delimiter, because each record will be seperated by CRLF when the WriteLn function is called.



Also note that when building a file in this format, you should only be calling WriteLn once for each record.

Wrong Example:

WriteLn(fn, Rec1Field1, Rec1Field2)
WriteLn(fn, Rec1Field3, Rec1Field4)

The above will actually create 2 logical  records, each with 2 fields. If you can't write all the data in a single WriteLn call, then the following should be done

Write(fn, Rec1Field1, Rec1Field2)     // Write will NOT add the #13#10
WriteLn(fn, Rec1Field3, Rec1Field4) // WriteLn adds the #13#10, and terminates the record


LVL 26

Expert Comment

by:Russell Libby
ID: 9943593
Slight correction to example code above. I should have put the following in the example (skipped the tab field seps):

WriteLn(fn, Rec1Field1, #9, Rec1Field2)
WriteLn(fn, Rec1Field3, #9, Rec1Field4)


Write(fn, Rec1Field1, #9, Rec1Field2, #9)     // Write will NOT add the #13#10
WriteLn(fn, Rec1Field3, #9, Rec1Field4)       // WriteLn adds the #13#10, and terminates the record


Author Comment

ID: 9947896
thanks Russell... i will once again respond you soon!

Author Comment

ID: 9948205
Hello Russell,

          I found the problem for my problem. I am sending the data from .xml file to .dat(data source for MailMerge). I kept some times two lines (static text) as a single node in .xml file.

for eg:

         student detais
                 name --->   student detais 
name, this a xml node, in the mail merge this enter character making the problem. i will solve this problem by writing all the static text directly in the document and only the dynamic as mergefields. (so in this case i will avoid xml file)

Now the new problm is :

        I was handling the MailMerged .doc files with OLE continer.createobjfromfile(filename);    
This is consuming lot of time, to display the document(around 10 sec.).

so now i am using TRichEdit, Delphi component, for displying the documents. --> it is much convinient and easier for me :)

                 eg: RichEdit1.lines.loadfromfile(fn.rtf); //it is expecting the file in the .rtf format.

so i am saving the documents to be 'MailMerged' in the .rtf format.

the problem that i am facing(in the execution of MailMerge application) in the MailMerging of the .rtf files:

 -->    WordApplication1.Documents.OpenOld(FileName, EmptyParam, EmptyParam,
                                     EmptyParam, EmptyParam, EmptyParam,
                                     EmptyParam, EmptyParam, EmptyParam,
//At this point of execution i am getting the Dialogue : 'ConverFile' --> options RichFormatText(RTF)

the remaining code is executing perfectly i.e
          pvDoc := WordDocument1.DefaultInterface as IDispatch;
        pvDoc.MailMerge.OpenDataSource(SrcName, EP, ConfConv, ReadOnly, LinkToSrc, AddToRecent,   wordDoc, PWordTemplate, EP,
      WritePWordDoc, WritePWordTemplate, Conn, SQL, SQL1);

So Could you suggest me how can i eliminate this 'ConverFile' Dialogue, when i execute the MailMergeapplication.(it would be nice, if the application runs with out asking this dialogue)




Author Comment

ID: 9950812
Hello Russell,

         if you need more information i am ready to provide!!!

 due to this The RichEdit in the main application unable to load the updated .rtf file.

please suggest me in this regard, i am really in a need.

LVL 26

Expert Comment

by:Russell Libby
ID: 9952405


Not sure I can help any further at this point....

I'm not sure what is required in order to get this to work with rtf files, vs word's native document format. Perhaps if you re-post this latest question, someone else may have some ideas/hints/ect, because I don't have any.

Kindest Regards,

Author Comment

ID: 9955634
ok, Russell,

         i am also trying to solve my problem in another way. May be i will post the question once again! any way thanks for your cooperation.

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question