473,586 Members | 2,546 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(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!
Nov 16 '05 #1
0 4018

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

Similar topics

5
7408
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 collection that I need. To begin, I am using xsltproc that conmes with Cygwin as my processor. I have no particular affinity to this processor except...
0
1536
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 3. After opening a document on that template, I put "2" in all the fields for test Problem is,
4
21473
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 AND CLIENTS. The word (it is a template) is named MASTER To store data, I have created two forms named CASES and MASTER. Since both forms have a lot...
1
4940
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 1. Query..I want to keep the fields seperatred. I do not want to sent on field with all accumulated languages from one person to Word. Each...
4
2689
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 the final output as a Word document. I know there are different options to do this. But I can't decide which one I should use. I don't want to use...
2
13496
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 execute a MailMerge to create mailing labels or customized letters. A label name known to MS Word MailMerge mailing label wizard may be used or a...
1
2335
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 from template (.dot) located on my serveur. When the application run i get an error. In fact, the problem come when i put an URL like...
12
10647
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
1872
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 match with the field names in Outlook form ' - value in form is that populated to the Word doc for auto-printing ' - prompt user for closing Word...
0
7915
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7841
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8204
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7965
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6617
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5392
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3869
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1184
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.