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


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.

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Russell LibbySoftware Engineer, Advisory Commented:

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.


pvp1Author Commented:
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.

Russell LibbySoftware Engineer, Advisory Commented:

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.

OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

pvp1Author Commented:
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.


pvp1Author Commented:

        do you have any problem with question?

Russell LibbySoftware Engineer, Advisory Commented:

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.


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.

Russell LibbySoftware Engineer, Advisory Commented:

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".

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

pvp1Author Commented:
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.


Russell LibbySoftware Engineer, Advisory Commented:

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



Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Russell LibbySoftware Engineer, Advisory Commented:
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

pvp1Author Commented:
thanks Russell... i will once again respond you soon!
pvp1Author Commented:
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)



pvp1Author Commented:
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.

Russell LibbySoftware Engineer, Advisory Commented:


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,
pvp1Author Commented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.