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).
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 need to create a new word 2016 file, using VS2017, insert content (that's the easy part), and also to control it like doing the following:
Merge certain cells in same row, or same column
Define Right to Left or LTR
color the text/the background.
and more similar tasks.
I can open a document using
using Microsoft.Office;
using Word = Microsoft.Office.Interop.Word;
I can add text and save the document, yet still I don't see a way to fine control the color/direction and more parameters. After reading the documentation, it seems that this is probably not supported, unless I missed it.
I would appreciate if anyone can guide to a detailed documentation how to edit a word file from C# program.
Anyway, I can bypass it by creating an excel file which is simple using Interop and then insert it.
Here is a working solution for merging cells in a table, using VS2017 c#
var doc = DocX.Create(word_fname);
Table table = doc.AddTable(tableSize, 3);
table.Rows[row_cnt].MergeCells(1, 2); // to merge the 2nd & 3rd cells in the specific row
I am writing a program which will run and refresh a bunch of Excel Files and Textbox Documents within the file. Using the .RefreshAll() Method, I can refresh the linked tables within the file, as well as the text documents that are linked. However, one function I need to add is to refresh the documents without updating the tables.
After searching on here, and MDSN, I can't seem to pinpoint the thing I need. Is anyone able to point me in the right direction?
Thanks!
If you want to update links to Excel files and not the Linked Data Tables (ListObjects), then you can just iterate through each link and refresh it individually:
using Excelx = Microsoft.Office.Interop.Excel;
Excelx.Workbook wb = xlApp.ActiveWorkbook;
object links = wb.LinkSources(Excelx.XlLink.xlExcelLinks);
Array linkz = (Array)links;
for (int i = 1; i <= linkz.Length; i++)
{
wb.UpdateLink(linkz.GetValue(i).ToString(), Excelx.XlLinkType.xlLinkTypeExcelLinks);
}
The initial part seems like it could theoretically be compressed, but I've never had much luck consolidating those statements.
I have some VBA code that iterates through a document to remove tables from a document. The following code works fine in VBA:
Set wrdDoc = ThisDocument
With wrdDoc
For Each tbl In wrdDoc.Tables
tbl.Select
Selection.Delete
Next tbl
End With
Unfortunately, I cannot easily translate this code to C#, presumably because there is a newer Range.Find method. Here are three things I tried, each failing.
First attempt (re-write of the VBA code):
foreach (var item in doc.Tables)
{
item.Delete; //NOPE! No "Delete" function.
}
I tried this:
doc = app.Documents.Open(sourceFolderAndFile); //sourceFolderAndFile opens a standard word document.
var rng = doc.Tables;
foreach(var item in rng)
{
item.Delete; //NOPE! No "Delete" function.
}
I also tried this:
doc = app.Documents.Open(sourceFolderAndFile); //sourceFolderAndFile opens a standard word document.
var rng = doc.Tables;
Range.Find.Execute(... //NOPE! No Range.Find available for the table collection.
...
Could someone please help me understand how I can use C# and Word Interop (Word 2013 and 2016) to iterate through a document, find a table, and then perform a function, like selecting it, deleting it, or replacing it?
Thanks!
It took me some time to figure this answer out. With all the code samples online, I missed the need to create an app. For posterity, here is how I resolved the problem.
Make sure you have a Using statement, like this:
using MsWord = Microsoft.Office.Interop.Word;
Open the document and then work with the new msWord reference, the range, and the table. I provide a basic example below:
//open the document.
doc = app.Documents.Open(sourceFolderAndFile, ReadOnly: true, ConfirmConversions: false);
//iterate through the tables and delete them.
foreach (MsWord.Table table in doc.Tables)
{
//select the area where the table is located and delete it.
MsWord.Range rng = table.Range;
rng.SetRange(table.Range.End, table.Range.End);
table.Delete();
}
//don't forget doc.close and app.quit to clean up memory.
You can use the Range (rng) to replace the table with other items, like text, images, etc.
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;