Getting workbooks from MS Word in C# - c#

In my application, I created excel workbooks in microsoft word document using this code :
wordApp.Selection.InlineShapes.AddOLEObject("MSGraph.Chart.8", excellApp.ActiveWorkbook.Name, false, false, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
Then I need to get that excel workbooks from word document, how can i do that?
I also tried:
Word.InlineShapes shapes = wordApp.Selection.InlineShapes;
However, shapescount equals to zero.

Word.InlineShapes shapes = wordApp.ActiveDocument.InlineShapes;
should work

Related

c# - Can I retain the format for cells when copying excel sheets using Microsoft.Office.Interop.Excel?

I have some code that copies all sheets from one excel file into another. This code is as follows:
Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook source = ExcelApp.Workbooks.Add(missing);
Microsoft.Office.Interop.Excel.Workbook destination = ExcelApp.Workbooks.Add(missing);
string file = #"C:\SomeExcelFile.xlsx";
source = ExcelApp.Workbooks._Open
(file, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing
, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
foreach (Microsoft.Office.Interop.Excel.Worksheet s in source.Worksheets)
{
s.Copy(Type.Missing, destination.Worksheets[destination.Worksheets.Count]);
}
In most cases this works as expected but there are two issues that I want to resolve. The first is that in one of the excel workbooks I am copying there is a column that is formatted like this:
$#,##0.00;[Red]-$#,##0.00
When running this through my code this column gets reformatted to this:
$#,##0.00_);[Red]($#,##0.00)
Is there a way I can retain the formatting of the original file?
Secondly, I'd like the widths of the columns to remain the same as the original file. Is this possible too?
This code is being used to merge multiple excel workbooks into a single file.
Thanks

C# can't open excel file

When trying to run these lines:
var app = new Excel.Application();
Excel.Workbook workbook = app.Workbooks.Open(pathToFile, Type.Missing, true, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
I've got these problems:
1) I'm getting a message of "file conversion is in progress". Why do I need to convert something if all I want to do is just read a column from one of the work sheets.
2) I'm getting this error message:
why do I need to care which version created the excel?
3) In the very end I get an exception: "Too many different cell formats".
So is there something wrong with the way I'm using the open workbook method or is there another way to read a specific column from an excel file which contains several sheets and I only need to read data from a single specific sheet?
It might not be obvious to you but calling
new Excel.Application()
opens an instance of Excel (you may check it on the Task Manager). So if your version of Excel is not compatible with the file you are opening you get this error.
Since you are trying to open the Excel document with your Excel application, you DO care of what versions they are.
I think OpenXML SDK could help you with it.

C# - COMexception unhandled - Cannot access read-only document?

I'm new to C# and trying to accomplish some simple excel manipulation through the interop library.
I'd like to delete a text value which always appears as a separate line below the actual data table (with a blank row between the table and the text). It's a 'rows selected.' count which prints out in an automated report. It always appears in the first column.
The error "COMexception unhandled - Cannot access read-only document - test1.xlsx" appears after the row is deleted and I try to Close() the workbook.
I cannot seem to release the workbook, I've tried a lot of random garbage collection methods found on other forums, but has anyone seen this before?
Appreciate the help!
EDIT - So the file isn't read-only until I start running the program. Once I run the program it becomes locked and read-only. It isn't closing/releasing the workbook...
EDIT - I ended up adding a Workbook.Save() function and using the Application object's Quit() function. This question was also very informative (similar question).
public void ExcelManip(string thisFileName)
{
Workbook workBook = _excelApp.Workbooks.Open(thisFileName,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
DeleteRowCount(ref workBook);
workBook.Save();
workBook.Close(false, Type.Missing, Type.Missing);
_excelApp.Application.Quit();
_excelApp.Quit();
//workBook.Close(true, thisFileName);//ERROR;Cannot access read-only document
//Marshal.ReleaseComObject(workBook);
//GC.Collect();
//GC.WaitForPendingFinalizers();
}
I ended up adding a Workbook.Save() function and using the Application object's Quit() function. This question was also very informative (similar question).

Word Interop app to write text to the end of an open document

I'm trying to write a C# application that will find a document open in MS Word and write some text to the end of the document using word interop. Is this possible?
I know it's possible to kind of solve this problem using Process and Sendkeys built into the .NET Framework, but I'd like to solve the problem using Word Interop so I can add more functionality down the road (also sendkeys would really only solve the problem in certain special cases.)
Thanks!
Update:
I got the following partial solution working:
Application wordApp = new Application();
wordApp.Visible = true;
wordApp.Documents.Add();
Range rng = wordApp.ActiveDocument.Range(0, 0);
rng.Text = "New Text";
But I'd like to use an already open instance of word instead of creating a new one. Thanks!
Update 2:
I'm close! The following code works with UAC turned off
Application wordApp = (Word.Application)Marshal.GetActiveObject("Word.Application");
Range rng = wordApp.ActiveDocument.Range(0, 0);
rng.Text = "New Text";
But I'm not sure how to get it working with UAC enabled. UAC isn't causing any errors or exceptions, it just doesn't write the text to the open document.
Thanks for everyones help so far, the end is now in sight :)!
Update 3:
Just tried it again with UAC turned on and it worked... strange. Still if you know of any good resources about interop and UAC in general, please post a link here!
Have you looked at using Marshal.GetActiveObject("Word.Application") to get the running application, rather than creating a new one?
Definitely Evan. The Microsoft Office Interop Assemblies let you do just about anything from C#! SendKeys() is an issue.
http://msdn.microsoft.com/en-us/library/15s06t57(v=vs.80).aspx
I guess I should elaborate about SendKeys(): it doesn't even work reliably anymore as it was a primary hacker tool. The MS Office Interop Assemblies allow you to do an enormous array of things with each of the Office components. I have used them extensively with MS Excel, and some with Word, and you can do just about anything a user can do programatically.
You can try below .Here i am giving example with image insert.
WordC.Application wordApp = new WordC.Application();
// create Word document object
WordC.Document aDoc = null;
object readOnly = false;
object isVisible = false;
wordApp.Visible = false;
// wordApp.DisplayAlerts = false;
//docFileName is the filename with complete path. ex c://test.doc
aDoc = wordApp.Documents.Open(docFileName, Type.Missing, ref readOnly, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, ref isVisible, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
aDoc.Activate();
aDoc.InlineShapes.AddPicture(imgFilename, Type.Missing, Type.Missing, Type.Missing);
aDoc.Save();
aDoc.Close(Type.Missing, Type.Missing, Type.Missing);
wordApp.Quit(Type.Missing, Type.Missing, Type.Missing);

C#: How can I open and close an Excel workbook?

Does anyone know how to simply open and close an Excel workbook?
I don't need to read any data from the file, I just need to open and close it. (*)
I'm guessing that I'll need to reference the Microsoft.Office.Interop.Excel assembly.
*Reason: I've already configured pivot table information with a 3rd party library (Aspose). Now I need to read the generated pivot table.
Unfortunately, the Aspose library can't generate the pivot table at runtime. It needs someone to open the file with Excel so that Excel can generate the pivot table values.
after referencing Microsoft.Office.Interop.Excel also Make sure to clean up in the finally.
using Excel = Microsoft.Office.Interop.Excel;
Excel.ApplicationClass _Excel;
Excel.Workbook WB;
Excel.Worksheet WS;
try
{
_Excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
WB = _Excel.Workbooks.Open("FILENAME",
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
//do something
}
catch (Exception ex)
{
WB.Close(false, Type.Missing, Type.Missing);
throw;
}
finally
{
GC.Collect();
GC.WaitForPendingFinalizers();
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(WB);
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(_Excel);
}
A quick Google gives me this on code project:
http://www.codeproject.com/KB/office/csharp_excel.aspx
Consider using System.Diagnostics.Process to start, monitor, and close Excel. This site gives a good intro: http://www.thescarms.com/dotnet/Process.aspx, including running with an invisible window and sending input to it.

Categories

Resources