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.dot";
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.ToShortDateString();
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.VerticalPercentScrolled=50;
object lobjCount=1;
unit=Word.WdUnits.wdLine;
extend=Word.WdMovementType.wdMove;
lvSel.MoveDown(ref unit,ref lobjCount,ref
extend);
lvSel.PasteAndFormat(Word.WdRecoveryType.wdPasteDefault);
}
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);
}
}
"Peter Huang" [MSFT] - 30 Mar 2005 08:14 GMT
Hi
Based on my understanding, when you are automation MSWord XP with C# 2003,
you find that you can not locate the insert point with the predefined
bookmark in a template.
I think you may try the steps below to see if that works for you.
1. Have you tried to use the Office XP PIA which is the official Primary
Interop Assembly for Office XP?
You may try to install it to see if that helps.
We can get it from the link below.
Working with the Office XP Primary Interop Assemblies
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnoxpta/htm
l/odc_oxppias.asp
Office XP PIAs
Download the Office XP PIAs and use them to develop Office XP solutions
with Visual Studio .NET.
http://www.microsoft.com/downloads/details.aspx?FamilyId=C41BD61E-3060-4F71-
A6B4-01FEBA508E52&displaylang=en
2. Have you tried to do the similar job in the work vba code?
To isolate the problem, we may try to make simplest template,e.g. build a
blank template with just one or two bookmark to see if the problem
persists. And then use the VBA/C# code to see if the inserted text is
located correctly with bookmark.
If the problem persists in VBA/C#, I think we may need to try to look into
the template.
If the problem persists in just C# with PIA installed with a simple
template, can you provide the template with the test C# code for us to do
further troubleshooting.
Please perform the steps and let me know the result and I will appreciate
your efforts.
If you still have any concern, please feel free to post here.
Best regards,
Peter Huang
Microsoft Online Partner Support

Signature
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.