OLE Automation with Delphi and MS Word [Edit]

I'm working with Delphi for a while now, but it's the first time that I need to export in Word some data taken from a sqlite database.

With SQLite there are no problems, everything works as it should. With Word, also because of the lack of good part of documentation, I'm currently having troubles, in particular, in the moment when i go to split a cell of a table, depending on the number of occurrences found with a SQLite query (1 occurrence = do nothing, 2 occurrences = split the cell into two rows, 15 occurrences = split the cell into 15 rows and so on).

 When i create the table in Word, everything works fine, and so does the first column popupation with the database found occurrences.

But when I ask him to split the cell, the next iteration that parses all results found by the query raises an Ole Error "The requested member of the collection does not exist".
Particularly, without the code used to split the cells, everything is fine, the program finishes to elaborate all iterations based on the query results and the process concludes succesfully.
 WITH the splitting code, only the first iteration completes and when the second begins, the Ole Error is raised.

I believe that there is something word doesn't like with the cursor position but the same cursor, controlling the parsing with  WordApp.Visible := True stays in the same place, at least visually (i don't know if word places the visual cursor in one place and a "virtual" one, in another)

This is my code...does someone understand where am I wrong?

adm_tbl := db.GetTable('SELECT * FROM rdb_administration_names ORDER BY administrations_order;');
  for l := 1 to adm_tbl.Count do
    WordApp.Selection.ParagraphFormat.Alignment := 1;
    WordApp.Selection.Font.Color := $0000FF;
    WordApp.Selection.Font.Size := 14;
    comp_tbl := db.GetTable('SELECT * FROM rdb_compartments WHERE administration_hash = "' + adm_tbl.FieldAsString(adm_tbl.FieldIndex['administration_hash']) + '" ORDER BY compartments_order;');
    for m := 1 to comp_tbl.Count do
      WordApp.Selection.ParagraphFormat.Alignment := 0;
      WordApp.Selection.Font.Color := $0000C0;
      WordApp.Selection.Font.Size := 10;
      WordApp.Selection.TypeText('COMPARTIMENTO DI ' + comp_tbl.FieldAsString(comp_tbl.FieldIndex['compartment_name']));
      WordApp.Selection.Font.Color := $0000C0;
      WordApp.Selection.Font.Size := 8;
      WordApp.Selection.Font.Bold := True;
      plant_tbl := db.GetTable('SELECT * FROM rdb_plants INNER JOIN rdb_plant_names ON rdb_plants.plant_hash = rdb_plant_names.plant_hash AND rdb_plants.compartment_hash = "' + comp_tbl.FieldAsString(comp_tbl.FieldIndex['compartment_hash']) + '" ORDER BY rdb_plant_names.plant_name');
      WordApp.Selection.Tables.Item(1).Cell(1, 1).Range.Paragraphs.Alignment := 1;
      WordApp.Selection.Tables.Item(1).Cell(1, 1).Range.Text := 'Stazione';
      WordApp.Selection.Tables.Item(1).Cell(1, 2).Range.Paragraphs.Alignment := 1;
      WordApp.Selection.Tables.Item(1).Cell(1, 2).Range.Text := 'Apparati';
      WordApp.Selection.Tables.Item(1).Cell(1, 3).Range.Paragraphs.Alignment := 1;
      WordApp.Selection.Tables.Item(1).Cell(1, 3).Range.Text := 'Att.';
      WordApp.Selection.Tables.Item(1).Cell(1, 4).Range.Paragraphs.Alignment := 1;
      WordApp.Selection.Tables.Item(1).Cell(1, 4).Range.Text := 'Sop.';
      WordApp.Selection.Tables.Item(1).Cell(1, 5).Range.Paragraphs.Alignment := 1;
      WordApp.Selection.Tables.Item(1).Cell(1, 5).Range.Text := 'Segnalamento';
      WordApp.Selection.Tables.Item(1).Cell(1, 6).Range.Paragraphs.Alignment := 1;
      WordApp.Selection.Tables.Item(1).Cell(1, 6).Range.Text := 'PS';
      WordApp.Selection.Tables.Item(1).Cell(1, 7).Range.Paragraphs.Alignment := 1;
      WordApp.Selection.Tables.Item(1).Cell(1, 7).Range.Text := 'TC';
      WordApp.Selection.Tables.Item(1).Cell(1, 8).Range.Paragraphs.Alignment := 1;
      WordApp.Selection.Tables.Item(1).Cell(1, 8).Range.Text := 'PL';
      WordApp.Selection.Tables.Item(1).Cell(1, 9).Range.Paragraphs.Alignment := 1;
      WordApp.Selection.Tables.Item(1).Cell(1, 9).Range.Text := 'SCmt';
      WordApp.Selection.Tables.Item(1).Cell(1, 10).Range.Paragraphs.Alignment := 1;
      WordApp.Selection.Tables.Item(1).Cell(1, 10).Range.Text := 'TE';
      WordApp.Selection.Tables.Item(1).Cell(1, 11).Range.Paragraphs.Alignment := 1;
      WordApp.Selection.Tables.Item(1).Cell(1, 11).Range.Text := 'TLc';
      WordApp.Selection.Tables.Item(1).Cell(1, 12).Range.Paragraphs.Alignment := 1;
      WordApp.Selection.Tables.Item(1).Cell(1, 12).Range.Text := 'M';
      WordApp.Selection.Tables.Item(1).Cell(1, 13).Range.Paragraphs.Alignment := 1;
      WordApp.Selection.Tables.Item(1).Cell(1, 13).Range.Text := 'ID';
      WordApp.Selection.Tables.Item(1).Cell(1, 14).Range.Paragraphs.Alignment := 1;
      WordApp.Selection.Tables.Item(1).Cell(1, 14).Range.Text := 'Blocco';
      WordApp.Selection.Tables.Item(1).Cell(1, 15).Range.Paragraphs.Alignment := 1;
      WordApp.Selection.Tables.Item(1).Cell(1, 15).Range.Text := 'Note';
      pb.Max := plant_tbl.Count;
      for n := 1 to plant_tbl.Count do
        WordApp.Selection.Tables.Item(1).Cell(n+1, 1).Range.Font.Color := $000000; <--- ERROR HERE AT SECOND ITERATION
        WordApp.Selection.Tables.Item(1).Cell(n+1, 1).Range.Font.Size := 8;
        WordApp.Selection.Tables.Item(1).Cell(n+1, 1).Range.Font.Bold := True;
        WordApp.Selection.Tables.Item(1).Cell(n+1, 1).Range.Text := plant_tbl.FieldAsString(plant_tbl.FieldIndex['plant_name']);
        pb.Position := n;
        StatusBar.Panels[0].Text := 'Exporting ' + adm_tbl.FieldAsString(adm_tbl.FieldIndex['administration_name']) + '->' + comp_tbl.FieldAsString(comp_tbl.FieldIndex['compartment_name']) + '->' + plant_tbl.FieldAsString(plant_tbl.FieldIndex['plant_name']) + ' - ' + IntToStr(n) + ' of ' + IntToStr(plant_tbl.Count);
        unit_tbl := db.GetTable('SELECT * FROM rdb_controlunits WHERE plant_hash = "' + plant_tbl.FieldAsString(plant_tbl.FieldIndex['plant_hash']) + '" ORDER BY controlunit_start;');
        for int := 2 to 15 do
          WordApp.Selection.Tables.Item(1).Cell(n+1, int).Split(unit_tbl.Count, 1);

Edited by: Gabriele Lanza on Feb 18, 2015 3:52 PM
2/18/2015 11:52:41 PM
embarcadero.delphi.ole 598 articles. 1 followers. Follow

0 Replies

Similar Articles

[PageSpeed] 55
Get it on Google Play
Get it on Apple App Store


Similar Artilces:

OLE Automation with Delphi and MS Word
Hi, I'm working with Delphi for a while now, but it's the first time that I need to export in Word some data taken from a sqlite database. With SQLite there are no problems, everything works as it should. With Word, also because of the lack of good part of documentation, I'm currently having troubles, in particular, in the moment when i go to split a cell of a table, depending on the number of occurrences found with a SQLite query (1 occurrence = do nothing, 2 occurrences = split the cell into two rows, 15 occurrences = split the cell into 15 rows and so on). When i create ...

MS-Word & Powerpoint automation, using OLE automation [Edit]
Hello, Here is what I need to do, from my Delphi application : starting from a MS-Word document, create a PPT presentation simply made of a series of embedded MS-Word objects. There would be 1 slide for each page of the source document (assuming same page size & orientation on both sides). This requires to control both a WordApplication and a PowerpointApplication in parallel, and proceed basically as follows : - in Word, open the source file - In Powerpoint, create a new presentation - For each page of the Word doc, |- in Word, select from start to end of current page, then &quo...

Coverting some C# code to Delphi (OLE) [Edit]
Hi, I am developing a program that uses the Shoretel COM SDK which interfaces with the Shoretel phone system. We imported the type library into Delphi (in this instance Delphi 6, but suspect the same in XE2) and we get a component named TCtl. One of the properties of this class is OnEventReady. Adding this event generates code procedure TForm1.Ctl1EventReady(Sender: TObject); begin end; Inside this procedure then we are then supposed to retrieve the next event and process it. You get the next event using the GetNextEvent method which looks like... function GetNextEvent: OleVa...

Word automation - PrintOut
We're migrating our system from Delphi 7 to Delphi XE 2 and have encountered a problem when using Word Automation to print customer letters. In this case our Delphi 7 solution works happily on Windows 7 32 bit, but the same code raises an OLE exception when compiled in Delphi XE 2. Essentially we present the user with the standard print dialogue then issue the printout command. At this point we get an OLE exception. Dlg := WPObject.Dialogs.Item(88); WPObject.PrintOut(Background := false, range := dlg.Range, From := dlg.Fro...

Delphi OLE automation inside a windows service
Hello: I have a simple question. I did a program (a service) with a timer: every 10 minutes, this service goes to a database, take records and sends informative mail to customers using Outlook with OLE automation. This program, as standalone program, works fine. But this program, converted as service, call Outlook and the service stop. Any ideas? The service code: procedure TDistriMail.ServiceExecute(Sender: TService); // the OnExecute service begin Timer1.Enabled := True; while not Terminated do ServiceThread.ProcessRequests(True);// wait for termination ...

OLE Automation and MS Word
In evaluating the feasibility of using PB for an application that will use OLE Automation to interface with MS Word, I wrote some exploratory code to determine how efective PB would be as a 'driver' for OLE automation. I have a really simple test application that contains the following snippet of code: integer li_count string ls_bookmark li_count = iole_worddoc.CountBookmarks ls_bookmark = iole_worddoc.bookmarkname(1) ls_bookmark = iole_worddoc.bookmarkname(2) The first line executes in less then 1/2 second. The second line take...

Word ole: How to close MS word
I have ole control on the window and inset file using Ole_control.InsertFile(file_name) When I close the window I still see the word running in task manager. How do I close Word? How about looking at the Quit method? Katarina wrote: > I have ole control on the window and inset file using > Ole_control.InsertFile(file_name) > > When I close the window I still see the word running in task > manager. How do I close Word? ...

WORD OLE automation
The following code works perfectly with WORD6/Windows95/PB6, but does not work for WORD 97/NT. OLEObject lole_word string ls_mytext = "Trial text" li_rc = lole_word = CREATE OLEObject li_rc = lole_word.ConnectToNewObject("word.basic") // in NT WORD is opened INVISIBLY // It appears as a process in the Task Manager // li_rc = 0 li_rc = lole_word.FileNew("normal", 0) // Returns li_rc = NULL li_rc = lole_word.Insert(ls_mytext) // Not recognised by WORD97 li_rc = ...

SEPA components for Delphi with Source Code (Delphi 5
Hi all, in the european union change next year the Bankingformat to the SEPA Format. All peoples and companies must change the bankingssoftware and the costumer data form acountnummers in the new IBAN and BIC numbers. See: http://www.arma-it.de/shop/artikelueber.php?wgruppeid=211&wgruppe_offen=211 Functions: - generate SEPA XML'S - Calc IBAN - BIC Database (DE,AT and CH) Questions: vertrieb@arma-it.de PS: Bankinssoftware for Develpoers (Germany only) http://www.arma-it.de/shop/artikelueber.php?wgruppeid=212&wgruppe_offen=212 El 26/10/13 21:38, A...

WordPerfect: OLE automation through OLE control
OK, I connected to WordPerfect from PB6.5 by using OLEObject like: OLEObject ole_obj ole_obj= CREATE OLEobject li_rc=ole_obj.ConnectToNewObject ("WordPerfect.PerfectScript") ........ Now it accepts automation commands. But my goal is to start OLE automation through Object property of OLE control. Something like: ole_1.Activate(Offsite!) ole_obj=ole_1.Object.WordPerfect.PerfectScript ..... And that does not work though it does with Word. Does somebody know if it is possible at all to make OLE automation through OLE control and how to do that? Thanks ...

OLE and MS Word
Hello, I'm looking for a simple application that creates a Word document (*.doc) and writes some text there without opening Word and then closes the connection to that OLE object... Thanks in advance... Have you tried looking at http://www.sybase.com/detail?id=47980 Just a hint. You can search the Sybase tech docs from http://www.sybase.com/support/techdocs Good luck, Terry [TeamSybase] and Sequel the techno-kitten On 3 Feb 2004 02:40:13 -0800, "Piotr Nadolny" <pnad@o2.pl> wrote: >I'm looking for a simple application that creates ...

Word OLE automation
Would like to open word document using OLE after populating word using OLE Automation. The document should open up in print preview mode,and then using either VBA, or other code print or close from print preview. I want to supress all menus and commandbars from the word document. What code can i run from PB script to open in print preview and turn off menus and command bars, and put in two of my own command buttons. ...

Word OLE automation
I am about to embark on a PB 5.0 project that will require dynamic creation of documents for use with Word 97's mail merge capability. Being that this is my first time using OLE, as well as my first time programming Word, could anybody point me in the right direction (books, articles, web sites, etc.) to get me started? I've experimented a little with what I could find on Microsoft's and Sybase's sites, but I am looking for something more comprehensive. =============== Daniel Buki Software Developer Butte County (CA) Migrant Education cerkit@ncal.net In article...

OLE with MS Word
Hello all, Where can I find a list of Active X commands to control MS word. I'm trying to do: ole_1.Object.SaveAs("c:\test.doc") but "Save As HTML" instead. I would like to find a list of all the commands to carry out various functions. Thanks for reading! Rick This is all wriiten down in the Office developers books. Best place to search on the internet is http://msdn.microsoft.com HTH -- - Eric Aling [TeamSybase], Avalix Information Systems, The Netherlands Eric's Home & PB Site @ http://www.knoware.nl/users/cypr115 ...

Convert Delphi 7 to Delphi 2009 [Edit]
Hi! Please, I have a code done in Delphi 7... Now I want to convert Delphi 7 to Delphi 2009 but I'm having problems when compile the code. Error message: +[DCC Fatal Error] Far.dpr(9): F2063 Could not compile used unit 'System.pas'+ Please can somebody help me? Source Download (349KB): http://rapidshare.com/files/256191328/FileManager.rar.html Mirror Source Download: http://www.megaupload.com/?d=12GYLPT0 Password: delphi Thank u so much! (sorry for my bad english, it is not my native language) Edited by: loquax loquax on Jul 15, 2009 12:24 PM Sorry, bu...

New to Delphi
Hi I am new to Delphi & want to know good resources for learning to use it. Is there a list of good Delphi learning resources (books/online tutorials) somewhere? What recommendations can anyone make for someone new to Delphi (& only a hobby programmer at the moment)? A couple of weeks ago I started with DelphiXE4 trial & have Googled for a few Delphi resources but I haven't found anything very good yet for me as a beginner. I'm just struggling along trying to learn as I go! (I'm presently struggling with DragandDrop...see [http://stackoverflow.com/questions/17232...

WDK and MS-Word through OLE
Hi, I am doing OLE to MS-Word to generate some letters. WDK gives an error message 'File Not found' when executing 'LinkTo()' function. But the file does exists in the specified path. Could anybody suggest a work around? Here is the code: ls_doc_name = "c:\charts\cse0007.doc" // check whether the document is existing lb_fileexists = FileExists(ls_doc_name) CHOOSE CASE lb_fileexists CASE FALSE MessageBox("Error", ls_doc_name + " Not found",StopSign!) Return FALSE END CHOOSE //ole_docword is an OLE control with Object Type ...

OLE Word97 and ole objects vs ole controls
--------------50D299EA9D99A20C83E801AB Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit I am new to OLE automation. I'm trying to connect to a word97 document and filling forms fields with information. I'm trying to use the following code. I'm getting the following error in my log file: DateTime: 10/27/99 18:32:07 Title: System Error Message: Error Number 36. Error text = Name not found accessing external object property object. Window/Menu/Object = w_ole_test. Error Object/Control = cb_okay. Script = clicked. Line in Script = 22. User: Comments: DateTime: 10/27/99 18:32:17 Title: System Error Message: Error Number 21. Error text = Bad runtime function reference. Window/Menu/Object = w_ole_test. Error Object/Control = cb_okay. Script = clicked. Line in Script = 22. User: Comments: This is my code I"m trying to use. I've tried alot of things but I can't get anything to work. oleobject ole_encform olecontrol ole_ctrl_encform n_cst_ole lnv_ole Integer li_ret String ls_encnbr, ls_client_name, ls_clientid, ls_fundsrc, ls_encloc, ls_proovname, ls_dob, ls_encdate, ls_enctime, ls_prictr If dw_1.event pfc_retrieve() = 1 Then lnv_ole = CREATE n_cst_ole //Getting the information to populate word form fields from the Registry lnv_ole.of_get_encform_info(ls_encnbr, ls_client_name, ls_clientid, ls_fundsrc, ls_encloc, ls_proovname, ls_dob, ls_encdate, ls_enctime, ls_prictr) ...

OLE automation with MS Project.
Hi, Has any one done OLE automation from PB App to MSProject?? Just wondering what command set to use to manipulate MSProject elements from Powerbuilder. The MSProject VBA help and MSDN doesn't have necessary documentation. Any feed back / links would be highly appreciated. FYI - I am using PB8 and MS Project 2k. Thanks - Rich. Yes. I have just completed such a project, and it works well. You have to look at the VBA Help file. Contact me if you want more help: victora.reinhart@phs.com. Here is some sample code. // The following will automatically start MS Project...

OLE Automation with MS Project95
Hi. I'm using PB 6.0 and trying to use OLE automation with MS Project95. I can't seem to figure out the command to insert task information into Project from the PB app. I've searched almost everywhere for documentation on this, but have had no luck. I think it's supposed to be something like "ole_control.Tasks.Add("Task 1")", but that's not quite it, because it results in an application error. If anyone has had success with this, I would be so happy to hear from them so that I might be able to pick their brain. Thanks in advance. -- ...

OLE to MS Word for Spellchecking
I'm starting an OLE instance of Word (version 10) to take advantage of it's spell checking feature. This works fine except for when I ADD a new document to Word. The ADD function wants to be visible by default. We don't want the user to see anything except the Checkspelling dialog if it finds and word in question. I've searched around and found that there are some parameters for the ADD statement. In fact, the fourth paramater is the visibility value. I've been trying various combinations of passing parameters or a parameter string through my OLE object to Word...

Update Delphi 2005 to Delphi 2010, which do i need? [Edit]
Hello, we have one Delphi 2005 Professional here and want to upgrade to Delphi 2010, but we are a bit confused. Our distributor in Germany, ingram-micro offers several upgrade products: UPG DELPHI 2010 PRO - NAMED USER UPG DELPHI 2010 PRO - NAMED USER ELS UPG DELPHI 2010 PRO - NAMED USER FLEX RAD STUDIO 2010 PRO - UPG UPG RAD STUDIO 2010 PRO - NAMED ELC UPG RAD STUDIO 2010 PRO - NAMED FLEX UPG DELPHI 2010 PRO - CONCURRENT ELS UPG DELPHI 2010 PRO - CONCURRENT FLEX What do they mean exactly and which one do i need? At the moment delphi is installed on two PCs and we want to use ...

Word 6 OLE automation
Hi, I'm using OLE automation with Word 6 to perform a mail merge. Most of the methods work OK (FileOpen, FilePrint etc.) but some of them don't (FileClose). PB says : Error calling external object function blablabla ... The function's name is OK, and the parameters also. I've debugged the externalexception event, and the OLE server tells me that I've tried to handle an element like a function when it isn't. The bizarre thing is that, calling a macro with that instruction (FileClose) works. Perhaps the solution is putting all the code in the macro ? ...

Delphi 7 to Delphi XE: TBlobField to XML [Edit]
Hi, I'm migrating a Delphi7 application to Delphi XE. I'm using a TClientDataSet to communicate, by using a XML frame, with my server. In this TClientDataSet I'm using a TBlobField which is an array of 384 byte. The blobField is allocate by a code like this : {code} myStream : TStream; myStream := aClientDataSet.CreateBlobStream(myBlobField, bmwrite); vResult := myStream.Write(ArrayOf384Byte[0], length(ArrayOf384Byte)); //vResult = 384 => GooD ! (...) {code} For communicate with the server, we have to decode the Blobfield in XML before to sending it. We have override the ApplyUpdate. In the new applyupdate, we are converting NewValue in base64 by a code like this : {code} aXmlString := StringToBase64(myBlobField.NewValue) {code} The problem is than in Delphi 7 the length of NewValue was correct (384), but in DXE this length is random (between 50 and 250 ??) (it's as if a character was an end of string) => There is data loss, so the XML string is bad and my server is crashing. I don't understand what happened between writing in the BlobField and reading the BlobField's property "newValue"??? Why the value was correct in Delphi 7 and bad in XE? What was changed in the ClientDataSet mechanism? Is it a problem of unicode migration? Any help will be appreciated. Thanks, Gwen Edited by: Gwénaël Manceau on Oct 20, 2011 9:46 AM I solved my problem by using {code} aXmlString := Stri...

Web resources about - OLE Automation with Delphi and MS Word [Edit] - embarcadero.delphi.ole

Automation - Wikipedia, the free encyclopedia
For a hierarchical presentation of automation topics, see Outline of automation . For other uses, see Automation (disambiguation) . Automation ...

Automation World - Covering the field of industrial automation
AutomationWorld.com delivers content for the Automation Buying team, which includes Information Technology and Networking; Operations and Engineering; ...

Facebook Ads API Partner AdStage: Campaign Automation, $6.25M Series A Funding
AdStage , a Facebook ads API partner, announced Wednesday the launch of campaign automation — as well as a $6.25 million round of Series A funding. ...

Facebook ads API partner AdStage announces Series A funding, campaign automation
AdStage , a Facebook ads API partner, announced Wednesday the launch of campaign automation — as well as a $6.25 million round of Series A funding. ...

Automation World (@automationworld)
Sign in Sign up To bring you Twitter, we and our partners use cookies on our and other websites. Cookies help personalize Twitter content, tailor ...

Automation World
Automation World is your link to the why and how of manufacturing automation. The purpose of this group is to connect manufacturing engineers, ...

Nest gains Android Wear support and new features for better home automation
... up is a new feature that will help your Nest products know whether you're at home or away. Here's the deal - lots of other home automation products ...

GM acquires driverless tech startup Cruise Automation
... GM has added yet another arrow to its autonomous vehicle quiver, announcing the acquisition of San Francisco tech startup Cruise Automation. ...

Setting up Nest to work with Wink home automation hub
We have previously written about setting up lights with the Wink home automation hub – there are a number of bulbs that work with including GE ...

Hoping to catch Tesla, GM just bought Cruise Automation for $1 billion
... taking steps to ensure that the industry doesn't pass them by. Case in point: General Motors today announced its plan to acquire Cruise Automation, ...

Resources last updated: 3/22/2016 11:01:55 PM