I have managed to keep Microsoft Word's track revisions property on. It is working fine.
How do I do the same for MS Excel 2007 and above? Follwing is my code for word (which is working fine) and next is for Excel that I am trying to execute.
Word:
Word.Application app = new Word.Application();
Word.Document tempDoc = app.Documents.Open(path);
tempDoc.TrackRevisions = true;
tempDoc.Protect(typ, ref missing, ref password, ref missing, ref missing);
Excel:
Excel.Application ex_APP = new Excel.Application();
Workbook wrk = ex_APP.Workbooks.Open(path);
not able to go past this. When I try doing
wrk.
I don't get property as trackReviosons.
This is the way I am currently doing it it seems to work really well
using Excel = Microsoft.Office.Interop.Excel;
Then you get active workbook
//Gets Excel and gets Activeworkbook and worksheet
Excel.Application oXL;
Excel.Workbook oWB;
Excel.Worksheet oSheet;
oXL = (Excel.Application)Marshal.GetActiveObject("Excel.Application");
oXL.Visible = true;
oWB = (Excel.Workbook)oXL.ActiveWorkbook;
docProps = oWB.CustomDocumentProperties
Related
I have an excel sheet with 47 columns and about 100 rows. I have a Windows form application with a text field, a button and a label.
Here's what I am trying to do:
When I paste a number into the text box and click the button, it searches the entire excel sheet and fetches the row and column of where that particular string is in the sheet (row, column). This is working fine. I am ok with the performance. Here's what I'd actually like to do -
After I get where the searched string is, I'd like to go to the 45th column, of that same row and get the string from that cell, and display it in the label.
Here's my code (Nothing I did is working!)
string File_name = "C:\\Users\\v-nikken\\Documents\\My Received Files\\Case Wellness.xlsx";
Microsoft.Office.Interop.Excel.Application oXL = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook oWB;
Microsoft.Office.Interop.Excel.Worksheet oSheet;
try
{
object missing = System.Reflection.Missing.Value;
oWB = oXL.Workbooks.Open(File_name, missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing,
missing, missing, missing, missing);
oSheet = (Microsoft.Office.Interop.Excel.Worksheet)oWB.Worksheets[1];
Microsoft.Office.Interop.Excel.Range oRng = GetSpecifiedRange(textBox_SRNumber.Text, oSheet);
if (oRng != null)
{
//THIS IS THE LOGIC I HAVE TO TAKE VALUE FROM THE CELL TO THE LABEL
//AND OBV IT ISN'T WORKING
int IRPlace = Convert.ToInt32(oRng.Column) + 46; //This is obv wrong
label_IRMet.Text = Convert.ToString(oSheet.Cells[Convert.ToInt32(oRng.Row), IRPlace]); //This also
label_scope_sent.Text = IRPlace.ToString();
}
else
{
MessageBox.Show("Case number not found!", "Please try again");
}
oWB.Close(false, missing, missing);
oSheet = null;
oWB = null;
oXL.Quit();
}
catch (Exception ex)
{
}
Sorry about the formatting. The code section wasn't working for some reason.
Please help!!
Windows 10 - Excel 2016 - VS 2017 - .net 4.6.1
Microsoft.Office.Interop.Excel.Range valueForLabel=(Microsoft.Office.Interop.Excel.Range)yourSheet.Cells[oRng.Row,IRPlace];
string labelText=valueForLabel.Value.ToString();
I'm trying to edit an Excel Sheet through C# code but the Excel sheet is giving me an error saying read only
CS0200 Property or indexer 'Range.Text' cannot be assigned to -- it is read only
This is the code that is trying to access the sheet.
Code:
//Load workbook
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(#"S:\Utils\documents\ServerManager\serverlist.xlsx");
Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
Excel.Range xlRange = xlWorksheet.UsedRange;
xlWorksheet.Range["D2"].Text = "Kelly Cooper";
xlWorksheet.Range["D2"].Style.Font.FontName = "Arial Narrow";
xlWorksheet.Range["D2"].Style.Font.Color = Color.DarkBlue;
In the properties the Excel sheet is not read only and when I open it to edit it does not prompt me with the "This document is read only, want to enable editing." How can I go around this, Visual Studio won't let me compile because of it.
This is Excel in Office365.
You want to use .Value instead of .Text.
xlWorksheet.Range["D2"].Value = "Kelly Cooper";
xlWorksheet.Range["D2"].Style.Font.FontName = "Arial Narrow";
xlWorksheet.Range["D2"].Style.Font.Color = Color.DarkBlue;
If you check the documentation you can see that Text is read-only.
Also, if you check the documentation you can see that Value is not read-only.
EDIT
You also might have to change your Open parameters to:
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(Filename: "path/to/file", ReadOnly: false);
I am currently trying to edit cells of an excel spreadsheet object with c# interop. I inserted it in a word document as an object.
Until there i didn't succeed to programm anything that really works. I'm able to select the component but i can't open it to edition and then reach grid's cells.
I use a button control in a custom office ribbon to launch edit. Here is my method:
public void EditTable(Office.IRibbonControl control)
{
Word.Application oWordApp = (Word.Application)Marshal.GetActiveObject("Word.Application");
Word.Document oWordDoc = oWordApp.ActiveDocument;
Word.Bookmark ReqsBookmark = DocumentHelper.GetBookmark("test");
ReqsBookmark.Select();
}
The only way i know to access a specific object with interop is with bookmarks.
Does anybody have an idea of how doing such a thing?
In Word, an Excel worksheet (workbook) is "wrapped" in an OLE control that is a member of the InlineShapes or Shapes collection. So you need the AddOLEObject method of the collection you want to use.
Access to the object model of the OLE server (Excel) is through the OLEFormat property of the InlineShape or Shape. So your code would be something like the sample below.
Note that although you say this is a VSTO project, the code you show us is not VSTO. You're starting up a new instance of the Word.Application, but the VSTO Add-in would be running in-process. My code is VSTO code, but can certainly be adjusted for other situations...
{
Word.Document doc = Globals.ThisAddIn.app.ActiveDocument;
object oRngTarget = Globals.ThisAddIn.app.Selection.Range;
//object oRngTarget = DocumentHelper.GetBookmark("test").Range;
object oOLEClass = "Excel.Sheet.12";
object oFalse = false;
Word.InlineShape ils = doc.InlineShapes.AddOLEObject(ref oOLEClass, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref oRngTarget);
Word.OLEFormat olef = ils.OLEFormat;
System.Globalization.CultureInfo oldCI= System.Threading.Thread.CurrentThread.CurrentCulture;
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
Excel.Workbook wb = (Excel.Workbook)olef.Object;
Excel.Worksheet ws = (Excel.Worksheet)wb.Worksheets[1];
try
{
ws.get_Range("A1").Value2 = "New category";
ws.get_Range("B1").Value2 = 6.8;
}
catch (Exception ex)
{
System.Diagnostics.Debug.Print(ex.Message);
}
finally
{
ws = null;
wb = null;
ils = null;
doc = null;
System.Threading.Thread.CurrentThread.CurrentCulture = oldCI;
}
}
To later work with a spreadsheet in the Word document, you basically follow the same principle: declare and instantiate a InlineShape.OLEFormat object, Activate it, then cast olef.Object to an Excel.Workbook:
olef.Activate();
Excel.Workbook wb = (Excel.Workbook)olef.Object;
I finally succeeded thanks to this post about modify an embedded Excel object inside a Word doc
Here's the c# method if anybody need it one day:
{
Word.Document oWordDoc = Globals.ThisAddIn.Application.ActiveDocument;
Excel.Workbook oOLE = null;
Excel.Worksheet oSheet = null;
Word.InlineShapes ils = oWordDoc.InlineShapes;
ils[1].OLEFormat.Activate();
oOLE = ils[1].OLEFormat.Object;
oSheet = oOLE.Worksheets[1];
oSheet.get_Range("A1").Value = "I did it too!";
}
Thank you again #CindyMeister for your answer, it helped me to understand how it really works.
I have a problem with Excel worksheet. I am trying to create an Excel file with c#.
This code works and runs correctly on my computer but in other computers get an error at last line:
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheetInvoice;
Excel.Worksheet xlWorkSheetInvoiceLine;
object misValue = System.Reflection.Missing.Value;
xlApp = new Microsoft.Office.Interop.Excel.Application();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheetInvoice = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
xlWorkSheetInvoiceLine = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(2);
System.Runtime.InteropServices.COMException (0x8002000B): Invalid index. (Exception from HRESULT: 0x8002000B (DISP_E_BADINDEX))
at Microsoft.Office.Interop.Excel.Sheets.get_Item(Object Index)
I executed a sample application with similar code on a machine with Excel 2013 and it fails at the same line of code you mentioned. By default Excel 2013 application opens up with a single worksheet ("Sheet1") so you would have to modify the code accordingly
DISP_E_BADINDEX seems to suggest the number of worksheets is less on the other computers. Build in a check to see if the number of worksheets is less than 2 before using get_Item().
i have created new sheet and assigned xlWorkSheetInvoice and xlWorkSheetInvoiceLine to solve it.
var xlSheets = xlWorkBook.Sheets as Excel.Sheets;
var xlNewSheet = (Excel.Worksheet)xlSheets.Add(xlSheets[1], Type.Missing, Type.Missing, Type.Missing);
var xlNewSheet2= (Excel.Worksheet)xlSheets.Add(xlSheets[2], Type.Missing, Type.Missing, Type.Missing);
xlWorkSheetInvoice = xlNewSheet;
xlWorkSheetInvoiceLine = xlNewSheet2;
xlWorkSheetInvoice = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
xlWorkSheetInvoiceLine = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(2);
...
Excel.Application oXL;
Excel._Workbook oWB;
Excel._Worksheet oSheet;
oXL = new Excel.Application();
oWB = (Excel._Workbook)oXL.ActiveWorkbook;
oSheet = (Excel._Worksheet)oWB.Sheets[1];
oSheet.Cells[5,10] = "Value";
...
yields this at crash:
Unhandled Exception: System.NullReferenceException: Object reference not set to
an instance of an object.
at ConsoleApplication1.Program.Main(String[] args) in C:\Wherever\Visual Studio 2008\Projects\ConsoleApplication20\ConsoleApplication20\Program.
cs:line 60
In this case, line 60 is
oSheet = (Excel._Worksheet)oWB.Sheets[1];
and the same thing happens if the line is written
oSheet = (Excel._Worksheet)oWB.ActiveSheet;.
Excel is already open onscreen at the time, with a fresh worksheet in place.
The error is telling you that oWB is null. It is null because unlike opening Excel from a GUI, automation does not create a new 3-sheet book. You need to specifically load a book first, or add one.
See example here http://support.microsoft.com/kb/302084 where it explicitly adds a new workbook to play with
//Get a new workbook.
oWB = (Excel._Workbook)(oXL.Workbooks.Add( Missing.Value ));