Excel AddIn : Hide a datafield in a pivottable - c#

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.

Related

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;

Hide a column in MS-Project with C# Interop

I'm doing a WinForm program on Visual Studio who automates the creation and automation of a MS-Project file.
I use those references :
Microsoft Office 16.0 Object Library
Microsoft Office Project 16.0 Object Library
Microsoft Project Task Launch Control
In some point in my MS-Project file, I want to customize displaying of the columns in the table.
For this purpose I use TableEditEx function. I've already made a new customized column like this :
Microsoft.Office.Interop.MSProject.Application projApp = new Microsoft.Office.Interop.MSProject.Application();
projApp.Application.SelectTaskColumn(Column: "Add New Column");
projApp.Application.TableEditEx(Name: "&Entry", TaskTable: true, NewName: "Progression", NewFieldName: "Text1", Title: "Completion", ShowInMenu: true, Width: 12);
projApp.Application.TableApply(Name: "&Entry");
And now I want to hide column "Resource Names" for example. To do so I tried the following code :
projApp.Application.SelectTaskColumn(Column: "Resource Names");
projApp.Application.TableEditEx(Name: "&Entry", TaskTable: true, Create: false, ShowInMenu: false);
projApp.Application.TableApply(Name: "&Entry");
But it doesn't appear to do anything more than selecting the column.
I've researched here :
Documentation : TableEditEx
To hide the column (not delete it), set the column width = 0:
projApp.Application.TableEditEx(Name: "&Entry", TaskTable: true, FieldName: "Resource Names", Width: 0);
projApp.Application.TableApply(Name: "&Entry");
That way, the user can unhide the column later without having to edit the table to insert it.
Okay,
I found a solution on my own. I've tried doing a macro to get the VBA code to hide a column and I got this :
SelectTaskColumn Column:="Resource Names"
ColumnDelete
And I converted it in C# code :
projApp.Application.SelectTaskColumn(Column: "Resource Names");
projApp.Application.ColumnDelete();
And it worked, thanks myself !

Excel C# interop worksheet delete

I have an app using Excel COM interop. It copies a template XLS to an existing doc, replacing same-named tabs by Delete old then Copy new from template.
I am getting 800A03ec exception when my app tries to delete a worksheet.
This problem is in code that has been working for years but now fails after an upgrade to Office 2016.
I find that if I set app.Visible = true, the operation completes properly! But I do not want Excel visible.
If app.Visible = false, I do not get an error from the first worksheet Delete(), but exception occurs on the second.
The first delete of the last tab seems to go OK, but 'Sheets' array of the worksheets object doesn't decrease as I would expect. However the corresponding Sheet item in the array becomes a "null" worksheet.
The second delete, of the tab before it, throws an exception on delete.
I have thoroughly ensured:
No COM reference leaks
All COM references are discarded before each delete, except for the worksheet to be deleted and its parent app, etc. objects
DisplayAlerts is false
Worksheets are simple and ordinary, nothing hidden etc.
Why would it work when app is visible, but not work when app is hidden??
UPDATE: code fragment
int _DeleteTargetTab(string tabName)
{
List<object> comRefs = new List<object>();
int prevTabIndex;
int tabToDelete = _FindTargetTemplateIndex(tabName, out prevTabIndex);
if (tabToDelete > 0)
{
try
{
Excel.Sheets targetSheets = _workbook.Sheets;
comRefs.Add(targetSheets);
Excel.Worksheet targetWorksheet = targetSheets[tabToDelete];
comRefs.Add(targetWorksheet);
targetWorksheet.Delete();
}
finally
{
ExcelUtility.ReleaseAll(comRefs);
}
}
return prevTabIndex;
}
and ExcelUtility.ReleaseAll() calls Marshal.ReleaseComObject(), then GC.Collect() and GC.WaitForPendingFinalizers().
Target worksheet has 8 tabs. Last tab is deleted, then copied, without exception. Then on tab 7 delete throws an exception, only when app is hidden. Works fine on older Office.
Copy code is
void _CopyTemplateTabToTarget(Excel.Worksheet templateWorksheet, int prevTargetTabIndex)
{
List<object> comRefs = new List<object>();
try
{
Excel.Sheets targetSheets = _workbook.Sheets;
comRefs.Add(targetSheets);
Excel.Worksheet prevSheet = targetSheets[prevTargetTabIndex];
comRefs.Add(prevSheet);
templateWorksheet.Copy(Type.Missing, prevSheet);
}
finally
{
ExcelUtility.ReleaseAll(comRefs);
}
}
Init code is
_app = new Excel.Application();
_app.DisplayAlerts = false;
_app.Visible = false;
I got further by adding this:
//
// Super important to activate a tab other than what needs to be deleted.
// Cast is required because an event and method have the same name "Activate".
//
((Excel._Worksheet)_weeklyDataSheet).Activate();
Then that got me to some code around my apps' Save function that used to work but was throwing exception:
_weeklyDataSheet.Select(Type.Missing);
I changed that to Activate() as well and made more progress. But yet Excel still threw exception on Delete() this time on the third workbook.
I was forced to run app Visible for the week's report. But even that ran through about 80 workbooks and then Excel locked up, mouse cursor rapidly flashing between arrow and wait timer animation, and my app got RPC error eventually.
Conclusion: ABANDON COM APIS for Office 2016. Microsoft seems not to support them properly anymore.

How do i load an automation addin (dll) programmatically from within a vsto addin

VSTO
VS2008 SP1
.NET 3.5
Excel 2007
I am a .net noob. I am trying to load an automation addin that is an excel application/automation addin (it is a dll not xla or xll) from within a vsto addin in the ThisAddIn_Startup() method of the vsto addin. From google I got the below solution which is not working.
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
Application excel = Globals.ThisAddIn.Application;
//Also tried without display alerts being set to false
excel.DisplayAlerts = false;
foreach (AddIn addin in excel.AddIns)
{
if (addin.progID.Equals("MY_ADDIN_PROG_ID"))
{
Debug.WriteLine("Addin installed is " + addin.Installed);
addin.Installed = false;
Debug.WriteLine("Addin is: " + addin.FullName + ", " + addin.progID);
Debug.WriteLine("Addin installed is " + addin.Installed);
}
}
AddIn addIn = excel.AddIns.Add("MY_ADDIN_PROG_ID", false);
addIn.Installed = true;
excel.DisplayAlerts = true;
Debug.WriteLine("Addin is: " + addIn.FullName + ", " + addIn.progID);
Debug.WriteLine("Addin installed is " + addIn.Installed);
excel.DisplayAlerts = false;
//OTHER STARTUP CODE
Debug.WriteLine("Starting up addin!");
}
Note, I can see the addin.installed is being set to false and back to true on startup but when I try to populate worksheet with udfs from the addin I tried to load in a later button_click method, I get #NAME? error. I am at my wits end. Any help will be greatly appreciated.
If I first try to call the udf in excel by typing it in a cell by hand before I call my button click method, the worksheet population works and the udfs get evaluted as expected but this is not ideal.
Also setting installed property to true does not seem to be doing anything as i can still see the udf addin as inactive in excel, it is only if I type it into a cell that it gets activated. Is there anything else I need to do to activate the automation addin in my vsto startup?
Thanks!
I'm not sure you want to do this in the startup event. I have done something similar but not quite the same before which may be applicable. I exposed some COM visible functions to VBA in a different event handler:
protected override object RequestComAddInAutomationService()
{
// return something com-visible
}
So maybe you can try to load your automation dll this way? This happens before the startup event fires... Excel might be doing something like locking its list of addins while a startup event is being handled - who knows? If it were possible to know Excel programming would be less tedious.
It is harder than it seems to combine VSTO and Automation in Excel. You may find my blog post helpful:
Communicating Between VSTO and UDF's in Excel
Just need to add String Value to the following registry key and you are good.
For Office 2007
Find regkey, HKEY_CURRENT_USER\SOftware\Microsoft\Office\12.0\Excel\Options, then create string value, where name = OPEN, value = /A "YOUR ADDIN NAME HERE" (quotes need to be included as well.)
Note that for the first addin, value name should be called OPEN, for the second one and onwards, use OPEN1, OPEN2, ... etc.
For Office 2010
Just replace 12.0 with 14.0 in the above regkey path, the rest are all the same.
Check out below article on MSDN, which will also help you a lot.
http://support.microsoft.com/kb/291392
Looks like this is a bug specific to VSTO. I converted my addin to a COM addin and was able to use the automation addin from code after that. My team has sent the issue to microsoft so we'll see what they say.

c# excel AddCanvas problem

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"

Categories

Resources