Solved

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

Posted on 2003-12-11
17
789 Views
Last Modified: 2012-05-04
Hello,

Please refer to my previous question.

http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_20815143.html

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.

thanks
pvp
0
Comment
Question by:pvp1
  • 8
  • 7
  • 2
17 Comments
 
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.):

FirstName~LastName~Address~City~PostalCode
FirstName~LastName~Address~City~PostalCode

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


Regards,
Russell

0
 

Author Comment

by:pvp1
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;
  AssignFile(tf,GetCurrentDir+'\param.dat');
  Rewrite(tf); // at this statement i am getting error
  writeln(tf,str1);
  writeln(tf,str2);
  CloseFile(tf);

could you suggest me how can i solve this error.

thanks
pvp
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 9922274

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

ERROR_SHARING_VIOLATION

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.

Regards,
Russell
0
 

Author Comment

by:pvp1
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;


  pvDoc.SaveAs(filename);

  //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;

  WordDocument1.Disconnect;
  WordApplication1.Quit;
  WordApplication1.Disconnect;
-----------------------------------------------

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.

thanks
pvp.






0
 

Author Comment

by:pvp1
ID: 9927871
hello,

        do you have any problem with question?

regards
pvp
0
 
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;
     str1,
     str2,
     p1,
     p2,
     p3,
     p4:         String;
begin

  p1:='One';
  p2:='Two';
  p3:='Three';
  p4:='Four';

  str1:='"param1"'+#9+'"param2"'+#9+'"param3"'+#9+'"param4"';
  str2:=p1+#9+p2+#9+p3+#9+p4;
  AssignFile(tf, GetCurrentDir+'\param.dat');
  Rewrite(tf);
  WriteLn(tf, str1);
  WriteLn(tf, str2);
  CloseFile(tf);

end;

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.


Regards,
Russell

0
 

Expert Comment

by:rk001
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.

regards,
pvp
0
 
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".

Russell
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Expert Comment

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

thanks
pvp
0
 

Author Comment

by:pvp1
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.

thanks
pvp


0
 
LVL 26

Accepted Solution

by:
Russell Libby earned 50 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.

Example

HeaderField1{#9}HeaderField2{#9}HeaderField3{#9}HeaderField4{#13#10}
Rec1Field1{#9}Rec1Field2{#9}Rec1Field3{#9}Rec1Field4{#13#10}
Rec2Field1{#9}Rec2Field2{#9}Rec2Field3{#9}Rec2Field4{#13#10}
etc...

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

Regards,
Russell



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

and

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

0
 

Author Comment

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

Author Comment

by:pvp1
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,
                                     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)

Thanks
pvp

       

 
0
 

Author Comment

by:pvp1
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.

thanks
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 9952405

pvp,

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,
Russell
0
 

Author Comment

by:pvp1
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.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now