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(o bject sender, System.EventArg s e)
{
try
{
object
lobjMissing=Sys tem.Reflection. Missing.Value;
Word.Applicatio nClass lwapWordApp=new
Word.Applicatio nClass();
object lobjNewTemplate =false;
object lobjDocType=0;
object lobjIsVisible=t rue;
object
lobjTemplateFil eName="C:\\PPMI P_PATH\\TestFie ld2.dot";
object
lobjOutputFileN ame="C:\\PPMIP_ PATH\\TestField 2.doc";
Word.Documents lvdocs =
lwapWordApp.Doc uments;//.Item(ref lobjCount);
Word.Document lwdcDoc=lvdocs. Add(ref
lobjTemplateFil eName,ref lobjNewTemplate ,ref lobjDocType,ref
lobjIsVisible);
Word.Bookmarks lvBookMarks=lwd cDoc.Bookmarks;
object unit;
object extend;
Word.Selection lvSel = lwapWordApp.Sel ection;
lvSel.WholeStor y();
lvSel.Copy();
foreach(Word.Bo okmark lwbkBookMark in
lvBookMarks)
{
switch(lwbkBook Mark.Name)
{
case "Address":
Word.Range
lvRange=lwbkBoo kMark.Range;
lvRange.Text="B udge Budge";
//lwbkBookMark.Ra nge.Text="Budge Budge";
break;
case "CustomerNa me":
lwbkBookMark.Ra nge.Text="Raj";
break;
case "CRN1":
lwbkBookMark.Ra nge.Text="CRN";
break;
case "RecoveryRa te":
lwbkBookMark.Ra nge.Text="123";
break;
case "TotalDebt" :
lwbkBookMark.Ra nge.Text="85247 1";
break;
case "CurrentDat e":
lwbkBookMark.Ra nge.Text=DateTi me.Now.ToShortD ateString();
break;
}
}
object lbObj="ENDOFFIL E";
Word.Bookmark lvBk=lvBookMark s.Item(ref lbObj);
if(lvBk.Name.To Upper()=="ENDOF FILE")
{
object
lobjColl=Word.W dCollapseDirect ion.wdCollapseE nd;
object
lobjBrk=Word.Wd BreakType.wdSec tionBreakNextPa ge;
lvBk.Range.Inse rtBreak(ref lobjBrk);
lwapWordApp.Act iveWindow.Activ ePane.VerticalP ercentScrolled= 50;
object lobjCount=1;
unit=Word.WdUni ts.wdLine;
extend=Word.WdM ovementType.wdM ove;
lvSel.MoveDown( ref unit,ref lobjCount,ref
extend);
lvSel.PasteAndF ormat(Word.WdRe coveryType.wdPa steDefault);
}
lwdcDoc.SaveAs( ref lobjOutputFileN ame,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(r ef lobjMissing,ref
lobjMissing,ref lobjMissing);
lwapWordApp.Qui t(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!