Recent

Author Topic: table formatting in MS Word  (Read 2452 times)

woodhead

  • Newbie
  • Posts: 5
table formatting in MS Word
« on: April 23, 2016, 08:08:11 am »
Hi.
Did not get to format the table in MS Word when working from Lazarus using the COM object. The error occurs on the method AutoFormat:
Quote
Method 'AutoFormat' is not supported by automation object
Code:
   
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3.     {$mode objfpc}{$H+}
  4.  
  5.     interface
  6.  
  7.     uses
  8.       Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ComObj, LazUTF8;
  9.  
  10.     type
  11.  
  12.       { TForm1 }
  13.  
  14.       TForm1 = class(TForm)
  15.         procedure FormShow(Sender: TObject);
  16.       private
  17.         { private declarations }
  18.       public
  19.         { public declarations }
  20.       end;
  21.  
  22.     var
  23.       Form1: TForm1;
  24.  
  25.     implementation
  26.  
  27.     {$R *.lfm}
  28.  
  29.     { TForm1 }
  30.  
  31.     procedure TForm1.FormShow(Sender: TObject);
  32.     const
  33.       NUM_ROWS = 7;
  34.       NUM_COLS = 6;
  35.     var
  36.       W, WD, MyRange: OleVariant;
  37.     begin
  38.       //запуск приложения Word
  39.       try
  40.         W := GetActiveOleObject('Word.Application');// активация запущенного Word
  41.       except
  42.         On E : Exception do
  43.         begin
  44.           ShowMessage('Error: ' + WinCPToUTF8(E.Message));
  45.           W := CreateOleObject('Word.Application');// запуск Word
  46.         end;
  47.  
  48.       end;
  49.     // включение видимости Word
  50.       W.Parent.Visible:=true;
  51.       WD:=W.Documents.Add;
  52.       // добавление таблицы
  53.       MyRange:=W.ActiveDocument.Range(0,0);
  54.       W.ActiveDocument.Tables.Add(Range:=MyRange,NumRows:=NUM_ROWS,NumColumns:=NUM_COLS);
  55.       // форматирование таблицы
  56.       try
  57.         W.ActiveDocument.Tables.Item(1).AutoFormat(Format:=16,
  58.                          ApplyBorders:=true,
  59.                          ApplyShading:=false,
  60.                          ApplyFont:=false,
  61.                          ApplyColor:=false,
  62.                          ApplyHeadingRows:=false,
  63.                          ApplyLastRow:=false,
  64.                          ApplyFirstColumn:=false,
  65.                          ApplyLastColumn:=false,
  66.                          AutoFit:=true);
  67.       except
  68.         On E : Exception do
  69.           ShowMessage('Error: ' + WinCPToUTF8(E.Message));
  70.       end;
  71.  
  72.     end;
  73.  
  74.     end.
  75.  
Almost the same code works in Delphi without error. What could be wrong?

 

TinyPortal © 2005-2018