How to access Tables?

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
 
P

Peter Jamieson

I tried our code with some of my own definitions etc. to make it work,
using Delphi 7 (the version I happen to have to hand) and Word 2003 but
if I have 1 table in the document,
wrdDoc.Tables.Count
returns 1.

Which makes me wonder whether you are definitely opening the document
that you think you are opening.

If you have a more complete piece of code that includes everything
required to make your code work (e.g. including the declarations for
wrdApp and wrdDoc (I declared them as Variants in the Button1Click
procedure) it might be easier to spot the problem. Also, which
version(s) of Word?

Peter Jamieson

http://tips.pjmsn.me.uk
Visit Londinium at http://www.ralphwatson.tv
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top