BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Replacing a Bookmark with a Table

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi OLE Automation
View previous topic :: View next topic  
Author Message
Martin
Guest





PostPosted: Wed Sep 13, 2006 4:32 pm    Post subject: Replacing a Bookmark with a Table Reply with quote



I am trying to use Delphi7 to replace a Bookmark in an Office 11 Word
document, generated from a Template.dot file with the content of a database
Table. The size of the database Table, and hence probably the Word Table I
want to create varies, as does the position of the Table in the Document.

The examples I have found only put a string in a word bookmark. Any ideas
how to put a word table, generated from a database table, at the bookmarks
place? I am working with the TWordApplication and TWordDocument types.
Back to top
Deborah Pate (TeamB)
Guest





PostPosted: Wed Sep 13, 2006 6:02 pm    Post subject: Re: Replacing a Bookmark with a Table Reply with quote



<<Martin:
Any ideas how to put a word table, generated from a database table,
at the bookmarks place?
Quote:


Manipulating tables in Word is pretty slow, so it would probably be
best to insert e.g. tab-delimited strings (using
Bookmark.Range.InsertAfter), and then just use Range.ConvertToTable
to create a table from it as late as possible.

Please don't crosspost, by the way: follow-ups set to
b.p.d.oleautomation.

--
Deborah Pate (TeamB) http://delphi-jedi.org

TeamB don't see posts sent via Google or ISPs
Use the real Borland server: newsgroups.borland.com
http://www.borland.com/newsgroups/genl_faqs.html
Back to top
Martin
Guest





PostPosted: Wed Sep 13, 2006 7:39 pm    Post subject: Re: Replacing a Bookmark with a Table Reply with quote



"Deborah Pate (TeamB)" <Debs (AT) djpate (DOT) freeserve.co.not-this-bit.uk> wrote

Quote:
Manipulating tables in Word is pretty slow, so it would probably be
best to insert e.g. tab-delimited strings (using
Bookmark.Range.InsertAfter), and then just use Range.ConvertToTable
to create a table from it as late as possible.

Should not I set that Range somehow. I found this:

Range := MSWord.Documents.Item(1).Range(
MSWord.Documents.Item(1).Paragraphs.Item(1).Range.Start,
MSWord.Documents.Item(1).Paragraphs.Item(1).Range.End);


Quote:
Please don't crosspost, by the way: follow-ups set to
b.p.d.oleautomation.

Not your fault of course, but IMHO Borland sometimes has too many newsgroups
for too little products. And I wont do it anymore.


Last:

Making a tab delimited string is done by, this below??

'José Luis-Contabilidad 1' + #13

The plus bracket 13 ?

I am not an experienced Delphi programmer. Sorry.
Back to top
Martin
Guest





PostPosted: Wed Sep 13, 2006 8:29 pm    Post subject: Re: Replacing a Bookmark with a Table Reply with quote

"Deborah Pate (TeamB)" <Debs (AT) djpate (DOT) freeserve.co.not-this-bit.uk> wrote in
message news:VA.00002106.010d85cd (AT) djpate (DOT) freeserve.co.not-this-bit.uk...
Quote:


Manipulating tables in Word is pretty slow, so it would probably be
best to insert e.g. tab-delimited strings (using
Bookmark.Range.InsertAfter), and then just use Range.ConvertToTable
to create a table from it as late as possible.


Ok, but it does not work, and I feel kinda miserable!! I am working with
Delphi 7 Entreprsie, and Office Word 2003 (11.6568.6568)

The following gives me a table with only one row and
the first cell filled with:

"HeadOfficeAddress before tab after tab before tab
after tab "

cell two had no content set.


//==================================
MyWAP := TWordApplication.Create(Application);
MyWDoc := TWordDocument.Create(Application);

exeDir := ExtractFilePath(application.exename);
strTemplate := exeDir + 'Naam1.DOT';
VarFalse := false;
VarTrue := true;
ItemIndex := 1;
NumR := 12;
NumC := 12;
TBNm := 'HeadOfficeAddress';
MyWAP.Documents.Add(strTemplate, VarFalse, EmptyParam, VarTrue);
MyWDoc.ConnectTo(MyWAP.Documents.Item(ItemIndex));
LocalText := ' before tab ' + #9 + ' after tab ';
MyWDoc.Bookmarks.Item(TBNm).Range.ConvertToTable(EmptyParam, NumR,
NumC,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam);
MyWDoc.Bookmarks.Item(TBNm).Range.InsertAfter(LocalText);
MyWDoc.Bookmarks.Item(TBNm).Range.InsertAfter(LocalText);
//===================================================
Back to top
Deborah Pate (TeamB)
Guest





PostPosted: Wed Sep 13, 2006 10:05 pm    Post subject: Re: Replacing a Bookmark with a Table Reply with quote

<<Martin:
Ok, but it does not work, and I feel kinda miserable!!
Quote:


You have to call ConvertToTable /after/ entering the text. I can't test it
right now, but something like this:

const
ThreeStrings = '%s'#9'%s'#9'%s';
FourIntegers = '%d'#9'%d'#9'%d'#9'%d';
var
Direction, Separator: OleVariant;
NumCols, Autofit: OleVariant;
R: Range;
...
Direction := wdCollapseEnd;
R := WordDoc.Bookmarks.Item(TBNm).Range;
R.Collapse(Direction);
R.InsertAfter(Format(ThreeStrings, ['One', 'Two', 'Three']));
R.InsertAfter(Format(FourIntegers, [1, 2, 3, 4]));
Separator := #9;
NumCols := 4;
Autofit := True;
R.ConvertToTableOld(Separator, EmptyParam, NumCols,
EmptyParam, EmptyParam, EmptyParam,
EmptyParam, EmptyParam, EmptyParam,
EmptyParam, EmptyParam, EmptyParam,
EmptyParam, Autofit);

--
Deborah Pate (TeamB) http://delphi-jedi.org

TeamB don't see posts sent via Google or ISPs
Use the real Borland server: newsgroups.borland.com
http://www.borland.com/newsgroups/genl_faqs.html
Back to top
Deborah Pate (TeamB)
Guest





PostPosted: Thu Sep 14, 2006 1:32 am    Post subject: Re: Replacing a Bookmark with a Table Reply with quote

<<Deborah Pate (TeamB):
I can't test it right now
Quote:


And I see I left the carriage returns off the end of the lines. Oh
well. :)

--
Deborah Pate (TeamB) http://delphi-jedi.org

TeamB don't see posts sent via Google or ISPs
Use the real Borland server: newsgroups.borland.com
http://www.borland.com/newsgroups/genl_faqs.html
Back to top
Martin
Guest





PostPosted: Thu Sep 14, 2006 8:12 am    Post subject: Re: Replacing a Bookmark with a Table Reply with quote

"Deborah Pate (TeamB)" <Debs (AT) djpate (DOT) freeserve.co.not-this-bit.uk> wrote

Quote:
You have to call ConvertToTable /after/ entering the text.

In my original program I already tried that, then the string is placed
behind the table, not inside the table.

Looking at your example I think I may-behave to do something else too. There
are more differences.
Back to top
Deborah Pate (TeamB)
Guest





PostPosted: Thu Sep 14, 2006 2:15 pm    Post subject: Re: Replacing a Bookmark with a Table Reply with quote

<<Martin:
Looking at your example I think I may-behave to do something else
too. There are more differences.
Quote:


You should definitely use a local Range variable, assign the
bookmark's range to it, and then use the local variable only for
the text and table insertion:
R := WordDoc.Bookmarks.Item(TBNm).Range;
...
R.InsertAfter(LocalText);

For one thing it will be faster than calling the Bookmarks
collection every time (it is always good to reduce the number of
dots you have in your automation code, as each one represents an
expensive out-of-process call). But the more important issue here
is that the Bookmark's range is set by the Bookmarks.Add method,
and you are reverting to that each time you use the
Bookmark(Item).Range method. When you use a local range variable,
its bounds change when you use commands like InsertAfter.

--
Deborah Pate (TeamB) http://delphi-jedi.org

TeamB don't see posts sent via Google or ISPs
Use the real Borland server: newsgroups.borland.com
http://www.borland.com/newsgroups/genl_faqs.html
Back to top
Martin
Guest





PostPosted: Thu Sep 14, 2006 4:50 pm    Post subject: Re: Replacing a Bookmark with a Table Reply with quote

"Deborah Pate (TeamB)" <Debs (AT) djpate (DOT) freeserve.co.not-this-bit.uk> wrote

Quote:
But the more important issue here
is that the Bookmark's range is set by the Bookmarks.Add method,
and you are reverting to that each time you use the
Bookmark(Item).Range method.

Eso es!!

This works:

Unit1.pas:

=======================
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Word_TLB;

type
TForm1 = class(TForm)
Label1: TLabel;
Button1: TButton;
procedure Button1Click(Sender: TObject);
function ReplaceTM(var aWDoc : TWordDocument; TM_Name, MyText: String):
Integer;
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}


function TForm1.ReplaceTM(var aWDoc : TWordDocument; TM_Name, MyText:
String): Integer;
// Replaces the given Bookmark with the given Text
// Returns 0 when ok, -1 on Error
var LocalName, LocalText : OLEVariant;
begin
// Check if Bookmark exists
LocalName := TM_Name;
LocalText := MyText;
If aWDoc.Bookmarks.exists(LocalName) then begin
aWDoc.Bookmarks.Item(LocalName).Range.Text := LocalText;
// Now bookmark should no longer exist
If aWDoc.Bookmarks.exists(LocalName) then
result := -1
else
result := 0;
end
else
result := -1;
end;


procedure TForm1.Button1Click(Sender: TObject);
const
MyBookmarks: array [1..7] of String =
('Naam','Datum','JaNee','Integer','Float','Special','HeadOfficeAddress');
MyTexts: array [1..7] of String = ('Maritza Castro','22 maart
1980','Ja','1','2.0','Heel Speciaal','Bos en Lommerplein, maar we staan op
instorten!');
var MyWAP : TWordApplication;
MyWDoc : TWordDocument;
MyRange : Range;
exeDir : String;
strTemplate, VarFalse, VarTrue, ItemIndex, NumR, NumC, TBNm, LocalText
: OLEVariant;
BM: integer;
rtnCode : integer;
loopInt : integer;

begin
// Zorg dat je een word applicatie al draaiende hebt anders werkt het
niet.
MyWAP := TWordApplication.Create(Application);
MyWDoc := TWordDocument.Create(Application);

exeDir := ExtractFilePath(application.exename);
strTemplate := exeDir + 'WordTemplate.DOT';
VarFalse := false;
VarTrue := true;
ItemIndex := 1;
NumR := 12;
NumC := 12;
TBNm := 'Tabel';
MyWAP.Documents.Add(strTemplate, VarFalse, EmptyParam, VarTrue);
MyWDoc.ConnectTo(MyWAP.Documents.Item(ItemIndex));

BM := 1;
Repeat
rtnCode := ReplaceTM(MyWDoc, MyBookmarks[BM], MyTexts[BM]);
BM := BM + 1;
Until (BM > 7) OR (rtnCode < 0);


MyRange := MyWDoc.Bookmarks.Item(TBNm).Range;

for loopInt := 1 to 144 do
begin
LocalText := '[ ' + IntToStr(loopInt) + '] ' + #9;
MyRange.InsertAfter(LocalText);
end;
MyRange.ConvertToTable(EmptyParam, NumR,
NumC,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam);

end;

end.
================================



Project1.dpr:

===================================
program Project1;

uses
Forms,
Unit1 in 'Unit1.pas' {Form1},
Word_TLB in 'C:\Running\Ontwikkeling\lib\Imports\Word_TLB.pas',
Office_TLB in 'C:\Running\Ontwikkeling\lib\Imports\Office_TLB.pas',
VBIDE_TLB in 'C:\Running\Ontwikkeling\lib\Imports\VBIDE_TLB.pas';

{$R *.res}

begin
Forms.Application.Initialize;
Forms.Application.CreateForm(TForm1, Form1);
Forms.Application.Run;
end.
========================
Back to top
Robert Pointon
Guest





PostPosted: Fri Sep 22, 2006 1:15 pm    Post subject: Re: Replacing a Bookmark with a Table Reply with quote

Hope you find this useful. I ran out off time so I left the user to save the
file and close word. If you use this and find out hoe to save the file and
close word please let me know.

unit TableInWord;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,
Forms,
Dialogs, StdCtrls, Comobj;

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
s : string ;
MsWord : Variant ;
i, y, l : integer ;
CONST
NEW_LINE = #13;
//************** Helper ******
Procedure AddToWord ;
BEGIN
Inc(L);
MSWord.Insert(S);
s := '' ;
END;
//************** Helper ******
Procedure AddNewLine ;
BEGIN
Inc(L);
MSWord.Insert(NEW_LINE);
s := '' ;
END;

//********************
begin
// Show wait message
try
MsWord := CreateOleObject('Word.Basic');
except
ShowMessage('Could not start Microsoft Word.');
Exit;
end;

// MsWord.AppShow;
MSWord.FileNew;

L := 0;
// MsWord.Fon.Font.bold := true;
// Make header
S := 'Report title' + ListSeparator+ #13;

for i:=0 to 10 do begin
// Make titles
S := 'Col 1 header' + ListSeparator
+'Col 2 header' + ListSeparator
+'Col 3 header' + ListSeparator
+'Col 4 header' + ListSeparator
+'Col 5 header' + ListSeparator
+'Col 6 header' + NEW_LINE ;
AddToWord ;

// Create the block of the for the an element
FOR y := 0 TO 5 DO BEGIN
S := S + 'Col 1 line '+inttostr(y) + ListSeparator
+ 'Col 2 line '+inttostr(y) + ListSeparator
+ 'Col 3 line '+inttostr(y) + ListSeparator
+ 'Col 4 line '+inttostr(y) + ListSeparator
+ 'Col 5 line '+inttostr(y) + ListSeparator
+ 'Col 6 line '+inttostr(y) + ListSeparator;
AddToWord ;
END; // for
AddNewLine ;
s := 'another row =' + ListSeparator+ 'Info' + ListSeparator;
AddToWord ;
AddNewLine ;
AddNewLine ;
// MsWord.Insert.PageBreak;
end;

MsWord.AppShow;
MSWord.LineUp(L, 1);
MSWord.TextToTable(ConvertFrom := 2, NumColumns := 6);
end;

end.

Bob Pointon
Back to top
Sjors Dubbeljoe Boes
Guest





PostPosted: Tue Sep 26, 2006 1:45 pm    Post subject: Re: Replacing a Bookmark with a Table Reply with quote

"Robert Pointon" <RPointon (AT) phosyn (DOT) com> wrote

Quote:
If you use this and find out hoe to save the file and
close word please let me know.

I've seen the message. If I can help, and I find the above, I'll will let
you know.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi OLE Automation All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.