tags:

views:

85

answers:

1

Hi All,

I need to create an word document based on the template in c#. I have tags for only the paragraphs. Is there any way to replace the bullets and tables that are already available in the template based on the user input.

I was able to replace the paragraph with input text using the Replace command in the Word InterOp.

Need help to do the rest of the items.

  1. Replace the bullets based on the user input
  2. Fill the tables with the input values

Code for replacing the Paragraph based on the tag:

FindAndReplace(wordApplication, "/date/", DateTime.Now.Date.ToString("MMM dd, yyyy"));

FindAndReplace(){

    wordApplication.Selection.Find.Execute(ref findText,
            ref matchCase, ref matchWholeWord, ref matchWildCards, ref matchSoundsLike,
            ref matchAllWordsForms, ref forward, ref wrap, ref format, ref replaceWithText, ref replace, ref matchKashida,
            ref matchDiacritics, ref matchAlefHamsa, ref matchControl);

}

Thanks in Advance. ASAP

+2  A: 

I'd suggest putting bookmarks into your Word document and using them to assign data.

object oBookMark = "MyBookmark";
oWordDoc.Bookmarks.Item(ref oBookMark).Range.Text = "Some Text Here";

You can populate tables using this method and I imagine you should be able to populate bullet points.

This is the generally accepted way to programmatically populate a Word template. Matching strings is difficult to write and maintain accurately and can easily yield unexpected results. With a bookmark, you know what data you're assigning and exactly where it's going.

Take a look at this Coderush project.

David Neale
Bookmark is working for Paragraph. But not sure how to do for Bulleted list and for tables.
Siva
Please suggest some idea..Code sample would help me to start off..
Siva
There's not much more to the code that the above. As far as adding bookmarks is concerned - take a look here: http://www.ehow.com/how_2093405_add-bookmark-microsoft-word.html
David Neale
David's right here. For bullet lists, just have a single bullet with a bookmark. Add inputed text there and then do a carriage return after - Word will know it's a bookmark if the bullet list isn't broken. Tables will be super easy - just place a bookmark in cell 0,0 and from there you can place in all other cells. VBA, but this is a good place to get started if you're not familiar with Office programming - http://www.thezcorp.com/VBACodeSamples.aspx
Otaku