Refresh Text Box (Document) in Excel using C# - c#

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.

Related

How to put a table from excel into a scrollable list of items in C#?

I have a table in an excel sheet with two columns and about 400 rows. Very simple. All I want is to now put that data into a scrollable list on a form in winforms.
I've been at this for awhile and have become frustrated. Everything I find is showing me how to add an item to a listview one by one, I assume there's an easier way.
This list is static, it will never need to change. How would I do this?
Holding data in source code is generally not a good idea. I think, there is no such thing as 'never changes'. I understand you don't want to implement the Excel-Stuff, but you may put the data in a Json, whatever... and use it as resource.
Anyway, you can use Excel functions to prepare your data for copy-paste.
Sorry for the german Excel, I think the english name of the function would be 'Concatenate'
then you copy the cells and paste it in your Code (assuming your variable is called 'list' and it's instantinated already...)
don't forget about the extra parenthesis in the excel function for strings!
Try this:
Type ExcelType = Type.GetTypeFromProgID("Excel.Application");
dynamic Excel = Activator.CreateInstance(ExcelType);
int Index = 1;
Excel.WorkBooks.Open("Your workbooks file location here...");
do
{
listBox1.Items.Add(Excel.Range("A" + Index).Value);
Index += 1;
} while (Excel.Range("A" + Index).Value != null);
Excel.Quit();
Excel = null;

c# controlling word file table

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

Add dynamic table row using microsoft work automation c#

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).

Bug while using Excel Interop FreezePanes : resulting workbook loses its structure

I've been trying for a while to run the following code with Microsoft.Excel.Interop on a new sheet which I add to an existing workbook and fill dynamically, then for display purpose I set these 3 properties:
worksheet.Application.ActiveWindow.SplitRow = 4;
worksheet.Application.ActiveWindow.SplitColumn = 1;
worksheet.Application.ActiveWindow.FreezePanes = true;
Everytime I have the same bug in the resulting workbook : all the other worksheets which were already with frozen panes, become unstructured (half of column B overlaps column C, and other interesting graphical details...).
What I have found out while checking other libraries of Excel automation (ClosedXML for example) is that others modify directly the XML code, so they don't use the ActiveWindow, but directly the worksheet itself.
Is it possible that my problem comes from the fact that I modify a property of the ActiveWindow improperly?
If yes, does somebody know whether it's possible to freeze panes without doing it through the ActiveWindow ?
If no, does somebody have an idea where it can come from ?
Thanks in advance
Roddhes
Have You activated appropriate worksheet before setting the properties? I mean something like:
Worksheet.Activate();
Worksheet.Application.ActiveWindow.SplitRow = 4;
Worksheet.Application.ActiveWindow.SplitColumn = 1;
Worksheet.Application.ActiveWindow.FreezePanes = true;
As a help, You can find some code samples for freezing the top row here (be sure to read the comments there for additional code improvements): https://stackoverflow.com/a/5060792/232530
It seems that freezing panes can be done using OpenXML as well. It's described in another SO thread: Freeze Panes in OpenXml SDK 2.0 for Excel document

Save specific pages of an Excel workbook

I have one workbook with three sheets. Each sheet got 3 pages. What I want to reach is: I want to save only the first page of each sheet.
I can only count those pages with
int numberOfPages = 0;
foreach(Excel.Worksheet sheet in excelWorkbook.Sheets)
{
numberOfPages += sheet.PageSetup.Pages.Count;
}
But I cant find a way how to save these pages. Is there a way?
Here is how to copy a worksheet:
Excel.Worksheet worksheet1 = ((Excel.Worksheet)Application.ActiveWorkbook.Worksheets[1]);
Excel.Worksheet worksheet3 = ((Excel.Worksheet)Application.ActiveWorkbook.Worksheets[3]);
worksheet1.Copy(worksheet3);
Hope that helps.
I'd suggest using the Macro Recorder in such cases (which is available in Excel). The required VBA code can generated automatically in the background for you. Most probably you will need to correct it because an auto-generated code is not well-optimized, but at least you will have an idea what properties and methods should be used to get the job done. See Create or delete a macro for more information.

Categories