 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Sjors Dubbeljoe Boes Guest
|
Posted: Fri Sep 29, 2006 8:45 pm Post subject: Word: Bookmarks: Inserted Page break is not between the inse |
|
|
Hello,
I have managed to insert two pages into another file, this file is a
template with one bookmark. But I want to have a page break BETWEEN the two
pages. Somehow the page breaks appear before the two pages. Here is my code,
what am I doing wrong? Any help is welcome! :-)
//=================================================
unit WordFilesSamen;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Word_TLB;
type
TForm1 = class(TForm)
Button1: TButton;
function ReplaceTM(var aWDoc : TWordDocument; TM_Name, MyText: String):
Integer;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var MyWAP : TWordApplication;
MyWDoc : TWordDocument;
MyRange : Range;
exeDir : String;
ovDirection, ovBreakKind, ovstrNwFNm, ovstrTemplate, ovItemIndex,
ovRange, ovConfirmConversions, ovLink, ovAttachment, ovTBNm, ovVarFalse,
ovVarTrue: OLEVariant;
rtnCode : integer;
wsFileName : WideString;
begin
MyWAP := TWordApplication.Create(Application);
MyWDoc := TWordDocument.Create(Application);
exeDir := ExtractFilePath(application.exename);
ovstrTemplate := exeDir + 'WordTemplate.DOT';
ovVarFalse := false;
ovVarTrue := true;
ovItemIndex := 1;
MyWAP.Documents.Add(ovstrTemplate, ovVarFalse, EmptyParam, ovVarTrue);
MyWDoc.ConnectTo(MyWAP.Documents.Item(ovItemIndex));
wsFileName := 'C:\tmp\testa.doc';
ovRange := EmptyParam;
ovConfirmConversions := EmptyParam;
ovLink := EmptyParam;
ovAttachment := EmptyParam;
ovTBNm := 'Allemaal';
MyRange := MyWDoc.Bookmarks.Item(ovTBNm).Range;
MyRange.InsertFile(wsFileName, ovRange, ovConfirmConversions, ovLink,
ovAttachment );
ovBreakKind := wdPageBreak;
ovDirection := wdCollapseEnd; //dunno what this is just copied it
from google
MyRange.Collapse(ovDirection);
MyRange.InsertBreak(ovBreakKind);
wsFileName := 'C:\tmp\testb.doc';
MyRange.InsertFile(wsFileName, ovRange, ovConfirmConversions, ovLink,
ovAttachment );
MyRange.InsertBreak(ovBreakKind);
rtnCode := ReplaceTM(MyWDoc, ovTBNm, '');
ovstrNwFNm := 'C:\tmp\NewFile.doc';
MyWDoc.SaveAs(ovstrNwFNm);
MyWDoc.Close;
MyWDoc.Destroy;
MyWAP.Quit;
MyWAP.Destroy;
end;
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;
end.
//====================================
//====================================
program Samen;
uses
Forms,
WordFilesSamen in 'WordFilesSamen.pas' {Form1},
Word_TLB in 'C:\Running\...\lib\Imports\Word_TLB.pas',
Office_TLB in 'C:\Running\...\lib\Imports\Office_TLB.pas',
VBIDE_TLB in 'C:\Running\...\lib\Imports\VBIDE_TLB.pas';
{$R *.res}
begin
Forms.Application.Initialize;
Forms.Application.CreateForm(TForm1, Form1);
Forms.Application.Run;
end. |
|
| Back to top |
|
 |
Sjors Dubbeljoe Boes Guest
|
Posted: Fri Sep 29, 2006 9:05 pm Post subject: Re: Bookmarks: Inserted Page break is not between the insert |
|
|
"Sjors Dubbeljoe Boes" <m.devries (AT) ingcarlease (DOT) com> wrote
| Quote: | I have managed to insert two pages into another file, this file is a
template with one bookmark. But I want to have a page break BETWEEN the
two pages. Somehow the page breaks appear before the two pages.
|
By the way a practical solution would be to just add an extra page break at
the end of the two pages I want to insert in this other document, but it's a
bit patchy. |
|
| Back to top |
|
 |
Oliver Townshend Guest
|
Posted: Sat Sep 30, 2006 4:47 am Post subject: Re: Bookmarks: Inserted Page break is not between the insert |
|
|
| Quote: | I have managed to insert two pages into another file, this file is a
template with one bookmark. But I want to have a page break BETWEEN the
two pages. Somehow the page breaks appear before the two pages. Here is my
code, what am I doing wrong? Any help is welcome!
|
Record a Macro of everything you want to do and convert that to Delphi.
You'll find it much easier.
I think your problem is the use of Ranges, which are a great tool in Excel,
but lousy in Word. Use selection instead. But record the macro, like I
said, and you'll see how to use selection.
Oliver Townshend |
|
| Back to top |
|
 |
Sjors Guest
|
Posted: Sat Sep 30, 2006 8:12 am Post subject: Re: Bookmarks: Inserted Page break is not between the insert |
|
|
"Oliver Townshend" <oliveratzipdotcomdotau> wrote
| Quote: | I think your problem is the use of Ranges, which are a great tool in
Excel, but lousy in Word. Use selection instead.
|
Right adding this works:
ovBreakKind := wdPageBreak;
ovDirection := wdCollapseEnd;
MyWAP.Selection.Collapse(ovDirection);
MyWAP.Selection.InsertBreak(ovBreakKind);
Thnx |
|
| Back to top |
|
 |
Deborah Pate (TeamB) Guest
|
Posted: Sat Sep 30, 2006 2:30 pm Post subject: Re: Word: Bookmarks: Inserted Page break is not between the |
|
|
<<Sjors Dubbeljoe Boes:
ovDirection := wdCollapseEnd; //dunno what this is just copied
it from google
A range has a starting and an end point - numbers representing its
position in the document. Calling its collapse method with
wdCollapseEnd means setting its start to equal its end position -
this prevents overwriting the contents of the range when you insert
text. It doesn't change the position of the end of the range, which
you need to do after inserting a file in order to put the break in.
There are a few ways you could do this - you might use two
bookmarks, for example, one a paragraph after the first:
R.InsertBreak(BreakKind);
R.InsertFile(FileName, EmptyParam, EmptyParam, EmptyParam,
EmptyParam);
BM := 'BM2';
R := Doc.Bookmarks.Item(BM).Range;
R.InsertBreak(BreakKind);
R.InsertFile(FileName, EmptyParam, EmptyParam, EmptyParam,
EmptyParam);
--
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
|
Posted: Sun Oct 01, 2006 8:12 am Post subject: Re: Word: Bookmarks: Inserted Page break is not between the |
|
|
<<Sjors:
I presume I cannot add bookmarks to the template dynamically
Well, you can, using the Bookmarks collection's Add method, but it is
easier to do something like this:
BM := Doc.Bookmarks.Item(BMName);
Direction := wdCollapseEnd;
for Idx := 1 to NumDocs do
begin
R := BM.Range;
R.Collapse(Direction); // Prevents the bookmark being overwritten
R.InsertBreak(BreakKind);
R.InsertParagraphAfter; // Position the range after the break
R.Collapse(Direction);
R.InsertFile(FileName, EmptyParam, EmptyParam,
EmptyParam, EmptyParam);
end;
--
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 |
|
 |
Sjors Guest
|
Posted: Sun Oct 01, 2006 8:12 am Post subject: Re: Word: Bookmarks: Inserted Page break is not between the |
|
|
"Deborah Pate (TeamB)" <Debs (AT) djpate (DOT) freeserve.co.not-this-bit.uk> wrote
| Quote: | There are a few ways you could do this - you might use two
bookmarks, for example, one a paragraph after the first:
|
Thank you for your information, but there is one problem, I do not know how
many separate word documents I have to merge to one document. Hence I do not
know how many bookmarks I would have to put in my template, and I presume I
cannot add bookmarks to the template dynamically. (And after each separate
document I want to put a pagebreak.) |
|
| Back to top |
|
 |
Sjors Dubbeljoe Boes Guest
|
Posted: Tue Oct 03, 2006 4:28 pm Post subject: Re: Word: Bookmarks: Inserted Page break is not between the |
|
|
"Deborah Pate (TeamB)" <Debs (AT) djpate (DOT) freeserve.co.not-this-bit.uk> wrote
Thnx this works then:
ovTBNm := 'Allemaal';
BM := MyWDoc.Bookmarks.Item(ovTBNm);
// No, no, no you stupid fool, do not put this here this does not work
// MyR := BM.Range;
for Idx := 5 downto 1 do // then the samen.doc starts with test1.doc not
test5.doc
begin
wsFileName := 'C:\tmp\test'+ IntToStr(Idx) +'.doc';
MyR := BM.Range; //resets it at the right position, correct Ms.
Pate??
if Idx <> 1 then // you do not want to START with a pagebreak!
begin
MyR.Collapse(ovDirection); // Prevents the bookmark being
overwritten
MyR.InsertBreak(ovBreakKind);
MyR.InsertParagraphAfter; // Position the range after the break
MyR.Collapse(ovDirection);
end;
MyR.InsertFile(wsFileName, EmptyParam, EmptyParam,
EmptyParam, EmptyParam);
end; |
|
| Back to top |
|
 |
Deborah Pate (TeamB) Guest
|
Posted: Tue Oct 03, 2006 9:04 pm Post subject: Re: Word: Bookmarks: Inserted Page break is not between the |
|
|
<<Sjors Dubbeljoe Boes:
MyR := BM.Range; //resets it at the right position, correct Ms.
Pate??
Yes. :)
--
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 |
|
 |
|
|
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
|
|