I have been trying to ad an image to a cell in a worksheet using openXML. Most solutions I have found while searching use code from or similar to the code at PolymathProgrammer website:
http://polymathprogrammer.com/2009/11/30/how-to-insert-an-image-in-excel-open-xml/
When I try this I get an error regarding NonVisualDrawingProperties, like this:
even though my usings should incluse it:
What am I doing wrong? Or is there some easier code I could be using?
This is because there are several NonVisualDrawingProperties in various namespaces and it's ambiguous as to which one you want here.
You can solve this by fully qualifying the name:
var nvdp = new DocumentFormat.OpenXml.Drawing.Spreadsheet.NonVisualDrawingProperties();
You can also alias the using statement to prevent having to type quite so much:
using SPD = DocumentFormat.OpenXml.Drawing.Spreadsheet;
...
var nvdp = new SPD.NonVisualDrawingProperties();
Related
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'm trying to add a map chart in PowerPoint.
As of now I can't see the option to do so in XlChartType, but if I insert one manually I can then examine the inserted chart and see that its XlChartType evaluates to 140.
If I try to insert a chart with this type I get a map chart as expected. However, if I try to access its workbook it throws an exception. These two lines of code should explain what I'm doing:
var chart = _slide.Shapes.AddChart((XlChartType)140).Chart;
var workbook = (Workbook)chart.ChartData.Workbook;
I assume this is related to the fact that it's not officially supported. Is there any way to work around this problem and edit the data of the chart?
I think you are missing call to Activate method.
Checkout remark from official source:
Note You must call the Activate method before referencing this property; otherwise, an error occurs.
Your code should be:
var chart = _slide.Shapes.AddChart((XlChartType)140).Chart;
chart.ChartData.Activate(); // missing piece
var workbook = (Workbook)chart.ChartData.Workbook;
I'm trying to read all the tables from a word file into a list, although for some reason the count is 0 regardless of how many tables are in the file. Here's my code.
public void FindAndReplace(string DocPath)
{
using (WordprocessingDocument doc = WordprocessingDocument.Open(DocPath, true))
{
using (StreamReader reader = new StreamReader(doc.MainDocumentPart.GetStream()))
{
//Text titlePlaceholder = doc.MainDocumentPart.Document.Body.Descendants<Text>().Where((x) => x.Text == "Compliance Review By:").First();
List<Table> tables = doc.MainDocumentPart.Document.Descendants<Table>().ToList();
System.Console.WriteLine(tables.Count);
tables.Count = 0. What am I doing wrong?
If all you're trying to do is READ the tables, then there's no need to open the document for editing (which is what you're doing currently)
Set the second parameter to false in WordprocessingDocument.Open() to open for reading. This will prevent the error related to opening an entry more than once in Update mode (I assume that's what you're running into anyway).
Solution based on chatter
The real culprit here has to do with using the wrong OpenXml namespace when examining tables in the document. When looking for Descendants of type Table, the passed-in type must be OpenXml.Wordprocessing.Table, NOT OpenXml.Drawing.Table
I don't know what type of object the OpenXml.Drawing.Table is used for. I'll ask about this in a separate question.
You probably are referencing a wrong Table. This should work:
var tables = doc.MainDocumentPart.Document.Descendants<DocumentFormat.OpenXml.Wordprocessing.Table>().ToList();
Anu start had the answer in the comments. The problem was that I was using an incorrect namespace. Instead of using DocumentFormat.OpenXml.Wordprocessing.Table I was using DocumentFormat.OpenXml.Drawing.Table
I have an Excel template which I am opening, which has a pie chart bound to a table (ListObject), and I want to insert rows into the table. There are plenty of examples of how to ADD a table into a worksheet, but I can't find anywhere that lets me select the table.
I have tried:
using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;
oWB = (Excel._Workbook)(oXL.Workbooks.Open(LastFile));
oSheet = (Excel._Worksheet)oWB.ActiveSheet;
Excel.ListObject ValList = oSheet.ListObjects("ValueData");
but that gives the compile error:
Non-invocable member 'Microsoft.Office.Interop.Excel._Worksheet.ListObjects' cannot be used like a method. (CS1955)
I have also tried doing it with a macro, then trying to convert the VB to C#, but still with no joy.
For information, I am relatively new to C# Excel automation, although I have done quite a lot in C# and have done Excel COM object work using other languages.
good morning,
mr.Reband is right, i think... maybee this is worth a look? as the error says, taken the MSDN link into account, i think, oSheet.ListObjects is a field or property, rather than a method. so, your invocation is not allowed. cheers!
I have a requirement to create read-only merge fields in Word.
I've already tried using the Locked property which can be seen below.The description of this property states - When a field is locked you cannot update field results which sounds like a perfect fit for my problem but this doesn't seem to be working
Below is the code i use to add the merge field to MS Word:
using Word = Microsoft.Office.Interop.Word;
Word.Range currentRange = Globals.ThisAddIn.Application.Selection.Range;
Word.MailMerge merge = Globals.ThisAddIn.Application.ActiveDocument.MailMerge;
merge.Fields.Add(currentRange, selectedNode.LocalName).Locked = true;
Once I run the code above and the field is created in Word, I am still able to right click it and select "Edit Field" where I can potentially rename the field or perform other changes without getting any errors or preventions from Word.
If anyone implemented something like this before, please share your knowledge.
Here is some insight on the technologies:
The solution is targeted at MS Word Office 2010
It must be written in .NET C# 3.5
Cannot use Open Xml SDK, the fix must be performed using Office Interop
The solution must achieve the desired goals without making the whole document read-only
Thank you for the replies, however I needed to only make the merge fields read-only the rest of the document should still stay as is.
A colleague of mine found a nice way to achieve what I was looking for, just sharing it in case someone else may need this functionality:
All you need to do is create a ContentControl object and add your merge field to the content control.
Set the LockContents property to true. This property is used to determine whether a user is allowed to edit the contents of a content control.
using Word = Microsoft.Office.Interop.Word;
object missing = System.Type.Missing;
Word.Selection PosRange = Globals.ThisAddIn.Application.Selection;
Microsoft.Office.Interop.Word.ContentControl cntCtrl;
cntCtrl = PosRange.Range.ContentControls.Add(Microsoft.Office.Interop.Word.WdContentControlType.wdContentControlRichText, ref missing);
object fldType = Microsoft.Office.Interop.Word.WdFieldType.wdFieldMergeField;
object fldText = "Employee";
Microsoft.Office.Interop.Word.Field fld = cntCtrl.Range.Fields.Add(cntCtrl.Range, ref fldType, ref fldText);
cntCtrl.LockContents = true;
In the image below a merge field is hosted inside a content control, note users are now unable to edit the field
Using the Locked property will only prevent the value of the field from changing, which I'm assuming is not what you want.
The only way I know to prevent the field code from changing is to protect the document. The fields themselves should still be updatable.