Mathematical formulas from Pascal to MS Word lazarus / 21-General ---------------------------------------------------------------------- PiotrAmator · 2021-02-04 09:19 I need to create a program (in Lazarus) that will generate math formulas in MS Word (in VBA it is OMath). Does anyone know how to do this or where to look for it? If possible, please also provide examples. The screen shows an example of a mathematical formula that I would like to create with my program. ---------------------------------------------------------------------- MarkMLl · 2021-02-04 17:53 I don't think I can help with this, but to keep the question on the "Recent" pane: what exactly are you looking for, a way of translating Pascal to MS Word's macro language or a way of controlling MS Word's typesetting from Lazarus/FPC? MarkMLl ---------------------------------------------------------------------- alaa123456789 · 2021-02-05 11:23 please find this component ,it is in delphi ,we need some one expert to convert it to lazarus https://github.com/karser/QDSEquations thanks ---------------------------------------------------------------------- PiotrAmator · 2021-02-05 14:51 Quote from: MarkMLl on February 04, 2021, 06:53:37 pmI don't think I can help with this, but to keep the question on the "Recent" pane: what exactly are you looking for, a way of translating Pascal to MS Word's macro language or a way of controlling MS Word's typesetting from Lazarus/FPC? MarkMLl Thank you for your interest! My program has to do the calculations and then make a note in MS Word (I want to use one button for this). I think if you can write a Lazarus program that puts text in MS Word, it can put equations too. I would like to do this without using VBA. ---------------------------------------------------------------------- PiotrAmator · 2021-02-05 14:55 Quote from: alaa123456789 on February 05, 2021, 12:23:17 pmplease find this component ,it is in delphi ,we need some one expert to convert it to lazarus https://github.com/karser/QDSEquations thanks I do not understand. My English is poor (I use google translator). Is this a help for me? Or are you asking me for something? What can I do with the linked files? ---------------------------------------------------------------------- alaa123456789 · 2021-02-05 15:16 Quote from: PiotrAmator on February 05, 2021, 03:55:23 pmQuote from: alaa123456789 on February 05, 2021, 12:23:17 pmplease find this component ,it is in delphi ,we need some one expert to convert it to lazarus https://github.com/karser/QDSEquations thanks I do not understand. My English is poor (I use google translator). Is this a help for me? Or are you asking me for something? What can I do with the linked files? my friend iam not asking you to help me, you were asking for something to write equations in Lazarus, i provided you a link ,it is in Delphi,  maybe someone Expert could port it for Lazarus so everyone get benefit of it thanks ---------------------------------------------------------------------- wp · 2021-02-05 15:47 Quote from: alaa123456789 on February 05, 2021, 12:23:17 pmplease find this component ,it is in delphi ,we need some one expert to convert it to lazarus https://github.com/karser/QDSEquations I ported the package and its demo to Lazarus 2.0+ and created a pull request for the original author. I don't know if he will merge it. For those who are interested I am attaching a zip. My impression: Looks nice although is usage is as complicated as I remember from Microsoft Word There are also a few bugs which would require further attention. ---------------------------------------------------------------------- PiotrAmator · 2021-02-05 17:32 Quote from: wp on February 05, 2021, 04:47:38 pmQuote from: alaa123456789 on February 05, 2021, 12:23:17 pmplease find this component ,it is in delphi ,we need some one expert to convert it to lazarus https://github.com/karser/QDSEquations I ported the package and its demo to Lazarus 2.0+ and created a pull request for the original author. I don't know if he will merge it. For those who are interested I am attaching a zip. My impression: Looks nice although is usage is as complicated as I remember from Microsoft Word There are also a few bugs which would require further attention. There is a problem with installing the package (screen as attachment). Do you know why? ---------------------------------------------------------------------- wp · 2021-02-05 17:36 Ah I normally work with Laz trunk, this property seems to be missing in 2.0.10. Simply comment this line out, it determines how the tabs of the pagecontrol are displayed (tabs, buttons, flat buttons) - it is not essential for the functionality of the component. ---------------------------------------------------------------------- PiotrAmator · 2021-02-05 18:27 Quote from: wp on February 05, 2021, 06:36:39 pmAh I normally work with Laz trunk, this property seems to be missing in 2.0.10. Simply comment this line out, it determines how the tabs of the pagecontrol are displayed (tabs, buttons, flat buttons) - it is not essential for the functionality of the component. Thank you. ---------------------------------------------------------------------- PiotrAmator · 2021-02-05 20:22 It looks like my previous idea wasn't the best so maybe I'll ask otherwise. Is it possible for Lazarus (Pascal) to run MS Word first and then run VBA code? I would like the VBA code to be part of a program written in Lazarus. Can I attach a piece of VBA code to Lazarus and then run it from Lazarus? Is it possible to run VBA code in a program created in Lazarus (Pascal)? ---------------------------------------------------------------------- wp · 2021-02-05 21:36 This is a German wiki article about Word automation (https://wiki.lazarus.freepascal.org/WordAutomation/de) - this is some kind of "VBA for Pascal". But I never got happy with it, too many things to do wrong and no compiler to help with syntax issues. I wonder what is your motivation behind this task? Just an idea, a nice-to-have feature? A requirement of your boss? If I understood your correctly a few posts higer you want Lazarus to perform some calculation and to document the calculation and results in a Word document. Why don't you write this to a text file which you can import into Word and format nicely then. Do you want to avoid this manual formatting? Believe me: Writing this with Word automation will take you longer than doing all this manual formatting. Or you could create a html document. You can even embed MathML instructions which gives you perfectly formatted math equations. (Maybe the formula editor which I ported to Lazarus can be modified to create MathML...). Or you could have Lazarus write a LaTeX document. Lots of possibilities better than Word Automation. ---------------------------------------------------------------------- PiotrAmator · 2021-02-08 07:49 Quote from: wp on February 05, 2021, 10:36:39 pmThis is a German wiki article about Word automation (https://wiki.lazarus.freepascal.org/WordAutomation/de) - this is some kind of "VBA for Pascal". But I never got happy with it, too many things to do wrong and no compiler to help with syntax issues. I wonder what is your motivation behind this task? Just an idea, a nice-to-have feature? A requirement of your boss? If I understood your correctly a few posts higer you want Lazarus to perform some calculation and to document the calculation and results in a Word document. Why don't you write this to a text file which you can import into Word and format nicely then. Do you want to avoid this manual formatting? Believe me: Writing this with Word automation will take you longer than doing all this manual formatting. Or you could create a html document. You can even embed MathML instructions which gives you perfectly formatted math equations. (Maybe the formula editor which I ported to Lazarus can be modified to create MathML...). Or you could have Lazarus write a LaTeX document. Lots of possibilities better than Word Automation. 1. (This is a German wiki article about Word automation (https://wiki.lazarus.freepascal.org/WordAutomation/de) - this is some kind of "VBA for Pascal". But I never got happy with it, too many things to do wrong and no compiler to help with syntax issues.) Thank you for the link to the article in German. I think it will be useful. 2. (I wonder what is your motivation behind this task? Just an idea, a nice-to-have feature? A requirement of your boss?) The program is needed for me. There are many programs with the option I need (it is not new) - I find it convenient and significantly shorten the work. 3. (Why don't you write this to a text file which you can import into Word and format nicely then. Do you want to avoid this manual formatting? Believe me: Writing this with Word automation will take you longer than doing all this manual formatting.) I repeat the calculations many times. It is impossible to format manually each time. I will write the program once and it will always work. 4. (Or you could create a html document. You can even embed MathML instructions which gives you perfectly formatted math equations. (Maybe the formula editor which I ported to Lazarus can be modified to create MathML...). Or you could have Lazarus write a LaTeX document. Lots of possibilities better than Word Automation.) MS Word is my basic program (I also get a * .docx file from people I work with). I learned programming on my own - I can't use HTML and LaTeX, but I intend to become interested in them in the future. Thanks for your comments and help. ---------------------------------------------------------------------- egsuh · 2021-02-08 08:40 Here're some explanations. Your objective should be possible, in principle at least. https://wiki.freepascal.org/Office_Automation I've done some tests with OpenOffice, not MS-Word. But there should be no reason MS-Office is not possible.  I could write texts into an open OpenOffice file, with following statement.      xText.insertString(document.text.getEnd,  'Question heading 3', false);  Here, xText and document.text are objects within OpenOffices' VBA, so these should be declared as Variant type variable.  insertString and getEnd are their methods, so they do not need to be declared within Pascal codes. I have not tried running VBA module saved in OpenOffice within the codes of Pascal, but again, I guess they will be possible. Following example is a fully functioning unit I downloaded from somewhere --- do not remember where.  If you are familiar with both VBA and Pascal, I believe you would have no difficulty in understanding the logic of following codes (I have big difficulty.) Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit OLECreateTextDoc; {$mode objfpc}{$H+} interface uses  Classes, sysutils, variants, comobj; const  ServiceName = 'com.sun.star.ServiceManager'; var  ServiceManager, Desktop, Document, CoreReflection, LoadParams: Variant;  xText, xTextCursor: Variant;  xTable, xTableRows, xRow: Variant;  xFrame, xFrameText, xFrameTextCursor, xSize: Variant;  procedure InsertIntoCell(const CellName, theText: WideString; Table: Variant);function CreateStruct(const Reflection: Variant; const strTypeName: WideString): Variant;  implementation  procedure InsertIntoCell(const CellName, theText: WideString; Table: Variant);var  xTableText, xTableTextCursor: Variant;begin  xTableText := Table.getCellByName(CellName);  xTableTextCursor := xTableText.createTextCursor();  xTableTextCursor.setPropertyValue('CharColor', 16777215);  xTableText.setString( theText );end; function CreateStruct(const Reflection: Variant; const strTypeName: WideString): Variant;var  IdlClass: Variant;begin  IdlClass := Reflection.forName(strTypeName);  // https://api.libreoffice.org/docs/idl/ref/interfacecom_1_1sun_1_1sta r_1_1reflection_1_1XIdlClass.html  IdlClass.createObject(Result);end;  begin  if Assigned(InitProc) then    TProcedure(InitProc);   try    ServiceManager := CreateOleObject(ServiceName);  except    WriteLn('Unable to start OO/LO.');    Exit;  end;   // https://api.libreoffice.org/docs/idl/ref /servicecom_1_1sun_1_1star_1_1frame_1_1Desktop.html  Desktop := ServiceManager.CreateInstance('com.sun.star.frame.Desktop');   CoreReflection := ServiceManager.CreateInstance('com.sun.star.reflecti on.CoreReflection');   LoadParams := VarArrayCreate([0, -1], varVariant);   // Desktop implements XComponentLoader https://api.libr eoffice.org/docs/idl/ref/interfacecom_1_1sun_1_1star_1_1frame_1_1XComp onentLoader.html  // Document is a TextDocument https://api.libreoffic e.org/docs/idl/ref/servicecom_1_1sun_1_1star_1_1text_1_1TextDocument.h tml  Document := Desktop.LoadComponentFromURL('private:factory/swriter', '_blank', 0, LoadParams);   // insert some text.  // For this purpose get the Text- Object of the document an create the  // cursor. Now it is possible to insert a text at the cursor-position  // via insertString   //getting the text object  xText := Document.getText();   //create a cursor object  xTextCursor := xText.createTextCursor();   //inserting some Text  xText.insertString( xTextCursor, 'The first line in the newly created text document.' + #10, False);   //inserting a second line  xText.insertString( xTextCursor, 'Now we''re in the second line' + #10, False);   // insert a text table.  // For this purpose create an  // instance of com.sun.star.text.TextTable and initialize it. Now it can  // be inserted at the cursor position via insertTextContent.  // After that some properties are changed and some data is inserted.   xTable := Document.createInstance('com.sun.star.text.TextTable');   //initialize the text table with 4 columns an 4 rows  xTable.initialize(4, 4);   //insert the table  xText.insertTextContent(xTextCursor, xTable, False);   // get first Row  xTableRows := xTable.getRows();  xRow := xTableRows.getByIndex(0);   // Change the BackColor  xTable.setPropertyValue('BackTransparent', False);  xTable.setPropertyValue('BackColor', 13421823);  xRow.setPropertyValue('BackTransparent', False);  xRow.setPropertyValue('BackColor', 6710932);   InsertIntoCell('A1','FirstColumn', xTable);  InsertIntoCell('B1','SecondColumn', xTable) ;  InsertIntoCell('C1','ThirdColumn', xTable) ;  InsertIntoCell('D1','SUM', xTable) ;    //Insert Something in the text table  xTable.getCellByName('A2').setValue(22.5);  xTable.getCellByName('B2').setValue(5615.3);  xTable.getCellByName('C2').setValue(-2315.7);  xTable.getCellByName('D2').setFormula('sum ');   xTable.getCellByName('A3').setValue(21.5);  xTable.getCellByName('B3').setValue(615.3);  xTable.getCellByName('C3').setValue(-315.7);  xTable.getCellByName('D3').setFormula('sum ');   xTable.getCellByName('A4').setValue(121.5);  xTable.getCellByName('B4').setValue(-615.3);  xTable.getCellByName('C4').setValue(415.7);  xTable.getCellByName('D4').setFormula('sum ');   // insert a colored text.  // Get the propertySet of the cursor, change the CharColor and add a  // shadow. Then insert the Text via InsertString at the cursor position.   // Change the CharColor and add a Shadow  xTextCursor.setPropertyValue('CharColor', Integer(255));  xTextCursor.setPropertyValue('CharShadowed', True);   // create a paragraph break  // https://api.libreoffice.org/docs/idl/ref/namespace com_1_1sun_1_1star_1_1text_1_1ControlCharacter.html  xText.insertControlCharacter(xTextCursor, 0 {PARAGRAPH_BREAK}, False);   //inserting colored Text  xText.insertString(xTextCursor, ' This is a colored Text - blue with shadow' + #10, False);  xText.insertControlCharacter(xTextCursor, 0, false);   // insert a text frame.  // create an instance of com.sun.star.text.TextFrame using the MSF of the  // document. Change some properties an insert it.  // Now get the text-Object of the frame an the corresponding cursor.  // Insert some text via insertString.   // Create a TextFrame  xFrame := Document.createInstance('com.sun.star.text.TextFrame');   // Set size  xSize := CreateStruct(CoreReflection, 'com.sun.star.awt.Size');  xSize.Height := 400;  xSize.Width := 15000;  xFrame.setSize(xSize);   // alternatively  {  xFrame.Height := 400;  xFrame.Width := 15000;  }   // get the property set of the text frame  // https://api.libreoff ice.org/docs/idl/ref/namespacecom_1_1sun_1_1star_1_1text.html#a470b1ca eda4ff15fee438c8ff9e3d834acb5ff4ea4718bf38762e2da8c553f924  xFrame.setPropertyValue('AnchorType', 1 {AS_CHARACTER});   xText.insertTextContent(xTextCursor, xFrame, False);   //getting the text object of Frame  xFrameText := xFrame.getText();   //create a cursor object  xFrameTextCursor := xFrameText.createTextCursor();   //inserting some Text  xFrameText.insertString(xFrameTextCursor,         'The first line in the newly created text frame.', False);   xFrameText.insertString(xFrameTextCursor,           #10 + 'With this second line the height of the frame raises.', false);   //insert a paragraph break  xText.insertControlCharacter(xTextCursor, 0, false );   xTextCursor.setPropertyValue('CharColor', 65536);  xTextCursor.setPropertyValue('CharShadowed', False);   xText.insertString(xTextCursor, ' That''s all for now !!', false); end. ---------------------------------------------------------------------- trev · 2021-02-08 08:52 There's also: * https://wiki.freepascal.org/PowerpointAutomation/de * https://wiki.freepascal.org/AccessAutomation/de * https://wiki.freepascal.org/ExcelAutomation/de * https://wiki.freepascal.org/ExcelAutomation2007plus/de * https://wiki.freepascal.org/WordAutomation/de * https://wiki.freepascal.org/CalcAutomation/de * https://wiki.freepascal.org/WriterAutomation/de ---------------------------------------------------------------------- PiotrAmator · 2021-02-25 10:43 Thank you for your help.