OLE automation write chinese characters to Excel lazarus / 34-Windows ---------------------------------------------------------------------- robotech · 2016-05-03 12:06 Dear All, I'm using Lazarus 1.6 , and try to do the same what i did in Delphi several years before at last: Fill some cells by OLE office automation. When i hardcoding the text to the program, it's export very well to Excel, but if i want to send a variable to excel (for ex: edit1.text) just appear Chinese text, but nothing else... Here is some code: procedure TForm1.Button2Click(Sender: TObject); begin   XLApp := CreateOleObject('Excel.Application');  try    XLApp.Visible := True;    XLApp.DisplayAlerts := True;     XLApp.Workbooks.Add;     // Open the Workbook        XLApp.Cells[1,1].Value :='This is a Hardcoded text in program';        XLApp.Cells[2,1].Value :=edit2.text ;       end;     end;  finally    XLApp.ActiveWorkBook.SaveAs(edit1.text, 51);  //here also save excel whith name full chinese chars... end;            I took  screenshot about the results.... I think there is must be a string conversation  issue, but a couldn't figure it out what may wrong...      Could you help in this , please?    ---UPDATE---! Very interesting! if i type:     XLApp.Cells[2,1].Value :=utf8Decode(edit2.text) ; Stops with SIGSEGV error, but this is the conversion, what i was looking for... But if i put in function Function StrToWidestr(aString: String): WideString; begin   Result:= UTF8Decode(aString); end;                      with XLApp.Cells[2,1].Value :=StrToWidestr(edit2.text) ;  Its works! How this can be? Another question: Can you help me to find the command to change printer? Xlapp.Application.ActivePrinter:='Xerox Phaser 3117' doesnt work... or how can i make sure the name of the printer exactly in the system? ---------------------------------------------------------------------- fred · 2016-05-03 12:43 Did you try WideString(Edit2.Text) ? ---------------------------------------------------------------------- wp · 2016-05-03 12:44 Excel, by OLE automation, I think, requires Unicode strings. Edit2.Text, however, is UTF8, and assigning to a variant should not trigger an internal conversion. So, I think, you should try 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 XLApp.Cells[2,1].Value :=UTF8ToUTF16(edit2.text); // or // ... := UTF8Decode(edit2.text); Alternatively you could try fpspreadsheet which works in UTF8 and does the conversions needed for saving automatically: 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 uses  fpspreadsheet, fpstypes, xlsxooxml;var  workbook: TsWorkbook;  worksheet: TsWorksheet;begin  workbook := TsWorkbook.Create;  worksheet := workbook.AddWorksheet('Sheet 1');  worksheet.WriteText(0, 0, 'This is a hardcoded text in program');  worksheet.WriteText(1, 0, Edit2.Text);  workbook.WriteToFile(Edit1.Text, sfOOXML, true);  workbook.Free;end; It would be interesint to see it with Chinese characters, I never tested it this way... As for the printer, try 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 uses  Printers;var  activePrinter: String;....  activePrinter := Printer.PrinterName;In fpspreadsheet, the Printer, however, is not supported. ---------------------------------------------------------------------- robotech · 2016-05-04 08:09 I have tried Printers class, but i didnt reach any goal... everything i try it makes : SIGSEGV error...  but just simple a wanted to ask the installed printers in two different way: procedure TForm1.Button2Click(Sender: TObject); begin   ComboBox1.Items.Assign(Printer.Printers); end;          procedure TForm1.Button3Click(Sender: TObject); begin   edit1.text := Printer.PrinterName; end; Lazarus stopping here in Printers class: //Return the current selected printer function TPrinter.GetPrinterIndex: integer; begin   Result:=fPrinterIndex;   if (Result<0) and (Printers.Count>0) then      Result:=0; //printer by default end;                What could be wrong? ---------------------------------------------------------------------- wp · 2016-05-04 09:03 Did you add the package printer4lazarus as a requirement to your project? http://wiki.freepascal.org/Using_the_printer ---------------------------------------------------------------------- robotech · 2016-05-04 09:09 Huhh!  This was a very good observation, Thanks!  Of course i didn't add it!  But i really need to write it onto a sticky notes to remember for this in the following years too, as i don't use printer class to often!  Thanks for help! ---------------------------------------------------------------------- robotech · 2016-05-04 09:42 Just one more question: I intended to know what printers are available, because i would like to print documents on two different printer (PDF, and a real printer). To set this with OLE autiomation i use: XLApp : OLEvariant; .... XLApp.Appication.ActivePrinter:=strtowidestr(edit1.text); As errormessage shown: Unable to set ActivePrinter property of the Application class  is that means that this property is not exist, (have to), or it means it is read only, or how i can set printer to the Excel by OLE automation? Unfortunately if i set printer by lazarus with: Printer.Setprinter('name'); doesn't work, because when excel opens, it's set printer the default printer... ---------------------------------------------------------------------- wp · 2016-05-04 10:58 Microsoft knows many tricks to confuse people... You must add the port name to the printer name. Strangely enough, all ports are called "NeXX:" with XX being replaced by a two-digit number, but I did not find out the logics behind these numbers. And, there is a localized word between the printer name and port name to indicate that the printer prints TO this port. Just open Excel, record a macro for printing to the requested port, edit the macro and you'll see the required string in the command "ExecuteExcel4Macro". For selecting the printer "PDF24 Fax" on my Germany system, for example, the code would be 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   XlApp.Application.ActivePrinter:= WideString(PDF24 Fax auf Ne02:');  // In English Windows this would be  // XlApp.Application.ActivePrinter:= WideString(PDF24 Fax to Ne02:');Tested, and this works. [EDIT] Check this out (I didn't), it may help you to find the printer string correctly.