By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,900 Members | 1,367 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,900 IT Pros & Developers. It's quick & easy.

Word Template problem

P: n/a
I am facing a problem with Visual C# 2003 and MS Word XP. I am using a
customized template for Word and in that template I have defined some
bookmarks. I am trying to create a Word document based on the customized
template, reading all the bookmarks and replacing them with desired
values, resulting in a new one page document being created which is then
saved. This all works fine but when I try to extend the process so that
the template with the bookmarks (and the resulting document) spans
across multiple pages but I am unable to read the bookmarks past the
first page. For instance when I try to insert the text into the second
page what happens is the text starts immediately after the end of the
text on the first page, resulting in the second page starting on the
first with the overall formatting being lost. I have tried using the
page break command in C# to resolve this but with no joy. I have
supplied a small code example below. Any help would be appreciated.



private void button3_Click(object sender, System.EventArgs e)

{

try

{

object
lobjMissing=System.Reflection.Missing.Value;

Word.ApplicationClass lwapWordApp=new
Word.ApplicationClass();

object lobjNewTemplate=false;

object lobjDocType=0;

object lobjIsVisible=true;

object
lobjTemplateFileName="C:\\PPMIP_PATH\\TestField2.d ot";

object
lobjOutputFileName="C:\\PPMIP_PATH\\TestField2.doc ";

Word.Documents lvdocs =
lwapWordApp.Documents;//.Item(ref lobjCount);

Word.Document lwdcDoc=lvdocs.Add(ref
lobjTemplateFileName,ref lobjNewTemplate,ref lobjDocType,ref
lobjIsVisible);

Word.Bookmarks lvBookMarks=lwdcDoc.Bookmarks;

object unit;

object extend;

Word.Selection lvSel = lwapWordApp.Selection;

lvSel.WholeStory();

lvSel.Copy();

foreach(Word.Bookmark lwbkBookMark in
lvBookMarks)

{

switch(lwbkBookMark.Name)

{

case "Address":

Word.Range
lvRange=lwbkBookMark.Range;

lvRange.Text="Budge Budge";
//lwbkBookMark.Range.Text="Budge Budge";

break;

case "CustomerName":

lwbkBookMark.Range.Text="Raj";

break;

case "CRN1":

lwbkBookMark.Range.Text="CRN";

break;

case "RecoveryRate":

lwbkBookMark.Range.Text="123";

break;

case "TotalDebt":
lwbkBookMark.Range.Text="852471";

break;

case "CurrentDate":
lwbkBookMark.Range.Text=DateTime.Now.ToShortDateSt ring();

break;

}

}

object lbObj="ENDOFFILE";

Word.Bookmark lvBk=lvBookMarks.Item(ref lbObj);

if(lvBk.Name.ToUpper()=="ENDOFFILE")

{

object
lobjColl=Word.WdCollapseDirection.wdCollapseEnd;

object
lobjBrk=Word.WdBreakType.wdSectionBreakNextPage;

lvBk.Range.InsertBreak(ref lobjBrk);
lwapWordApp.ActiveWindow.ActivePane.VerticalPercen tScrolled=50;

object lobjCount=1;

unit=Word.WdUnits.wdLine;

extend=Word.WdMovementType.wdMove;

lvSel.MoveDown(ref unit,ref lobjCount,ref
extend);
lvSel.PasteAndFormat(Word.WdRecoveryType.wdPasteDe fault);

}

lwdcDoc.SaveAs(ref lobjOutputFileName,ref
lobjMissing,ref lobjMissing,ref lobjMissing,ref lobjMissing,ref
lobjMissing,ref lobjMissing,ref lobjMissing,ref lobjMissing,ref
lobjMissing,ref lobjMissing,ref lobjMissing,ref lobjMissing,ref
lobjMissing,ref lobjMissing,ref lobjMissing);

lwdcDoc.Close(ref lobjMissing,ref
lobjMissing,ref lobjMissing);

lwapWordApp.Quit(ref lobjMissing,ref
lobjMissing,ref lobjMissing);

}

catch(Exception ex)

{

MessageBox.Show(ex.Message);

}

}

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.