473,406 Members | 2,378 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Word Template problem

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
0 4003

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Clifford W. Racz | last post by:
Has anyone solved the issue of translating lists in Word 2003 (WordML) into xHTML? I have been trying to get the nested table code for my XSLT to work for a while now, with no way to get the...
0
by: Sudipto Sen | last post by:
================== Problem Description ================== Following code is for 1. Load a Word Template called CustomerInfo1.dot 2. CustomerInfo1.dot is a normal Word Template with Form fields...
4
by: Paolo | last post by:
Hello, I am trying to create a procedure to export my Access data to a word template. I would like to export data from two tables to a word file at the same time. My two tables are named CASES...
1
by: svdh | last post by:
I have posed a question last saturday and have advanced alot in the meantime. But I am still not there Problem is that I try to merging various fields from various tables in one document in Word...
4
by: Nikhil Patel | last post by:
Hi all, I need to generate a word document and save it on the server from an ASP.Net application. Basically I want to load a word template and insert some field values from a dataset and save...
2
by: Mikey | last post by:
Sample VB .NET source code to create mailing labels or customized letters using MS Word MailMerge This VB .NET source code will start MS Word and call methods and set properties in MS Word to...
1
by: Sylum | last post by:
Hi everyone, I'm coding an web application with Javascript and ActiveXWord 2000. Basicly, on the client side, i use javascript to create an instance Word 2000 in order to have a new document...
12
by: Dixie | last post by:
Is there a way to shell to Microsoft Word from Access and load a specific template - using VBA? dixie
1
by: belinda | last post by:
I have used the ff command but only get the date printout and have no clue whats wrong: 'Printing function ' - opening a Word template with bookmarks ' - read bookmarks list from template and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.