c# excel AddCanvas problem - c#

hi can you help me to find the problem at this line of my code where i try to add canvas to excel sheet from c#
Line 1 Excel.Worksheet ws =
(Excel.Worksheet) Globals.ThisAddIn.GetActiveWorksheet();
Line 2 ws.Shapes.AddCanvas(100,100,100,100);
at line 2 it gives me exception... am i doing smth wrong? thanks a lot!

See this - http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.shapes.addcanvas.aspx
It says "This method supports the .NET Framework infrastructure and is not intended to be used directly from your code"

Related

Change Excel Range Background Color

i try to Change with C# the Background Color of a specific Range in an Excel Worksheet.
My Code:
this.worksheetResult.Select();
this.worksheetResult.Cells[1,1].Value = "Export1";
this.worksheetResult.Range["A1:G1"].Merge();
var Section1 = this.worksheetResult.Range["A1:G1"];
Section1.Interior.Color = Colors.LightBlue;
Eyerything I tried wont work. either a HResult error shows up, or this Error Message:"Value does not Fall within the expected range."
Could need some quick help.
Which library are you using to accomplish this? And have you tried to debug to get the exact line where the error occur?
I solved already the Problem. I used the System.Drawing Library but in
Section1.Interior.Color = Colors.LightBlue;
"Colors" there was used another Library that shouldn´t. So i just addes the correct Library before Colors.
using System.Drawing;
.
.
.
Section1.Interior.Color = ColorTranslator.ToOle(Color.LightBlue);

Range Selection and / or Using InputBox in Excel's VSTO AddIn on C# Leading To Error and Build Failed

It's quite strange to state hereby, but remains to be a fact that, in one of my VSTO projects while the following piece of simple code works well enough, in another project, while it's almost exactly the same, i.e. assigned to a Ribbon's Button, the latter project only reflects the message in Visual Studio's Footer as Build Failed, as per the image following the same i.e. without even an error being highlighted?
private void button1_Click(object sender, RibbonControlEventArgs e)
{
Microsoft.Office.Interop.Excel.Range RG = Globals.ThisAddIn.Application.InputBox("Select your desired cells:", Type: 8);
MessageBox.Show(RG.Address);
Microsoft.Office.Interop.Excel.Range RG2 = Globals.ThisAddIn.Application.Selection;
MessageBox.Show(RG2.Address);
}
What could be the reason???
The only difference between the said two projects is that in the latter project I am trying to use the WPF Forms in the same as per a video on YouTube, i.e.: Office Addin with WPF Windows Controls for Word Excel Access Forms
Upon Googling led me to find out the solution of using the following line for matter of Selection but have been unable to find a similar solution for Application.InputBox:
Microsoft.Office.Interop.Excel.Range RG2 = Globals.ThisAddIn.Application.ActiveWindow.RangeSelection
Even placing a similar question on Microsoft's Answers Community led to no reply till now, :,-(
Any advice / help would be appreciated.
Thanks in advance.
It's quite strange, but true enough. Till now the Visual Studio was not reflecting any error but today when I tried again to implement WPF in a VSTO it finally reflected that there was a Casting Error???
The said error also appeared for only few seconds and then again disappeared with the same old footer reflecting build failed.
When I googled it no doubt turn out to be so and the very first site clarifying the said matter was quite fruitful enough.
Thus, changing my code to the following did turn out to be resulting to a successful build:
private void button1_Click(object sender, RibbonControlEventArgs e)
{
Microsoft.Office.Interop.Excel.Range RG = (Microsoft.Office.Interop.Excel.Range)Globals.ThisAddIn.Application.InputBox("Select your desired cells:", Type: 8);
MessageBox.Show(RG.Address);
Microsoft.Office.Interop.Excel.Range RG2 = (Microsoft.Office.Interop.Excel.Range)Globals.ThisAddIn.Application.Selection;
MessageBox.Show(RG2.Address);
}
Alhamdulillah!

Get excel filename and path error - C#

I am getting a very stupid error. I am new to VSTO and I need to get the location of the Excel file in some variable in my Addin.
string name = ActiveWorkbook.FullName;
I am getting a red line below ActiveWorkbook with error:
The name ActiveWorkBook does not exist in the current context.
I have added reference of Microsoft.Office.Interop.Excel in the code but its showing this error. I am new to this.. am I missing something?
In Excel VSTO, you need to use Globals.ThisAddIn.Application to get access to the Excel Application Model, see below :
var wb = Globals.ThisAddIn.Application.ActiveWorkbook;
string name = wb.FullName;
see also Programming VSTO Add-ins
If your code is inside the ThisAddIn class you can directly call: this.Application.ActiveWorkbook
ActiveWorkbook is not a class. It's the property of the Application interface. You can not call it in the class-name manner.
Then, you need to change your code to this.Application.ActiveWorkbook.FullName;

Excel AddIn : Hide a datafield in a pivottable

I'm trying to control an Excel (2010) PivotTable from a VSTO (excel AddIn) in C# (4.0). I've got no problem adding PivotFields (Dimensions) and DataFields (Measures) to the PivotTable.
The problem is I can't remove a DataField.
My DataField is a PivotField object.
I've tried :
myDataField.Hidden = true;
myDataField.DisplayInReport = false;
myDataField.Orientation = XlPivotFieldOrientation.xlHidden;
// This last one is what I use to remove a (Dimension) PivotField
Every one of these lines throws a COM Exception with absolutely no information in it. The only thing I have is the message : "Exception de HRESULT : 0x800A03EC", which seems to be common to every VSTO exception.
If anyone has a solution, that would help me a lot.

How to add a Bookmark control to a Word document at runtime

I want to add in a C# application a bookmark to a particular range to my Word document at runtime . I have found one solution
Microsoft.Office.Tools.Word.Bookmark bookmark1;
bookmark1 = this.Controls.AddBookmark(this.Paragraphs[1].Range, "bookmark1");
But an error shows that the Windows form has no definition for AddBookmark. Please help.
You are using a WinForms application, therefore by using the keyword this it will refer to the form class you are using which does not have a definition for Controls.AddBookmark.
I suggest you take a look here: http://msdn.microsoft.com/en-us/library/cc442946.aspx
This will show you how to create a word addin from which you can then use this code to add a bookmark.
Microsoft.Office.Tools.Word.Bookmark bookmark1; bookmark1 = this.Controls.AddBookmark(this.Paragraphs[1].Range, "bookmark1");

Categories

Resources