I am using Word.Interop in c# to inject data into a word document whch serves as a template for me.
This document has a table I wish to fill.
like this:
I am inserting the text like this (simpilified):
String text = "1" + "\t" + "2" + "\t" ; //Etc..
But this is not working.
Any idea how I should do it?
This will not work. If you want to create a new table in a word document, set a bookmark in the document where you want the table to be created, then create the table as described here:
http://msdn.microsoft.com/en-us/library/vstudio/w1702h4a.aspx
If you have an existing fixed table in the document, place custom document properties in the cells, then set their values like this:
thedocument.CustomDocumentProperties["NameOfTheCustomProperty"].Value = 1;
Related
I'm looking for a solution in ASPOSE .Net to decrement the NUMPAGES. Reason is that I don't want to count the last page of the document. Here is what I tried so far:
builder.Write("Page ");
builder.InsertField("Page", "");
builder.Write(" of ");
builder.InsertField("NUMPAGES", $"{(doc.PageCount - 1)}");
// Another try in separate build
builder.InsertField("NUMPAGES - 1", "");
// Another try in separate build
builder.InsertField("NUMPAGES", "NUMPAGES - 1");
Document either doesn't display anything or count the last page as well.
You should use formula and nested NUMPAGES field to get the desired output. Field code in your MS Word document should look like this:
{={NUMPAGES}-1}. To insert such field using Aspose.Words you can use code like this:
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert formula field
Field formula = builder.InsertField("=", "");
// Move document builder inside field code of the inserted field
// And put NUMPAGES field in it.
builder.MoveTo(formula.Separator);
builder.InsertField("NUMPAGES");
builder.Write("-1");
doc.UpdateFields();
doc.Save(#"C:\Temp\out.docx");
Please see Aspose.Words documentation to learn how to work with fields.
I use EPPLus to create a excel file with a table in it. Creating the excel file and table works like a charm using:
worksheet.Cells["A1"].LoadFromCollection(rows, true, TableStyles.Light12);
...but when I try to insert a formula into the first empty row after the table, the file becomes corrupt and cannot be opened normally. Excel can recover it though, but the formula I added will never show.
I use this line of code to create the formula:
worksheet.Cells["E"+(rows.Count+2).ToString()].Formula = "=SUBTOTAL(109;[TurnoverCurrent])";
I've already confirm that I'm targeting the correct cell, and if I insert just a string instead of a formula, it works. Hence my feeling is that I don't use the formula method correctly. How can I achieve a SUBTOTAL formula after my table?
Three things jump out at me, and I'm not sure if either/or will fix it, but I thought to offer.
1) your subtotal formula is right next to the table -- when Excel renders the XML, is it trying to add this to the table and freaking out because it's self-referencing? In other words, the subtotal formula is now part of the table and adding itself?
2) when I use subtotal, I use a comma, not a semicolon
3) you didn't reference the table name in your formula. Since this is a brand new sheet, it's safe to try Table1. If you really want to be fancy, you can look up the table and find its name. I wouldn't.
So maybe add one row, prefix the table name and change your semicolon to a comma?
worksheet.Cells[rows.Count + 3, 5].Formula = "=SUBTOTAL(109,Table1[TurnoverCurrent])";
Change
worksheet.Cells["E" + (rows.Count+2).ToString()].Formula = "=SUBTOTAL(109;[TurnoverCurrent])";
to
worksheet.Cells["E" + (rows.Count+2).ToString()].Formula = "SUBTOTAL(109;[TurnoverCurrent])";
Do nut use the "="
I have a word document template which contains several form fields that need to be filled using c# code.
below is the document image
The code below is used to reach and fill the document form fields,
But when i reach the table sections sometimes the rows need to be filled are more than what is pre defined inside the template.
red marked area is the table which i want to fill it with data and create as many rows as needed.
the code i use for filling the data is
string fileName = Path.GetTempFileName();
File.WriteAllBytes(fileName, Properties.Resources.DocumentTemplate);
Word.Application word = new Word.Application();
Word.Document doc = new Word.Document();
doc = word.Documents.Add(fileName);
doc.Activate();
doc.FormFields["file_num"].Range.Text = "some text";
doc.FormFields["fam_size"].Range.Text = "another text";
doc.FormFields["nationality"].Range.Text = "another text";
for(int i =0; i< rowsInDatabaseCount; i++)
{
//i don't know how to add row and reach each form field inside
}
I hope someone can help me on how to achieve what i want.
Thank you in advance...
There are multiple ways to handle that.
1) Since the data is coming from a database, one way is to use InsertDatabase method.
2) You could insert the block as tab or comma separated text then convert to a table using ConvertToTable() method.
3) You might use Rows and Cols collections (of Table) and .Add method to add new rows.
4) You might instead create your template as an XSL and transform data (XML) using that XSL to generate final HTM. Word is able to open an HTM file.
(All these options worked for me in "resume", "test results" ... creations which have similar layouts to the ones you gave. I found final option 4 to be the most dynamic one, where end users could define their own templates using simple HTML editors - worked better than word templates for me).
My requirement is to create a word document dynamically i.e. create a document with template having data placeholders like version, author, header etc. How can I create this template & open the document and replace the placeholder with actual data by code (C#)? I have to put placeholders in document header/footer, document page & watermark.
Also, how can I pass data from other project to VSTO project?
Please suggest me some demo videos, user guide, tutorial or manual regarding the same? Thanks.
Firstly you need to create your template with fields or bookmarks to contain your data.
Using bookmarks you create your template and add bookmarks where you want to insert your data. In your VSTO addin would fill the bookmark with the following. This example is from an application level addin
For the sake of this I'll assume you are retrieving data from a data base and that you have a bookmark called ProductName
Microsoft.Office.Interop.Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;
string productName = GetProductNameFromDatabase();
doc.Bookmarks["ProductName"].Range.Text = productName;
I am afraid this only answers the specific question of adding data to a word document.
Hello I am automating a word document from a winforms application using c#. The document contains a number of mail merge fields that I fill from my code. This works fine for fixed fields.
However the document contains a few recurring sections that are repeated n times at run time. Can someone tell me how to create a block of some sort that can be inserted multiple times?
I have tried inserting the text between two bookmarks however this messes up the page formatting.
Any advice would be much appreciated.
Edit:
The function below is what I use to merge the data on to the word template. The Dictionary holds the field name in the first string and the content in the second.
The user is given a sample word template with all the field codes in the document and is then able to change the order of the fields and the styling on his own.
public static void CreateBody(Application wApp, Document wDoc, Dictionary<string, string> fieldContent)
{
//Loop through fields in document body.
foreach (Field mergeField in wDoc.Fields)
{
//Find mailmerge fields in the Word document body.
if (mergeField.Type == WdFieldType.wdFieldMergeField)
{
//Get the fieldName from the template file.
string fieldText = mergeField.Code.Text;
string fieldName = GetFieldName(fieldText);
if (fieldContent.ContainsKey(fieldName))
{
mergeField.Select();
if (fieldContent[fieldName] != "")
{
wApp.Selection.TypeText(fieldContent[fieldName]);
}
else
{
//If the field has no content remove the field text from the word file.
wApp.Selection.TypeText(" ");
}
}
}
}
Now I am looking for a way to repeat a block of mail merge fields in the template and have them be inserted multiple times, once for each record.
Here is MSDN link which help you to resolve your issue.
Building a Word Document Using SQL Server Data