Recent

Author Topic: Beginners ask the question of generating word documents  (Read 1163 times)

jianwt

  • Jr. Member
  • **
  • Posts: 85
Beginners ask the question of generating word documents
« on: March 25, 2023, 06:39:20 am »
Beginners ask the question of generating word documents
Why do I get an error setting the row height and column width when generating a Word document?
Thank you very much

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Beginners ask the question of generating word documents
« Reply #1 on: March 25, 2023, 02:52:32 pm »
I don't why but, I also don't do any compression files other than ZIP, so you kind of narrowed your audience.

 I know RAR is popular in the unix/linux scene, but I really don't think this is case here.

The only true wisdom is knowing you know nothing

jianwt

  • Jr. Member
  • **
  • Posts: 85
Re: Beginners ask the question of generating word documents
« Reply #2 on: March 27, 2023, 02:02:17 am »
I'll change it to Zip

jianwt

  • Jr. Member
  • **
  • Posts: 85
Re: Beginners ask the question of generating word documents
« Reply #3 on: March 27, 2023, 02:05:28 am »
Change table column width and row height error!!
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2.   var
  3.  wordapp,MyRange,mytable: OLEVariant;
  4. begin
  5.   try
  6.      wordapp := CreateOleObject('Word.Application');
  7.   except
  8.     on e:exception do begin
  9.        Application.MessageBox('Error al crear el documento. Primeramente debe instalar el Office.', pchar(Self.Caption), MB_ICONINFORMATION);
  10.       exit;
  11.     end;
  12.   end;
  13.   wordapp.WordBasic.FileNew;
  14.  
  15.   wordapp.Selection.TypeParagraph;
  16.   MyRange:=wordapp.ActiveDocument.Range(0,0);
  17.   mytable := wordapp.ActiveDocument.Tables.Add(MyRange, 4, 2);
  18.  
  19. {Change table column width and row height error!!}
  20.   // Column width
  21.  // mytable.Columns.Item(1).SETWidth (50,0);  //wdAdjustNone
  22.  // mytable.Columns.Item(1).SETWidth (300,0);
  23.   //rows  height
  24. // mytable.Rows.Item(1).SetHeight(220,0);  // wdAdjustNone
  25. // mytable.Rows.Item(2).SetHeight(120, 0);
  26.  
  27.  
  28.   //  merge
  29.   mytable.cell(1, 1).merge(mytable.cell(1, 2));
  30.   mytable.cell(2, 1).merge(mytable.cell(2, 2));
  31.  
  32.  
  33.   mytable.Borders.InsideLineStyle :=1;
  34.   mytable.Borders.OutsideLineStyle := 1;
  35.  
  36.   wordApp.Visible := True;
  37.  
  38. end;
  39.  

loaded

  • Hero Member
  • *****
  • Posts: 824
Re: Beginners ask the question of generating word documents
« Reply #4 on: March 27, 2023, 07:05:01 am »
Tested working codes;

Code: Pascal  [Select][+][-]
  1. uses ComObj,LCLType;
  2.  
  3. procedure TForm1.Button1Click(Sender: TObject);
  4.   var
  5.  wordapp,MyRange,mytable: OLEVariant;
  6.  
  7. begin
  8.   try
  9.      wordapp := CreateOleObject('Word.Application');
  10.   except
  11.     on e:exception do begin
  12.        Application.MessageBox('Error al crear el documento. Primeramente debe instalar el Office.', pchar(Self.Caption), MB_ICONINFORMATION);
  13.       exit;
  14.     end;
  15.   end;
  16.   wordapp.WordBasic.FileNew;
  17.  
  18.   wordapp.Selection.TypeParagraph;
  19.   MyRange:=wordapp.ActiveDocument.Range(0,0);
  20.   mytable := wordapp.ActiveDocument.Tables.Add(MyRange, 4, 2);
  21.  
  22.   //  merge
  23.   mytable.cell(1, 1).merge(mytable.cell(1, 2));
  24.   mytable.cell(2, 1).merge(mytable.cell(2, 2));
  25.  
  26.  
  27.   // Set height and width
  28.   mytable.rows(1).height:=integer(50); //Older versions of Lazarus didn't need such numeric declarations, but updated versions throw an error if this type is not declared.
  29.   mytable.rows(3).cells(1).width:=integer(30);
  30.   mytable.rows(3).cells(2).width:=mytable.rows(1).cells(1).width-mytable.rows(3).cells(1).width;
  31.   //
  32.  
  33.   mytable.Borders.InsideLineStyle :=1;
  34.   mytable.Borders.OutsideLineStyle := 1;
  35.  
  36.   wordApp.Visible := True;
  37.  
  38. end;
Check out  loaded on Strava
https://www.strava.com/athletes/109391137

jianwt

  • Jr. Member
  • **
  • Posts: 85
Re: Beginners ask the question of generating word documents
« Reply #5 on: March 27, 2023, 07:17:19 am »
Thank you so much

loaded

  • Hero Member
  • *****
  • Posts: 824
Re: Beginners ask the question of generating word documents
« Reply #6 on: March 27, 2023, 07:45:10 am »
I think the number of people having problems with the issue is increasing. In this case, I felt the need to make a notification. Because, this problem had a lot of trouble for me at the time.

Let's get to the point;
I think the main cause of such problems is a bug in Lazarus;
Because the code that works fine in old Lazarus versions results in such an error in updated versions.

As I understand it, we can no longer directly assign numerical values to some properties. As follows;

Code that works fine in old versions but gives type mismatch error in new versions;
Code: Pascal  [Select][+][-]
  1. mytable.rows(1).height:=50;

If corrected as follows, the code that works smoothly in all versions;
Code: Pascal  [Select][+][-]
  1. mytable.rows(1).height:=integer(50);

Or
Code: Pascal  [Select][+][-]
  1. var
  2. wx:integer=50;
  3. ...
  4. mytable.rows(1).height:=wx;
  5.  

I think developers can easily fix this problem.    :)
Check out  loaded on Strava
https://www.strava.com/athletes/109391137

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: Beginners ask the question of generating word documents
« Reply #7 on: March 27, 2023, 12:01:15 pm »
This is not a Lazarus bug.
It's caused by a change in FreePascal, which, AFAIK, is Delphi compatible.

Bart

Bitbeisser

  • New Member
  • *
  • Posts: 28
Re: Beginners ask the question of generating word documents
« Reply #8 on: March 28, 2023, 05:04:31 pm »
I don't why but, I also don't do any compression files other than ZIP, so you kind of narrowed your audience.

 I know RAR is popular in the unix/linux scene, but I really don't think this is case here.
And if you are using 7ZIP (what else  :D ), then it just doesn't matter...

 

TinyPortal © 2005-2018