was ist neu ¦  programmier tips ¦  indy artikel ¦  intraweb artikel ¦  informationen ¦  links ¦  interviews
 sonstiges ¦  tutorials ¦  Add&Win Gewinnspiel

Tips (1541)

Dateien (137)
Datenbanken (90)
Drucken (35)
Grafik (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Mathematik (76)
Multimedia (45)
Oberfläche (107)
Objekte/
ActiveX (51)

OpenTools API (3)
Sonstiges (126)
Strings (83)
System (266)
VCL (242)

Tips sortiert nach
Komponente


Tip suchen

Tip hinzufügen

Add&Win Gewinnspiel

Werbung

41 Visitors Online


 
...eine Tabelle in einem MS Word Dokument in ein TStringgrid exportieren?
Autor: Thomas Stutz
[ Tip ausdrucken ]  

Tip Bewertung (15):  
     


uses
  
ComObj;

procedure TForm1.Button1Click(Sender: TObject);
const
  
AWordDoc = 'C:\xyz\testTable.doc';
var
  
MSWord, Table: OLEVariant;
  iRows, iCols, iGridRows, jGridCols, iNumTables, iTableChosen: Integer;
  CellText: string;
  InputString: string;
begin
  try
    
MSWord := CreateOleObject('Word.Application');
  except
    
// Error....
    
Exit;
  end;
  
  
try
    
MSWord.Visible := False;
    MSWord.Documents.Open(AWordDoc);

    // Get number of tables in document
    
iNumTables := MSWord.ActiveDocument.Tables.Count;

    InputString := InputBox(IntToStr(iNumTables) +
      ' Tables in Word Document', 'Please Enter Table Number', '1');
    // Todo: Validate string for integer, range...
    
iTableChosen := StrToInt(InputString);

    // access table
    
Table := MSWord.ActiveDocument.Tables.Item(iTableChosen);
    // get dimensions of table
    
iCols := Table.Rows.Count;
    iRows := Table.Columns.Count;
    // adjust stringgrid columns
    
StringGrid1.RowCount := iCols;
    StringGrid1.ColCount := iRows + 1;

    // loop through cells
    
for iGridRows := 1 to iRows do
      for 
jGridCols := 1 to iCols do
      begin
        
CellText := Table.Cell(jGridCols, iGridRows).Range.FormattedText;
        if not VarisEmpty(CellText) then
        begin
          
// Remove Tabs
          
CellText := StringReplace(CellText,
            #$D, '', [rfReplaceAll]);
          // Remove linebreaks
          
CellText := StringReplace(CellText, #$7, '', [rfReplaceAll]);

          // fill Stringgrid
          
Stringgrid1.Cells[iGridRows, jGridCols] := CellText;
        end;
      end;
    //..
  
finally
    
MSWord.Quit;
  end;
end;

 

Bewerten Sie diesen Tipp:

dürftig
ausgezeichnet


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners