i cant get selection range from excel. i am using below code block and i can get active sheet like this. but i need only selection range. how can i do this?
Microsoft.Office.Interop.Excel.Application ExApp = Globals.ThisAddIn.Application as Microsoft.Office.Interop.Excel.Application;
Microsoft.Office.Interop.Excel.Worksheet ExWorksheet = ExApp.ActiveSheet as Microsoft.Office.Interop.Excel.Worksheet;
Microsoft.Office.Interop.Excel.Range activeSheet = ExWorksheet.UsedRange as Microsoft.Office.Interop.Excel.Range;
thanks for advice.
The selection is a property of the Application, so you should use something like:
Microsoft.Office.Interop.Excel.Application ExApp = Globals.ThisAddIn.Application as Microsoft.Office.Interop.Excel.Application;
Microsoft.Office.Interop.Excel.Range SelectedRange = ExApp.Selection as Microsoft.Office.Interop.Excel.Range;
Just be careful that the object returned by Selection could be something different fromn a Range (e.g. it could be a Chart), so you should check for null values of SelectedRange.
Related
I am creating charts in PowerPoint. The below code opens two excel applications. One opens in the background that is invisible. The second one opens after the method ends. I need to make sure second excel either never open ideally or I can close it after it opens.
I have tried the below things but none worked.
I have tried forcing GC, Manual ReleaseComObject, Killing Excel process
I have tried separating excel COM objects and forcing GC
private void BtnInsert_Click(object sender, EventArgs e)
{
var Addin = Globals.ThisAddIn;
Microsoft.Office.Interop.PowerPoint.Application activeApplication = Addin.Application;
DocumentWindow activeWindows = activeApplication.ActiveWindow;
Microsoft.Office.Interop.PowerPoint.View activeView = activeWindows.View;
Slide activeSlide = activeView.Slide;
Microsoft.Office.Interop.PowerPoint.Shapes slideShape = activeSlide.Shapes;
Microsoft.Office.Interop.PowerPoint.Shape shape = slideShape.AddChart2(-1, XlChartType.xl3DBarClustered, -1, -1, -1, -1, true);
Microsoft.Office.Interop.PowerPoint.Chart chart = shape.Chart;
//Access the chart data
Microsoft.Office.Interop.PowerPoint.ChartData chartData = chart.ChartData;
chartData.Activate();
//Create instance to Excel workbook to work with chart data
Workbook workbook = chartData.Workbook;
Microsoft.Office.Interop.Excel.Application workbookApplication = workbook.Application;
workbookApplication.Visible = false;
workbookApplication.WindowState = XlWindowState.xlMinimized;
//Accessing the data worksheet for chart
Worksheet worksheet = workbook.Worksheets[1];
// I am adding data here
// This is not required to reproduce this
chartData.BreakLink();
workbook.Close(true);
}
Also, note that this issue does not occur while updating data.
Remove chartData.Activate() and chartData.BreakLink() solves this.
Although online documentation says that chartdata.activate is required before accessing the workbook.
Otherwise, we will get a null reference.
I think the documentation is incorrect or it does not apply to vsto.
This image show what i basically want to do
I have plenty excel files that i need to prepare before inserting the data into a SQL database, one of the steps is unmerge excel cells and duplicate the data, i'm doing this doc parse with c#
I found a solution with VBA Macro Excel here
Sub UnMergeFill()
Dim cell As Range, joinedCells As Range
For Each cell In ThisWorkbook.ActiveSheet.UsedRange
If cell.MergeCells Then
Set joinedCells = cell.MergeArea
cell.MergeCells = False
joinedCells.Value = cell.Value
End If
Next
End Sub
But i need to do it on c# with microsoft.office.interop.excel
Does anyone know if there's a way to do this?
C# code is very similar:
private void UnMergeFill(Workbook wb)
{
foreach (Range cell in ((_Worksheet)wb.ActiveSheet).UsedRange)
{
if (cell.MergeCells)
{
var joinedCells = cell.MergeArea;
cell.MergeCells = false;
joinedCells.Value = cell.Value;
}
}
}
This is what I have tried to do to have my chart show the data in the columns I have hidden:
Excel.Application oExcelApp = new Excel.Application;
Excel._Workbook oWB = oExcelApp.Workbooks.Add();
Excel._Worksheet oWS = oWB.ActiveSheet;
Excel.ChartObjects oCharts = (Excel.ChartObjects)oWS.ChartObjects();
Excel.ChartObject oChart = oCharts.Add(10, 80, 300, 250);
Excel.Chart chart = oChart.Chart;
// these three lines work
chart.HasTitle = true;
chart.ChartTitle.Text = "Chart Title";
chart.HasLegend = false;
// I get a compile error for this line
// because the HasHiddenContent property is read-only
chart.HasHiddenContent = true;
The HasHiddenContent property is the only one I can find that looks like it might be what I'm looking for, but it is read-only. Which property can I use to tell the chart to show data in hidden columns (and rows)?
After doing the "record the macro for what you want to do and then convert VBA to c#," I found the answer to my question.
The correct property to toggle showing data in hidden columns and rows is:
chart.PlotVisibleOnly = false;
I am looking for a way to move my selected cell from the top left to the bottom right. I was trying to use xlDirection but that highlights everything and doesnt allow me to do a combination of movements.
A B C
1 4 7
2 5 8
3 6 9
I start at A and now only want to focus on 9. The size of the excels change so i cant specify the actual cell to look for each time.
I was hoping there are similar commands as Ctrl+Down or Ctrl+Right that would put me on the cell.
can't you send keystrokes to the object?
A reference for all key strokes can be found here:
https://learn.microsoft.com/en-us/dotnet/api/microsoft.office.interop.excel._application.sendkeys?view=excel-pia
public class Navigator
{
private Excel.Application excel;
private Excel.Workbook workbook;
public void NavigateToBottomRight(string filePath, string worksheetName)
{
excel = new Excel.Application();
excel.Visible = true;
workbook = excel.Workbooks.Open(filePath);
var worksheet = workbook.Worksheets.Cast<Excel.Worksheet>().FirstOrDefault(x => x.Name == worksheetName);
Excel.Range cell = worksheet.Cells[1, 1];
cell.Activate();
string controlRight = "^{Right}";
string controlDown = "^{Down}";
excel.SendKeys(controlRight, true);
excel.SendKeys(controlDown, true);
//Do other work here
workbook.Save();
excel.Quit();
}
}
Hope this helps!
I need to add a scrollbar to worksheet and link cell to it using Microsoft.Office.Interop.Excel library and C#.NET.
The correspoding VBA code is like this:
ActiveSheet.ScrollBars.Add(545.25, 172.5, 398.25, 24.75).Select
With Selection
.Value = 0
.Min = 0
.Max = 100
.SmallChange = 1
.LargeChange = 10
.LinkedCell = "$A$1"
.Display3DShading = True
End With
I tried the below code, it added Scrollbar but not working as expected and also unable to link cell to it.
oSheet.Shapes.AddOLEObject("Forms.ScrollBar.1", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, 545.25, 172.5, 398.25, 24.75);
My C#.NET Code adds scrollbar as an object to Excel, where as i need it as a control. It seems there is a difference in it.
Actual Result using my C#.NET code is attached as image below
But the Expected result is attached as image below:
I found the solution Here.
Yes earlier I was adding ActiveX Object in place of Control. The below code works fine.
using Excel = Microsoft.Office.Interop.Excel;
:
:
Excel.ControlFormat Scrollbar = oSheet.Shapes.AddFormControl(Excel.XlFormControl.xlScrollBar, 545, 172, 398, 24).ControlFormat;
Scrollbar.Value = 0;
Scrollbar.Min = 0;
Scrollbar.Max=100;
Scrollbar.SmallChange = 1;
Scrollbar.LargeChange = 10;
Scrollbar.LinkedCell = "$A$1";