P
peter
I want to access text inside a table in a doc from delphi. I copied the code
from microsoft http://support.microsoft.com/kb/229310), and modified it to
make it simple.
I select a .doc file at runtime, the file contains a table.
But tables.count always return 0. why? I wrote similar code in macro, it
works fine.
procedure TForm1.Button1Click(Sender: TObject);
var
StrToAdd : String;
wrdSelection, wrdMailMerge, wrdMergeFields : Variant;
i, j, idx : integer;
begin
// Create an instance of Word and make it visible
wrdApp := CreateOleObject('Word.Application');
wrdApp.Visible := True;
// Create a new document
if OpenDialog1.Execute then
wrdApp.Documents.Open(OpenDialog1.FileName);
wrdDoc := wrdApp.ActiveDocument;
//or I just used
//if OpenDialog1.Execute then
// wrdDoc := wrdApp.Documents.Open(OpenDialog1.FileName);
i := wrdDoc.Tables.Count;
//the i is 0 here!
for i := 1 to wrdDoc.Tables.Count do
begin
j := wrdDoc.Tables.Rows.Count; //Error: Call was rejected by
callee
for j := 1 to wrdDoc.Tables.Rows.Count do
begin
for idx := 1 to wrdDoc.Tables.Rows[j].Cells.Count do
begin
Memo1.Text := Memo1.Text + #13#10 +
wrdDoc.Tables.Rows[j].Cells[idx].Range.Text;
end;
end;
end;
end;
For i = 1 To ActiveDocument.Tables.Count
For j = 1 To ActiveDocument.Tables(i).Rows.Count
For idx = 1 To ActiveDocument.Tables(i).Rows(j).Cells.Count
Text = Text & "--" &
ActiveDocument.Tables(i).Rows(j).Cells(idx).Range.Text
Next idx
Next j
Next i
Selection.MoveDown Unit:=wdLine, Count:=8
Selection.MoveUp Unit:=wdLine, Count:=1
Selection.TypeText Text:=Text
from microsoft http://support.microsoft.com/kb/229310), and modified it to
make it simple.
I select a .doc file at runtime, the file contains a table.
But tables.count always return 0. why? I wrote similar code in macro, it
works fine.
procedure TForm1.Button1Click(Sender: TObject);
var
StrToAdd : String;
wrdSelection, wrdMailMerge, wrdMergeFields : Variant;
i, j, idx : integer;
begin
// Create an instance of Word and make it visible
wrdApp := CreateOleObject('Word.Application');
wrdApp.Visible := True;
// Create a new document
if OpenDialog1.Execute then
wrdApp.Documents.Open(OpenDialog1.FileName);
wrdDoc := wrdApp.ActiveDocument;
//or I just used
//if OpenDialog1.Execute then
// wrdDoc := wrdApp.Documents.Open(OpenDialog1.FileName);
i := wrdDoc.Tables.Count;
//the i is 0 here!
for i := 1 to wrdDoc.Tables.Count do
begin
j := wrdDoc.Tables.Rows.Count; //Error: Call was rejected by
callee
for j := 1 to wrdDoc.Tables.Rows.Count do
begin
for idx := 1 to wrdDoc.Tables.Rows[j].Cells.Count do
begin
Memo1.Text := Memo1.Text + #13#10 +
wrdDoc.Tables.Rows[j].Cells[idx].Range.Text;
end;
end;
end;
end;
For i = 1 To ActiveDocument.Tables.Count
For j = 1 To ActiveDocument.Tables(i).Rows.Count
For idx = 1 To ActiveDocument.Tables(i).Rows(j).Cells.Count
Text = Text & "--" &
ActiveDocument.Tables(i).Rows(j).Cells(idx).Range.Text
Next idx
Next j
Next i
Selection.MoveDown Unit:=wdLine, Count:=8
Selection.MoveUp Unit:=wdLine, Count:=1
Selection.TypeText Text:=Text