EDN Admin
Well-known member
Hello,
I am trying to run the code to import data from C# to Excel found on MSD within a C# SolidWorks Macro, I do not believe it is created with any SolidWorks issue.
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small
using
<span style="font-size:small <br/>
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small
using
<span style="font-size:small SolidWorks.Interop.swconst;
<span style="font-size:small <br/>
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small
using
<span style="font-size:small System.Runtime.InteropServices;
<span style="font-size:small <br/>
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small
using
<span style="font-size:small System;
<span style="font-size:small <br/>
<br/>
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small
using
<span style="font-size:small Excel = Microsoft.Office.Interop.Excel;
<span style="font-size:small <br/>
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small
using
<span style="font-size:small System.Reflection;
<span style="font-size:small <br/>
<br/>
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small
namespace
<span style="font-size:small Test.csproj
<span style="font-size:small
{
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small public<span style="font-size:small
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small partial<span style="font-size:small
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small class<span style="font-size:small
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small SolidWorksMacro
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small <br/>
<span style="font-size:small
{
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small public<span style="font-size:small
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small void<span style="font-size:small Main()
<span style="font-size:small
{
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small //Form1 form1Excel = new Form1();
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small <br/>
<span style="font-size:small
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small //form1Excel.Show();
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small <br/>
<span style="font-size:small
Excel.Application oXL;
Excel._Workbook oWB;
Excel._Worksheet oSheet;
Excel.Range oRng;
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small try
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small <br/>
<span style="font-size:small
{
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small //Start Excel and get Application object.
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small <br/>
<span style="font-size:small
oXL =
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small new<span style="font-size:small Excel.Application();
<span style="font-size:small
oXL.Visible =
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small true<span style="font-size:small ;
<span style="font-size:small
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small //Get a new workbook.
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small <br/>
<span style="font-size:small
oWB = (Excel._Workbook)(oXL.Workbooks.Add(
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small Missing<span style="font-size:small .Value));
<span style="font-size:small
oSheet = (Excel._Worksheet)oWB.ActiveSheet;
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small //Add table headers going cell by cell.
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small <br/>
<span style="font-size:small
oSheet.Cells[1, 1] =
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "First Name"<span style="font-size:small ;
<span style="font-size:small
oSheet.Cells[1, 2] =
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "Last Name"<span style="font-size:small ;
<span style="font-size:small
oSheet.Cells[1, 3] =
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "Full Name"<span style="font-size:small ;
<span style="font-size:small
oSheet.Cells[1, 4] =
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "Salary"<span style="font-size:small ;
<span style="font-size:small
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small //Format A11 as bold, vertical alignment = center.
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small <br/>
<span style="font-size:small
oSheet.get_Range(
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "A1"<span style="font-size:small ,
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "D1"<span style="font-size:small ).Font.Bold =
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small true<span style="font-size:small ;
<span style="font-size:small
oSheet.get_Range(
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "A1"<span style="font-size:small ,
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "D1"<span style="font-size:small ).VerticalAlignment =
<span style="font-size:small
Excel.XlVAlign.xlVAlignCenter;
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small // Create an array to multiple values at once.
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small <br/>
<span style="font-size:small
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small string<span style="font-size:small [,] saNames =
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small new<span style="font-size:small
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small string<span style="font-size:small [5, 2];
<span style="font-size:small
saNames[0, 0] =
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "John"<span style="font-size:small ;
<span style="font-size:small
saNames[0, 1] =
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "Smith"<span style="font-size:small ;
<span style="font-size:small
saNames[1, 0] =
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "Tom"<span style="font-size:small ;
<span style="font-size:small
saNames[1, 1] =
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "Brown"<span style="font-size:small ;
<span style="font-size:small
saNames[2, 0] =
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "Sue"<span style="font-size:small ;
<span style="font-size:small
saNames[2, 1] =
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "Thomas"<span style="font-size:small ;
<span style="font-size:small
saNames[3, 0] =
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "Jane"<span style="font-size:small ;
<span style="font-size:small
saNames[3, 1] =
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "Jones"<span style="font-size:small ;
<span style="font-size:small
saNames[4, 0] =
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "Adam"<span style="font-size:small ;
<span style="font-size:small
saNames[4, 1] =
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "Johnson"<span style="font-size:small ;
<span style="font-size:small
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small //Fill A2:B6 with an array of values (First and Last Names).
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small <br/>
<span style="font-size:small
oSheet.get_Range(
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "A2"<span style="font-size:small ,
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "B6"<span style="font-size:small ).Value2 = saNames;
<span style="font-size:small
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small //Fill C2:C6 with a relative formula (=A2 & " " & B2).
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small <br/>
<span style="font-size:small
oRng = oSheet.get_Range(
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "C2"<span style="font-size:small ,
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "C6"<span style="font-size:small );
<span style="font-size:small
oRng.Formula =
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "=A2 & " " & B2"<span style="font-size:small ;
<span style="font-size:small
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small //Fill D26 with a formula(=RAND()*100000) and apply format.
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small <br/>
<span style="font-size:small
oRng = oSheet.get_Range(
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "D2"<span style="font-size:small ,
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "D6"<span style="font-size:small );
<span style="font-size:small
oRng.Formula =
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "=RAND()*100000"<span style="font-size:small ;
<span style="font-size:small
oRng.NumberFormat =
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "$0.00"<span style="font-size:small ;
<span style="font-size:small
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small //AutoFit columns A.
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small <br/>
<span style="font-size:small
oRng = oSheet.get_Range(
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "A1"<span style="font-size:small ,
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "D1"<span style="font-size:small );
<span style="font-size:small
oRng.EntireColumn.AutoFit();
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small //Manipulate a variable number of columns for Quarterly Sales Data.
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small <br/>
<span style="font-size:small
DisplayQuarterlySales(oSheet);
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small //Make sure Excel is visible and give the user control
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small <br/>
<span style="font-size:small
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small //of Microsoft Excels lifetime.
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small <br/>
<span style="font-size:small
oXL.Visible =
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small true<span style="font-size:small ;
<span style="font-size:small
oXL.UserControl =
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small true<span style="font-size:small ;
<span style="font-size:small
}
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small catch<span style="font-size:small (<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small Exception<span style="font-size:small
theException)
<span style="font-size:small
{
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small String<span style="font-size:small errorMessage;
<span style="font-size:small
errorMessage =
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "Error: "<span style="font-size:small ;
<span style="font-size:small
errorMessage =
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small String<span style="font-size:small .Concat(errorMessage, theException.Message);
<span style="font-size:small
errorMessage =
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small String<span style="font-size:small .Concat(errorMessage,
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small " Line: "<span style="font-size:small );
<span style="font-size:small
errorMessage =
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small String<span style="font-size:small .Concat(errorMessage, theException.Source);
<span style="font-size:small
MessageBox.Show(errorMessage,
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "Error"<span style="font-size:small );
<span style="font-size:small
}
}
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small private<span style="font-size:small
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small void<span style="font-size:small DisplayQuarterlySales(Excel._Worksheet oWS)
<span style="font-size:small
{
Excel._Workbook oWB;
Excel.Series oSeries;
Excel.Range oResizeRange;
Excel._Chart oChart;
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small String<span style="font-size:small sMsg;
<span style="font-size:small
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small int<span style="font-size:small iNumQtrs;
<span style="font-size:small
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small //Determine how many quarters to display data for.
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small <br/>
<span style="font-size:small
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small for<span style="font-size:small (iNumQtrs = 4; iNumQtrs >= 2; iNumQtrs--)
<span style="font-size:small
{
sMsg =
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "Enter sales data for "<span style="font-size:small ;
<span style="font-size:small
sMsg =
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small String<span style="font-size:small .Concat(sMsg, iNumQtrs);
<span style="font-size:small
sMsg =
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small String<span style="font-size:small .Concat(sMsg,
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small " quarter(s)?"<span style="font-size:small );
<span style="font-size:small
DialogResult iRet = MessageBox.Show(sMsg,
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "Quarterly Sales?"<span style="font-size:small ,
<span style="font-size:small
MessageBoxButtons.YesNo);
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small if<span style="font-size:small (iRet == DialogResult.Yes)
<span style="font-size:small
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small break<span style="font-size:small ;
<span style="font-size:small
}
sMsg =
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "Displaying data for "<span style="font-size:small ;
<span style="font-size:small
sMsg =
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small String<span style="font-size:small .Concat(sMsg, iNumQtrs);
<span style="font-size:small
sMsg =
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small String<span style="font-size:small .Concat(sMsg,
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small " quarter(s)."<span style="font-size:small );
<span style="font-size:small
MessageBox.Show(sMsg,
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "Quarterly Sales"<span style="font-size:small );
<span style="font-size:small
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small //Starting at E1, fill headers for the number of columns selected.
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small <br/>
<span style="font-size:small
oResizeRange = oWS.get_Range(
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "E1"<span style="font-size:small ,
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "E1"<span style="font-size:small ).get_Resize(<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small Missing<span style="font-size:small .Value,
iNumQtrs);
<span style="font-size:small
oResizeRange.Formula =
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "="Q" & COLUMN()-4 & CHAR(10) & "Sales""<span style="font-size:small ;
<span style="font-size:small
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small //Change the Orientation and WrapText properties for the headers.
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small <br/>
<span style="font-size:small
oResizeRange.Orientation = 38;
oResizeRange.WrapText =
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small true<span style="font-size:small ;
<span style="font-size:small
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small //Fill the interior color of the headers.
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small <br/>
<span style="font-size:small
oResizeRange.Interior.ColorIndex = 36;
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small //Fill the columns with a formula and apply a number format.
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small <br/>
<span style="font-size:small
oResizeRange = oWS.get_Range(
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "E2"<span style="font-size:small ,
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "E6"<span style="font-size:small ).get_Resize(<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small Missing<span style="font-size:small .Value,
iNumQtrs);
<span style="font-size:small
oResizeRange.Formula =
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "=RAND()*100"<span style="font-size:small ;
<span style="font-size:small
oResizeRange.NumberFormat =
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "$0.00"<span style="font-size:small ;
<span style="font-size:small
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small //Apply borders to the Sales data and headers.
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small <br/>
<span style="font-size:small
oResizeRange = oWS.get_Range(
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "E1"<span style="font-size:small ,
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "E6"<span style="font-size:small ).get_Resize(<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small Missing<span style="font-size:small .Value,
iNumQtrs);
<span style="font-size:small
oResizeRange.Borders.Weight = Excel.XlBorderWeight.xlThin;
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small //Add a Totals formula for the sales data and apply a border.
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small <br/>
<span style="font-size:small
oResizeRange = oWS.get_Range(
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "E8"<span style="font-size:small ,
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "E8"<span style="font-size:small ).get_Resize(<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small Missing<span style="font-size:small .Value,
iNumQtrs);
<span style="font-size:small
oResizeRange.Formula =
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "=SUM(E2:E6)"<span style="font-size:small ;
<span style="font-size:small
oResizeRange.Borders.get_Item(Excel.XlBordersIndex.xlEdgeBottom).LineStyle
= Excel.XlLineStyle.xlDouble;
oResizeRange.Borders.get_Item(Excel.XlBordersIndex.xlEdgeBottom).Weight
= Excel.XlBorderWeight.xlThick;
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small //Add a Chart for the selected data.
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small <br/>
<span style="font-size:small
oWB = (Excel._Workbook)oWS.Parent;
oChart = (Excel._Chart)oWB.Charts.Add(
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small Missing<span style="font-size:small .Value,
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small Missing<span style="font-size:small .Value,
<span style="font-size:small
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small Missing<span style="font-size:small .Value,
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small Missing<span style="font-size:small .Value);
<span style="font-size:small
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small //Use the ChartWizard to create a new chart from the selected data.
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small <br/>
<span style="font-size:small
oResizeRange = oWS.get_Range(
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "E2:E6"<span style="font-size:small ,
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small Missing<span style="font-size:small .Value).get_Resize(
<span style="font-size:small
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small Missing<span style="font-size:small .Value, iNumQtrs);
<span style="font-size:small
oChart.ChartWizard(oResizeRange, Excel.XlChartType.xl3DColumn,
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small Missing<span style="font-size:small .Value,
<span style="font-size:small
Excel.XlRowCol.xlColumns,
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small Missing<span style="font-size:small .Value,
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small Missing<span style="font-size:small .Value,
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small Missing<span style="font-size:small .Value,
<span style="font-size:small
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small Missing<span style="font-size:small .Value,
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small Missing<span style="font-size:small .Value,
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small Missing<span style="font-size:small .Value,
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small Missing<span style="font-size:small .Value);
<span style="font-size:small
oSeries = (Excel.Series)oChart.SeriesCollection(1);
oSeries.XValues = oWS.get_Range(
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "A2"<span style="font-size:small ,
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "A6"<span style="font-size:small );
<span style="font-size:small
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small for<span style="font-size:small (<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small int<span style="font-size:small
iRet = 1; iRet <= iNumQtrs; iRet++)
<span style="font-size:small
{
oSeries = (Excel.Series)oChart.SeriesCollection(iRet);
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small String<span style="font-size:small seriesName;
<span style="font-size:small
seriesName =
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "="Q"<span style="font-size:small ;
<span style="font-size:small
seriesName =
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small String<span style="font-size:small .Concat(seriesName, iRet);
<span style="font-size:small
seriesName =
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small String<span style="font-size:small .Concat(seriesName,
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small """<span style="font-size:small );
<span style="font-size:small
oSeries.Name = seriesName;
}
oChart.Location(Excel.XlChartLocation.xlLocationAsObject, oWS.Name);
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small //Move the chart so as not to cover your data.
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small <br/>
<span style="font-size:small
oResizeRange = (Excel.Range)oWS.Rows.get_Item(10,
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small Missing<span style="font-size:small .Value);
<span style="font-size:small
oWS.Shapes.Item(
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "Chart 1"<span style="font-size:small ).Top = (<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small float<span style="font-size:small )(<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small double<span style="font-size:small )oResizeRange.Top;
<span style="font-size:small
oResizeRange = (Excel.Range)oWS.Columns.get_Item(2,
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small Missing<span style="font-size:small .Value);
<span style="font-size:small
oWS.Shapes.Item(
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "Chart 1"<span style="font-size:small ).Left = (<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small float<span style="font-size:small )(<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small double<span style="font-size:small )oResizeRange.Left;
<span style="font-size:small
}
<span style="color:#808080; font-size:small <span style="color:#808080; font-size:small ///<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small <span style="color:#808080; font-size:small <span style="color:#808080; font-size:small <summary>
<span style="color:#808080; font-size:small <span style="color:#808080; font-size:small <br/>
<span style="font-size:small
<span style="color:#808080; font-size:small <span style="color:#808080; font-size:small ///<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small The SldWorks swApp variable is pre-assigned for you.
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small <br/>
<span style="font-size:small
<span style="color:#808080; font-size:small <span style="color:#808080; font-size:small ///<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small <span style="color:#808080; font-size:small <span style="color:#808080; font-size:small </summary>
<span style="color:#808080; font-size:small <span style="color:#808080; font-size:small <br/>
<span style="font-size:small
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small public<span style="font-size:small
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small SldWorks<span style="font-size:small swApp;
<span style="font-size:small
}
}
The error message is provided below.
Error<span> 1, Line 95 Namespace <global namespace> contains a definition conflicting with alias Excel
The using statement is per example as: <span style="color:blue; line-height:115%; font-family:Courier New; font-size:10pt
using<span style="line-height:115%; font-family:Courier New; font-size:10pt Excel = Microsoft.Office.Interop.Excel;<span>
The end goal is to be able to access data from SolidWorks through C# programming preform calculations and then provide results in Excel.<span>
Any further examples or assistance would be greatly appreciated. Your time and efforts regarding this matter are greatly appreciated.
Thanks!
<span style="font-size:small SolidWorks.Interop.sldworks;
View the full article
I am trying to run the code to import data from C# to Excel found on MSD within a C# SolidWorks Macro, I do not believe it is created with any SolidWorks issue.
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small
using
<span style="font-size:small <br/>
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small
using
<span style="font-size:small SolidWorks.Interop.swconst;
<span style="font-size:small <br/>
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small
using
<span style="font-size:small System.Runtime.InteropServices;
<span style="font-size:small <br/>
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small
using
<span style="font-size:small System;
<span style="font-size:small <br/>
<br/>
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small
using
<span style="font-size:small Excel = Microsoft.Office.Interop.Excel;
<span style="font-size:small <br/>
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small
using
<span style="font-size:small System.Reflection;
<span style="font-size:small <br/>
<br/>
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small
namespace
<span style="font-size:small Test.csproj
<span style="font-size:small
{
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small public<span style="font-size:small
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small partial<span style="font-size:small
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small class<span style="font-size:small
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small SolidWorksMacro
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small <br/>
<span style="font-size:small
{
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small public<span style="font-size:small
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small void<span style="font-size:small Main()
<span style="font-size:small
{
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small //Form1 form1Excel = new Form1();
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small <br/>
<span style="font-size:small
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small //form1Excel.Show();
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small <br/>
<span style="font-size:small
Excel.Application oXL;
Excel._Workbook oWB;
Excel._Worksheet oSheet;
Excel.Range oRng;
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small try
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small <br/>
<span style="font-size:small
{
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small //Start Excel and get Application object.
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small <br/>
<span style="font-size:small
oXL =
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small new<span style="font-size:small Excel.Application();
<span style="font-size:small
oXL.Visible =
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small true<span style="font-size:small ;
<span style="font-size:small
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small //Get a new workbook.
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small <br/>
<span style="font-size:small
oWB = (Excel._Workbook)(oXL.Workbooks.Add(
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small Missing<span style="font-size:small .Value));
<span style="font-size:small
oSheet = (Excel._Worksheet)oWB.ActiveSheet;
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small //Add table headers going cell by cell.
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small <br/>
<span style="font-size:small
oSheet.Cells[1, 1] =
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "First Name"<span style="font-size:small ;
<span style="font-size:small
oSheet.Cells[1, 2] =
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "Last Name"<span style="font-size:small ;
<span style="font-size:small
oSheet.Cells[1, 3] =
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "Full Name"<span style="font-size:small ;
<span style="font-size:small
oSheet.Cells[1, 4] =
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "Salary"<span style="font-size:small ;
<span style="font-size:small
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small //Format A11 as bold, vertical alignment = center.
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small <br/>
<span style="font-size:small
oSheet.get_Range(
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "A1"<span style="font-size:small ,
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "D1"<span style="font-size:small ).Font.Bold =
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small true<span style="font-size:small ;
<span style="font-size:small
oSheet.get_Range(
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "A1"<span style="font-size:small ,
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "D1"<span style="font-size:small ).VerticalAlignment =
<span style="font-size:small
Excel.XlVAlign.xlVAlignCenter;
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small // Create an array to multiple values at once.
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small <br/>
<span style="font-size:small
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small string<span style="font-size:small [,] saNames =
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small new<span style="font-size:small
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small string<span style="font-size:small [5, 2];
<span style="font-size:small
saNames[0, 0] =
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "John"<span style="font-size:small ;
<span style="font-size:small
saNames[0, 1] =
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "Smith"<span style="font-size:small ;
<span style="font-size:small
saNames[1, 0] =
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "Tom"<span style="font-size:small ;
<span style="font-size:small
saNames[1, 1] =
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "Brown"<span style="font-size:small ;
<span style="font-size:small
saNames[2, 0] =
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "Sue"<span style="font-size:small ;
<span style="font-size:small
saNames[2, 1] =
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "Thomas"<span style="font-size:small ;
<span style="font-size:small
saNames[3, 0] =
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "Jane"<span style="font-size:small ;
<span style="font-size:small
saNames[3, 1] =
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "Jones"<span style="font-size:small ;
<span style="font-size:small
saNames[4, 0] =
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "Adam"<span style="font-size:small ;
<span style="font-size:small
saNames[4, 1] =
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "Johnson"<span style="font-size:small ;
<span style="font-size:small
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small //Fill A2:B6 with an array of values (First and Last Names).
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small <br/>
<span style="font-size:small
oSheet.get_Range(
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "A2"<span style="font-size:small ,
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "B6"<span style="font-size:small ).Value2 = saNames;
<span style="font-size:small
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small //Fill C2:C6 with a relative formula (=A2 & " " & B2).
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small <br/>
<span style="font-size:small
oRng = oSheet.get_Range(
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "C2"<span style="font-size:small ,
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "C6"<span style="font-size:small );
<span style="font-size:small
oRng.Formula =
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "=A2 & " " & B2"<span style="font-size:small ;
<span style="font-size:small
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small //Fill D26 with a formula(=RAND()*100000) and apply format.
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small <br/>
<span style="font-size:small
oRng = oSheet.get_Range(
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "D2"<span style="font-size:small ,
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "D6"<span style="font-size:small );
<span style="font-size:small
oRng.Formula =
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "=RAND()*100000"<span style="font-size:small ;
<span style="font-size:small
oRng.NumberFormat =
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "$0.00"<span style="font-size:small ;
<span style="font-size:small
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small //AutoFit columns A.
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small <br/>
<span style="font-size:small
oRng = oSheet.get_Range(
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "A1"<span style="font-size:small ,
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "D1"<span style="font-size:small );
<span style="font-size:small
oRng.EntireColumn.AutoFit();
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small //Manipulate a variable number of columns for Quarterly Sales Data.
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small <br/>
<span style="font-size:small
DisplayQuarterlySales(oSheet);
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small //Make sure Excel is visible and give the user control
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small <br/>
<span style="font-size:small
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small //of Microsoft Excels lifetime.
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small <br/>
<span style="font-size:small
oXL.Visible =
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small true<span style="font-size:small ;
<span style="font-size:small
oXL.UserControl =
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small true<span style="font-size:small ;
<span style="font-size:small
}
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small catch<span style="font-size:small (<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small Exception<span style="font-size:small
theException)
<span style="font-size:small
{
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small String<span style="font-size:small errorMessage;
<span style="font-size:small
errorMessage =
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "Error: "<span style="font-size:small ;
<span style="font-size:small
errorMessage =
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small String<span style="font-size:small .Concat(errorMessage, theException.Message);
<span style="font-size:small
errorMessage =
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small String<span style="font-size:small .Concat(errorMessage,
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small " Line: "<span style="font-size:small );
<span style="font-size:small
errorMessage =
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small String<span style="font-size:small .Concat(errorMessage, theException.Source);
<span style="font-size:small
MessageBox.Show(errorMessage,
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "Error"<span style="font-size:small );
<span style="font-size:small
}
}
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small private<span style="font-size:small
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small void<span style="font-size:small DisplayQuarterlySales(Excel._Worksheet oWS)
<span style="font-size:small
{
Excel._Workbook oWB;
Excel.Series oSeries;
Excel.Range oResizeRange;
Excel._Chart oChart;
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small String<span style="font-size:small sMsg;
<span style="font-size:small
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small int<span style="font-size:small iNumQtrs;
<span style="font-size:small
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small //Determine how many quarters to display data for.
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small <br/>
<span style="font-size:small
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small for<span style="font-size:small (iNumQtrs = 4; iNumQtrs >= 2; iNumQtrs--)
<span style="font-size:small
{
sMsg =
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "Enter sales data for "<span style="font-size:small ;
<span style="font-size:small
sMsg =
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small String<span style="font-size:small .Concat(sMsg, iNumQtrs);
<span style="font-size:small
sMsg =
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small String<span style="font-size:small .Concat(sMsg,
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small " quarter(s)?"<span style="font-size:small );
<span style="font-size:small
DialogResult iRet = MessageBox.Show(sMsg,
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "Quarterly Sales?"<span style="font-size:small ,
<span style="font-size:small
MessageBoxButtons.YesNo);
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small if<span style="font-size:small (iRet == DialogResult.Yes)
<span style="font-size:small
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small break<span style="font-size:small ;
<span style="font-size:small
}
sMsg =
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "Displaying data for "<span style="font-size:small ;
<span style="font-size:small
sMsg =
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small String<span style="font-size:small .Concat(sMsg, iNumQtrs);
<span style="font-size:small
sMsg =
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small String<span style="font-size:small .Concat(sMsg,
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small " quarter(s)."<span style="font-size:small );
<span style="font-size:small
MessageBox.Show(sMsg,
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "Quarterly Sales"<span style="font-size:small );
<span style="font-size:small
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small //Starting at E1, fill headers for the number of columns selected.
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small <br/>
<span style="font-size:small
oResizeRange = oWS.get_Range(
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "E1"<span style="font-size:small ,
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "E1"<span style="font-size:small ).get_Resize(<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small Missing<span style="font-size:small .Value,
iNumQtrs);
<span style="font-size:small
oResizeRange.Formula =
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "="Q" & COLUMN()-4 & CHAR(10) & "Sales""<span style="font-size:small ;
<span style="font-size:small
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small //Change the Orientation and WrapText properties for the headers.
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small <br/>
<span style="font-size:small
oResizeRange.Orientation = 38;
oResizeRange.WrapText =
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small true<span style="font-size:small ;
<span style="font-size:small
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small //Fill the interior color of the headers.
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small <br/>
<span style="font-size:small
oResizeRange.Interior.ColorIndex = 36;
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small //Fill the columns with a formula and apply a number format.
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small <br/>
<span style="font-size:small
oResizeRange = oWS.get_Range(
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "E2"<span style="font-size:small ,
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "E6"<span style="font-size:small ).get_Resize(<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small Missing<span style="font-size:small .Value,
iNumQtrs);
<span style="font-size:small
oResizeRange.Formula =
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "=RAND()*100"<span style="font-size:small ;
<span style="font-size:small
oResizeRange.NumberFormat =
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "$0.00"<span style="font-size:small ;
<span style="font-size:small
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small //Apply borders to the Sales data and headers.
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small <br/>
<span style="font-size:small
oResizeRange = oWS.get_Range(
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "E1"<span style="font-size:small ,
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "E6"<span style="font-size:small ).get_Resize(<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small Missing<span style="font-size:small .Value,
iNumQtrs);
<span style="font-size:small
oResizeRange.Borders.Weight = Excel.XlBorderWeight.xlThin;
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small //Add a Totals formula for the sales data and apply a border.
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small <br/>
<span style="font-size:small
oResizeRange = oWS.get_Range(
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "E8"<span style="font-size:small ,
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "E8"<span style="font-size:small ).get_Resize(<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small Missing<span style="font-size:small .Value,
iNumQtrs);
<span style="font-size:small
oResizeRange.Formula =
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "=SUM(E2:E6)"<span style="font-size:small ;
<span style="font-size:small
oResizeRange.Borders.get_Item(Excel.XlBordersIndex.xlEdgeBottom).LineStyle
= Excel.XlLineStyle.xlDouble;
oResizeRange.Borders.get_Item(Excel.XlBordersIndex.xlEdgeBottom).Weight
= Excel.XlBorderWeight.xlThick;
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small //Add a Chart for the selected data.
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small <br/>
<span style="font-size:small
oWB = (Excel._Workbook)oWS.Parent;
oChart = (Excel._Chart)oWB.Charts.Add(
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small Missing<span style="font-size:small .Value,
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small Missing<span style="font-size:small .Value,
<span style="font-size:small
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small Missing<span style="font-size:small .Value,
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small Missing<span style="font-size:small .Value);
<span style="font-size:small
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small //Use the ChartWizard to create a new chart from the selected data.
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small <br/>
<span style="font-size:small
oResizeRange = oWS.get_Range(
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "E2:E6"<span style="font-size:small ,
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small Missing<span style="font-size:small .Value).get_Resize(
<span style="font-size:small
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small Missing<span style="font-size:small .Value, iNumQtrs);
<span style="font-size:small
oChart.ChartWizard(oResizeRange, Excel.XlChartType.xl3DColumn,
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small Missing<span style="font-size:small .Value,
<span style="font-size:small
Excel.XlRowCol.xlColumns,
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small Missing<span style="font-size:small .Value,
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small Missing<span style="font-size:small .Value,
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small Missing<span style="font-size:small .Value,
<span style="font-size:small
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small Missing<span style="font-size:small .Value,
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small Missing<span style="font-size:small .Value,
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small Missing<span style="font-size:small .Value,
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small Missing<span style="font-size:small .Value);
<span style="font-size:small
oSeries = (Excel.Series)oChart.SeriesCollection(1);
oSeries.XValues = oWS.get_Range(
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "A2"<span style="font-size:small ,
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "A6"<span style="font-size:small );
<span style="font-size:small
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small for<span style="font-size:small (<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small int<span style="font-size:small
iRet = 1; iRet <= iNumQtrs; iRet++)
<span style="font-size:small
{
oSeries = (Excel.Series)oChart.SeriesCollection(iRet);
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small String<span style="font-size:small seriesName;
<span style="font-size:small
seriesName =
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "="Q"<span style="font-size:small ;
<span style="font-size:small
seriesName =
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small String<span style="font-size:small .Concat(seriesName, iRet);
<span style="font-size:small
seriesName =
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small String<span style="font-size:small .Concat(seriesName,
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small """<span style="font-size:small );
<span style="font-size:small
oSeries.Name = seriesName;
}
oChart.Location(Excel.XlChartLocation.xlLocationAsObject, oWS.Name);
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small //Move the chart so as not to cover your data.
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small <br/>
<span style="font-size:small
oResizeRange = (Excel.Range)oWS.Rows.get_Item(10,
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small Missing<span style="font-size:small .Value);
<span style="font-size:small
oWS.Shapes.Item(
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "Chart 1"<span style="font-size:small ).Top = (<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small float<span style="font-size:small )(<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small double<span style="font-size:small )oResizeRange.Top;
<span style="font-size:small
oResizeRange = (Excel.Range)oWS.Columns.get_Item(2,
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small Missing<span style="font-size:small .Value);
<span style="font-size:small
oWS.Shapes.Item(
<span style="color:#800000; font-size:small <span style="color:#800000; font-size:small "Chart 1"<span style="font-size:small ).Left = (<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small float<span style="font-size:small )(<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small double<span style="font-size:small )oResizeRange.Left;
<span style="font-size:small
}
<span style="color:#808080; font-size:small <span style="color:#808080; font-size:small ///<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small <span style="color:#808080; font-size:small <span style="color:#808080; font-size:small <summary>
<span style="color:#808080; font-size:small <span style="color:#808080; font-size:small <br/>
<span style="font-size:small
<span style="color:#808080; font-size:small <span style="color:#808080; font-size:small ///<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small The SldWorks swApp variable is pre-assigned for you.
<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small <br/>
<span style="font-size:small
<span style="color:#808080; font-size:small <span style="color:#808080; font-size:small ///<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small <span style="color:#808080; font-size:small <span style="color:#808080; font-size:small </summary>
<span style="color:#808080; font-size:small <span style="color:#808080; font-size:small <br/>
<span style="font-size:small
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small public<span style="font-size:small
<span style="color:#008080; font-size:small <span style="color:#008080; font-size:small SldWorks<span style="font-size:small swApp;
<span style="font-size:small
}
}
The error message is provided below.
Error<span> 1, Line 95 Namespace <global namespace> contains a definition conflicting with alias Excel
The using statement is per example as: <span style="color:blue; line-height:115%; font-family:Courier New; font-size:10pt
using<span style="line-height:115%; font-family:Courier New; font-size:10pt Excel = Microsoft.Office.Interop.Excel;<span>
The end goal is to be able to access data from SolidWorks through C# programming preform calculations and then provide results in Excel.<span>
Any further examples or assistance would be greatly appreciated. Your time and efforts regarding this matter are greatly appreciated.
Thanks!
<span style="font-size:small SolidWorks.Interop.sldworks;
View the full article